标签:
Description
Input
Output
Sample Input
3 3 0 > 1 1 < 2 0 > 2 4 4 1 = 2 1 > 3 2 > 0 0 > 1 3 3 1 > 0 1 > 2 2 < 1
Sample Output
OK CONFLICT UNCERTAIN
一开始瞎做过不了 然后看了别人的代码才发现要用并查集 把=de的两个化为一个就好
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using namespace std; #define FIN freopen("input.txt","r",stdin); #define FOUT freopen("output.txt","w",stdout); #define INF 0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; const int MAXN=1e5+5; struct Edge{ int v,nxt; }E[MAXN*2]; int Head[MAXN],erear; int IN[MAXN],P[MAXN],sz; int Same[MAXN]; int A[MAXN],B[MAXN]; char OP[MAXN]; int Father[MAXN],Rank[MAXN]; int n,m; void all_init(){ erear=0; memset(Head,-1,sizeof(Head)); memset(IN,0,sizeof(IN)); for(int i=0;i<n;i++){ Father[i]=i; } } int Find(int x){ return Father[x]==x?x:(Father[x]=Find(Father[x])); } void edge_add(int u,int v){ E[erear].v=v; E[erear].nxt=Head[u]; Head[u]=erear++; } int main() { //FIN char op; while(~scanf("%d%d",&n,&m)) { int sum=n; all_init(); for(int i=0;i<m;i++){ scanf("%d %c %d",&A[i],&OP[i],&B[i]); if(OP[i]==‘=‘){ int root1=Find(A[i]); int root2=Find(B[i]); if(root1!=root2){ Father[root2]=root1; sum--; } } } for(int i=0;i<m;i++){ if(OP[i]==‘=‘){ continue; } else if(OP[i]==‘>‘){ int u=Find(A[i]); int v=Find(B[i]); edge_add(u,v); IN[v]++; } else if(OP[i]==‘<‘){ int u=Find(A[i]); int v=Find(B[i]); edge_add(v,u); IN[u]++; } } sz=0; queue<int>Q; while (!Q.empty()) Q.pop(); for(int i=0;i<n;i++){ if(!IN[i]&&Find(i)==i) Q.push(i); } bool sign=0; while(!Q.empty()){ if(Q.size()>=2) sign=1; int u=Q.front(); Q.pop(); P[sz++]=u; sum--; for(int i=Head[u];i!=-1;i=E[i].nxt){ int v=E[i].v; IN[v]--; if(!IN[v]) Q.push(v); } } /*for(int i=0;i<sz;i++){ if(i==0) printf("%d",P[i]); else printf(" %d",P[i]); } printf("\n"); cout<<"-----------------sz="<<sz<<endl; cout<<sum<<endl;*/ if(sum>0){ printf("CONFLICT\n"); } else if(sign){ printf("UNCERTAIN\n"); } else{ printf("OK\n"); } } return 0; }
HDU 1811 Rank of Tetris 拓扑排序+并查集
标签:
原文地址:http://www.cnblogs.com/Hyouka/p/5743167.html