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
OK CONFLICT UNCERTAIN
分析:因为是有相等的所以用并查集将相等的合成一个集合,之后再对处理过的图进行拓扑排序。
注:1》为什么用并查集将相等的合并到一起在排序就可以了,因为题目还有RP这一值,所以我们只需要对处理好的排序就好了,就是对父节点等于它本身的点来拓扑排序。
2》判断是冲突还是信息不完全,可以统计每一次入队列的的个数,如果是大于1的话,就说明有两个点的度数为0,那么他们的关系就无法确定,就是信息不完全,如果到最后还有没排好的就说明有冲突。。。
3》如果wa的话,试一下将判断‘>’改为‘<’,以及另外一个。
代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#define M 10005
using namespace std;
vector<int>map[M];
int n, m, tot;
int fat[M], in[M], a[M<<1], b[M<<1];
char ch[M];
int f(int x){
if(fat[x] != x) fat[x] = f(fat[x]);
return fat[x];
}
int merge(int a, int b){
int x = f(a); int y = f(b);
if(x == y) return 0;
if(x != y) fat[x] = y;
return 1;
}
int toposort(){
queue<int> q;
int i, flag = 0;
for(i = 0; i < n; i ++){
if(in[i] == 0&&fat[i] == i) q.push(i);
}
while(!q.empty()){
if(q.size() >= 2) flag = 1;
int temp = q.front();
q.pop();
tot --;
for(i = 0; i < map[temp].size(); i ++){
if(! --in[map[temp][i]]){
q.push(map[temp][i]);
}
}
}
if(tot > 0) flag = 2;
return flag;
}
int main(){
while(scanf("%d%d", &n, &m) == 2){
memset(in, 0, sizeof(in));
for(int i = 0; i < M; i ++){
fat[i] = i;
map[i].clear();
}
tot = n;
for(int i = 0; i < m; i ++){
scanf("%d %c %d", &a[i], &ch[i], &b[i]);
if(ch[i] == '='&&merge(a[i], b[i])){
tot --;
}
}
for(int i = 0; i < m; i ++){
int x = f(a[i]); int y = f(b[i]);
//if(x == y) continue;
if(ch[i] == '<'){ //就是这里,wa了好多次
map[x].push_back(y);
++in[y];
}
else if(ch[i] == '>'){
map[y].push_back(x); ++in[x];
}
}
int flag = toposort();
if(flag == 2) printf("CONFLICT\n");
else if(flag == 1) printf("UNCERTAIN\n");
else printf("OK\n");
}
return 0;
}hdoj 1811 Rank of Tetris 【拓扑】+【并查集】
原文地址:http://blog.csdn.net/shengweisong/article/details/41259683