Data |
Problem |
name |
times |
solution |
10.28 |
POJ 1753 |
Flip Game |
20min |
枚举 |
10.29 |
POJ 3368 |
Frequent values |
1h |
RMQ |
10.30 |
|
树的最大独立集 |
20min |
树dp |
poj1753
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
char s[5];
int a[5][5];
int b[5][5];
int main()
{
Rep(i,4) {
scanf("%s",s);
Rep(j,4)
a[i][j]= (s[j] == 'b')?1:0;
}
int ans=100;
Rep(i,1<<16)
{
memcpy(b,a,sizeof(b));
int l=0;
Rep(j,16)
{
int x=j/4,y=j%4;
if (i&(1<<j)) {
++l;
b[x][y]^=1;
if (x<3) b[x+1][y]^=1;
if (y<3) b[x][y+1]^=1;
if (x>0) b[x-1][y]^=1;
if (y>0) b[x][y-1]^=1;
}
}
int t=0;
Rep(x,4) Rep(y,4) t+=b[x][y];
if (!t||t==16) {
ans=min(ans,l);
}
}
if (ans<=16) cout<<ans<<endl;
else cout<<"Impossible"<<endl;
return 0;
}
poj 3368
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define N (100000)
#define MAXN (500000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n,m;
int a[MAXN];
int L[MAXN],R[MAXN];
int h[MAXN];
double A[MAXN];
double f[MAXN][32];
int main()
{
while(~scanf("%d%d",&n,&m) && n) {
MEM(h)
For(i,n) scanf("%d",&a[i]),a[i]+=N+1,h[a[i]]++;
MEM(R) MEMI(L)
int nn=2*N+1;
For(i,nn) f[i][0]=(double)h[i];
For(i,n) L[a[i]]=min(L[a[i]],i),R[a[i]]=max(R[a[i]],i);
for(int j=1;(1<<j)<=nn;j++) {
for(int i=1;i+(1<<j)-1<=nn;i++) {
f[i][j]=max(f[i][j-1],f[i+(1<<j)-(1<<j-1)][j-1]);
}
}
For(i,m) {
int l,r;
scanf("%d%d",&l,&r);
int l2=a[l]+1,r2=a[r]-1;
int ma2=min(r,R[a[r]])-max(l,L[a[r]])+1;
int ma3=min(r,R[a[l]])-max(l,L[a[l]])+1;
int ans=max(ma2,ma3);
if (l2<=r2) {
int p=0,len=r2-l2+1;
while ((1<<p+1)<=len) ++p;
double ma = max(f[l2][p] , f[r2-(1<<p)+1][p] );
ans=max(ans,(int)ma);
}
printf("%d\n",ans);
}
}
return 0;
}
树的最大独立集
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (100000+10)
vector<int> son[MAXN];
int n,d[MAXN]={0},f[MAXN],s[MAXN],gs[MAXN];
void dfs(int x,int fa){
int m=son[x].size();
Rep(i,m) {
if (son[x][i]==fa) continue;
f[son[x][i]]=x;
dfs(son[x][i],x);
}
d[x]=max(s[x],1+gs[x]);
if (fa)
{
s[fa] += d[x];
if (f[fa]) gs[f[fa]]+=d[x];
}
}
int main()
{
freopen("树的最大独立集.in","r",stdin);
cin>>n;
For(i,n-1){
int x,y;
x=read(),y=read();
son[x].pb(y);
son[y].pb(x);
}
dfs(1,0);
cout<<d[1]<<endl;
return 0;
}