标签:for 一个 recommend math ace com amp queue sub
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12344 Accepted Submission(s): 3497
#include<stdio.h> #include<iostream> #include<math.h> #include<string.h> #include<set> #include<map> #include<list> #include<queue> #include<algorithm> using namespace std; typedef long long LL; int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int getval() { int ret(0); char c; while((c=getchar())==‘ ‘||c==‘\n‘||c==‘\r‘); ret=c-‘0‘; while((c=getchar())!=‘ ‘&&c!=‘\n‘&&c!=‘\r‘) ret=ret*10+c-‘0‘; return ret; } #define max_v 20005 int pa[max_v]; int rk[max_v]; int indgree[max_v]; int a[max_v],b[max_v]; char o[max_v]; queue<int> q; vector<int> vv[max_v]; int n,m,cnt; int fa_num; void init() { for(int i=0; i<=n; i++) pa[i]=i,rk[i]=0; memset(indgree,0,sizeof(indgree)); while(!q.empty()) q.pop(); for(int i=0; i<=n; i++) vv[i].clear(); fa_num=0; cnt=0; } int find_set(int x) { if(x!=pa[x]) pa[x]=find_set(pa[x]); return pa[x]; } void union_set(int x,int y) { x=find_set(x); y=find_set(y); if(x==y) return ; if(rk[x]>rk[y]) pa[y]=x; else { pa[x]=y; if(rk[x]==rk[y]) rk[y]++; } } int tpsort() { for(int i=1; i<=n; i++) { if(find_set(i)==i)//拓扑点必须是根结点 { fa_num++;//计数 根结点 if(indgree[i]==0) q.push(i); } } int temp; int flag=0; while(!q.empty()) { if(q.size()>1) flag=1;//信息不完全 temp=q.front(); q.pop(); cnt++; for(int i=0; i<vv[temp].size(); i++) { indgree[find_set(vv[temp][i])]--; if(indgree[find_set(vv[temp][i])]==0) q.push(find_set(vv[temp][i])); } } if(flag)//不能全拓扑(整个图不是连通的) return 1; else return 0; } int main() { int x,y; char c; while(~scanf("%d %d",&n,&m)) { if(n==1&&m==0) { printf("OK\n"); continue; } init(); int flag1=0;//冲突 1 int flag2=0;//信息不完全 1 for(int i=1; i<=m; i++)//必须先合并除去=号,不能边除去边拓扑!!! { getchar(); scanf("%d %c %d",&a[i],&c,&b[i]); a[i]++,b[i]++; o[i]=c; if(c==‘=‘) { union_set(a[i],b[i]); } } for(int i=1;i<=m;i++) { if(o[i]==‘=‘) continue; x=find_set(a[i]); y=find_set(b[i]); if(o[i]!=‘=‘&&find_set(x)==find_set(y)) { flag1=1; continue; } if(o[i]==‘<‘) { int temp=x; x=y; y=temp; } if(count(vv[y].begin(),vv[y].end(),x)!=0)//环的一种情况 { flag1=1; } if(count(vv[x].begin(),vv[x].end(),y)==0)//预防重边 { vv[x].push_back(y); indgree[y]++; } } flag2=tpsort(); if(cnt!=fa_num)//不能全排列拓扑,因为信息不全 flag1=1; if(flag1==0&&flag2==0) { printf("OK\n"); } else if(flag1==1&&flag2==1) { printf("CONFLICT\n"); } else if(flag2==1&&flag1==0) { printf("UNCERTAIN\n"); } else if(flag1==1&&flag2==0) { printf("CONFLICT\n"); } } return 0; }
HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)
标签:for 一个 recommend math ace com amp queue sub
原文地址:https://www.cnblogs.com/yinbiao/p/9845756.html