标签:des style blog http io color ar os sp
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5415 Accepted Submission(s): 1514
1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<iterator> 6 #include<queue> 7 #include<functional> 8 using namespace std; 9 10 const int maxn=20005; 11 const int nn=10005; 12 13 struct path{ 14 15 int fr,to; 16 char ss[2]; 17 }tt[maxn]; 18 19 vector<int>aa[nn]; //模拟邻接表 20 int n,m,num; 21 int father[nn]; 22 int inde[nn]; //入度 23 24 void init(){ 25 for(int i=0;i<n;i++) 26 father[i]=i; 27 } 28 29 int fin(int x){ 30 while(x!=father[x]) 31 x=father[x]; 32 return x; 33 } 34 35 void unin(int a,int b){ 36 father[b]=a; 37 } 38 39 int main() 40 { 41 #ifdef LOCAL 42 freopen("test.in","r",stdin); 43 #endif 44 45 while(scanf("%d%d",&n,&m)!=EOF){ 46 init(); 47 num=n; 48 for(int i=0;i<m;i++){ 49 scanf("%d%s%d",&tt[i].fr,tt[i].ss,&tt[i].to); 50 if(tt[i].ss[0]==‘=‘){ 51 int x=fin(tt[i].fr); 52 int y=fin(tt[i].to); 53 if(x!=y){ 54 unin(x,y); 55 num--; 56 } 57 } 58 } 59 60 bool err=0,fight=0; 61 memset(inde,0,sizeof(int)*(n+1)); 62 63 for(int i=0;i<n;i++) 64 aa[i].clear(); 65 66 for(int i=0;i<m;i++) 67 { 68 if(tt[i].ss[0]!=‘=‘) 69 { 70 int x=fin(tt[i].fr); 71 int y=fin(tt[i].to); 72 if(x==y){ 73 err=1; //有矛盾 74 break; 75 } 76 //构建拓扑图 77 if(tt[i].ss[0]==‘>‘){ 78 aa[x].push_back(y); 79 inde[y]++; 80 } 81 else{ 82 aa[y].push_back(x); 83 inde[x]++; //记录入读的情况 84 } 85 } 86 } 87 88 if(err){ 89 printf("CONFLICT\n"); 90 continue; 91 } 92 93 queue<int>tuop; 94 //找出所有入度为0的点。 95 for(int i=0;i<n;i++) 96 if(inde[i]==0&&i==fin(i)) 97 tuop.push(i); 98 99 while(!tuop.empty()){ 100 if(tuop.size()>1){ 101 fight=1; 102 } 103 104 int gt=tuop.front(); 105 tuop.pop(); 106 num--; 107 vector<int>::iterator ww; 108 for(ww=aa[gt].begin(); ww!=aa[gt].end();ww++){ 109 inde[*ww]--; 110 if(inde[*ww]==0) 111 tuop.push(*ww); 112 } 113 } 114 if(num>0){ //成环 115 printf("CONFLICT\n"); 116 continue; 117 } 118 if(!fight) printf("OK\n"); 119 else printf("UNCERTAIN\n"); 120 } 121 return 0; 122 }
hdu 1811 Rank of Tetris (并查集+拓扑排序)
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/gongxijun/p/4094208.html