题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define MAXN 10001
using namespace std;
vector<int> mp[MAXN];
queue<int>Q;
int n,sum;
int indegree[MAXN],father[MAXN];
int fx[20001],fy[20001];
char ch[20001];
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
int Union(int x,int y)
{
int findx=find(x);
int findy=find(y);
if(findx==findy)
return 0;//已经在同一个集合不需合并
father[findx]=findy;
return 1;
}
void toposort()
{
int i,j,x,t,uncertain;
while (!Q.empty())
{
Q.pop();
}
for(i=0;i<n;++i)
{
if(!indegree[i]&&(father[i]==i))
Q.push(i);//将入度为0,且是根节点放入队列中
}
uncertain=0;
while(!Q.empty())
{
if(Q.size()>1) //确定信息不全,但是不能跳出,此时还可能是冲突
uncertain=1;
x=Q.front();
Q.pop();
--sum;
for(j=0;j<mp[x].size();++j)
{
t=mp[x][j];
indegree[t]--;
if(!indegree[t])
Q.push(t);
}
}
if(sum>0)
printf("CONFLICT\n");
else if(uncertain)
printf("UNCERTAIN\n");
else
printf("OK\n");
}
void Init()
{
int i;
for(i=0;i<n;++i)
{
father[i]=i;
mp[i].clear();
indegree[i]=0;
}
}
int main()
{
int m,i,x,y;
while(scanf("%d %d",&n,&m)!=EOF)
{
sum=n;
Init();
for(i=0;i<m;++i)
{
scanf("%d %c %d",&fx[i],&ch[i],&fy[i]);
if(ch[i]=='=')
{
if(Union(fx[i],fy[i]))
sum--;//相等的按一个算,总数减少1
}
}
for(i=0;i<m;++i)
{
if(ch[i]!='=')
{
x=find(fx[i]);
y=find(fy[i]);
if(ch[i]=='>')
{
mp[x].push_back(y);
indegree[y]++;
}
else
{
mp[y].push_back(x);
indegree[x]++;
}
}
}
toposort();
}
return 0;
}