Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:有人能用正确程序生成一些数据吗,错了很多遍就是不过...谢...In Reply To:有人能用正确程序生成一些数据吗,错了很多遍就是不过...谢... Posted by:mabaochang at 2009-08-03 09:31:36 > #include<iostream> using namespace std; struct pp{ int x,y; }; pp s[1000002]; pp tem[1000002]; int top; int g[1002][1002]; int low[1002]; int d[1002]; int n,m; int B; bool block[1002][1002]; int num=0; int color[1002]; bool used[1002]; int tt; void init(){ int i,j; int a,b; for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ g[i][j]=1; block[i][j]=false; } } for(i=1;i<=n;i++){ g[i][i]=-1; } for(i=1;i<=m;i++){ scanf("%d%d",&a,&b); g[a][b]=-1; g[b][a]=-1; } for(i=1;i<=n;i++){ d[i]=-1; color[i]=-1; used[i]=false; } top=1; B=0; num=0; } void dfs(int now,int p){ low[now]=d[now]=num++; int i; for(i=1;i<=n;i++){ if(g[now][i]==-1){ continue; } if(i!=p&&d[i]<d[now]){ s[top].x=now; s[top].y=i; top++; } if(d[i]<0){ dfs(i,now); low[now]=low[now]<low[i]? low[now]:low[i]; if(low[i]>=d[now]){ int j=0; pp key; do{ key=s[--top]; j++; tem[j]=key; }while(!(key.x==now&&key.y==i)); if(j>2){ B++; int k; for(k=1;k<=j;k++){ block[B][tem[k].x]=true; block[B][tem[k].y]=true; } } } } else{ if(i!=p){ low[now]=low[now]<d[i]? low[now]:d[i]; } } } } bool dfsc(int now,int c,int b){ int i; color[now]=c; for(i=1;i<=n;i++){ if(g[now][i]==-1||!block[b][i]){ continue; } if(color[i]==-1){ if(!dfsc(i,1-c,b)){ return false; } } else{ if(color[i]==c){ return false; } } } return true; } void cal(){ int i,j; for(i=1;i<=n;i++){ if(d[i]==-1){ dfs(i,-1); } } int ans=0; for(i=1;i<=B;i++){ int ok; for(j=1;j<=n;j++){ if(block[i][j]){ ok=dfsc(j,0,i); break; } } if(!ok){ for(j=1;j<=n;j++){ if(block[i][j]){ used[j]=true; } } } } for(i=1;i<=n;i++){ if(!used[i]){ ans++; } } cout<<ans<<endl; } int main(){ //freopen("a1.in","r",stdin); //freopen("in.txt","w",stdout); while(cin>>n>>m&&!(n==0&&m==0)){ init(); cal(); } return 0; } PS:贴代码,自己顶一下 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator