# hdu1811 Rank of Tetris（并查集 + 拓扑排序）

题意：

$AC \ Code:$

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N =  2e4 + 10;
int u[N],s[N],v[N];

int Find(int x) {return pre[x] == -1?x:pre[x] = Find(pre[x]);}
void join(int x,int y){
int p = Find(x),q = Find(y);
if(p != q){
pre[p] = q;
}
}
int res[N];
bool vis[N];
struct Edge{
int next;
int to;
}edge[N<<1];
edge[tot].to = to;
deg[to] ++;
}
int topsort(int n,int k){
queue<int> Q;
for(int i = 1;i <= n;++i) if(!vis[Find(i)]&&!deg[Find(i)]) {vis[Find(i)] = 1;Q.push(Find(i));}
int cnt = 0;
bool r = 1;
while(Q.size()){
if(Q.size() >= 2) r = 0;
int x = Q.front();
x = Find(x);
res[++cnt] = x;
Q.pop();
for(int i = head[x];~i;i = edge[i].next){
int y = edge[i].to;
y = Find(y);
if(-- deg[y] == 0) Q.push(y);
}
}
if(cnt != k) return -1;
else if(r == 0) return 1;
else return 0;

}
inline void init(){
memset(pre,-1,sizeof pre);
memset(deg,0,sizeof deg);
memset(vis,0,sizeof vis);
tot = 0;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m) == 2){
init();
//int u,v;char s;
for(int i = 1;i <= m;++i){
scanf("%d%s%d",&u[i],&s[i],&v[i]);
u[i] ++;v[i] ++;
if(s[i] == '=') join(u[i],v[i]);
}
for(int i = 1;i <= m;++i){
if(s[i] == '>') {
}
else if(s[i] == '<'){
}
}
int sum = 0;
for(int i = 1;i <= n;++i) {
int fa = Find(i);
if(!vis[fa]) {sum ++;vis[fa] = 1;}
}
memset(vis,0,sizeof vis);
int ans = topsort(n,sum);
if(ans == -1) puts("CONFLICT");
else if(ans == 0) puts("OK");
else puts("UNCERTAIN");
}

}


©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客