#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
struct node//边
{
int a, b;//顶点
char ch;//运算符
}c[];
vector<int>map[];//map数组存贮邻接表 (大佬都是这么开数组的)
int n, m, sum, in[], fa[];//in数组表示入度,fa[i]表示顶点i所在集合的根节点 int find(int x)//查找根节点
{
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
} bool comb(int x, int y)//合并集合
{
x = find(x);
y = find(y);
if (x == y)
return false;
else
{
fa[y] = x;
return true;
}
} void init()//初始化
{
for (int i = ; i<n; i++)
fa[i] = i;
} void top_sort()//queue实现拓扑排序
{
queue<int>s;
int flag = ;
for (int i = ; i<n; i++)
{
//找到入度为零的切祖宗为自己的加入到队列中
if (in[i] == && fa[i] == i)
s.push(i);
}
while (!s.empty())
{
if (s.size() > )//即使发现信息不完整也要继续运行下去,因为如果信息同时不完整和冲突都是CONFLICT
flag = ;
int pos = s.front();
s.pop(), sum--; //记录下运行的次数
for (int i = ; i<map[pos].size(); i++)
{
in[map[pos][i]]--;
if (in[map[pos][i]] == )
s.push(map[pos][i]);
}
}
if (sum>) printf("CONFLICT\n"); //冲突,即有多个入度为零且祖宗为自己的出现
else if (flag) printf("UNCERTAIN\n");
else printf("OK\n");
} int main()
{
while (scanf("%d %d", &n, &m) != EOF)
{
sum = n;
init();
memset(map, , sizeof(map));
memset(in, , sizeof(in));
for (int i = ; i<m; i++)
{
scanf("%d %c %d", &c[i].a, &c[i].ch, &c[i].b);
//如果相等,就合并为同一个集合
if (c[i].ch == '=')
{
if (comb(c[i].a, c[i].b))
sum--;
}
}
for (int i = ; i<m; i++)
{
if (c[i].ch == '=')
continue;
int x = find(c[i].a);
int y = find(c[i].b);
if (c[i].ch == '>')
{
map[x].push_back(y);
in[y]++;
}
else
{
map[y].push_back(x);
in[x]++;
}
}
top_sort();
}
//system("pause");
return ;
}

hdu1811 Rank of Tetris 并查集+拓扑排序的更多相关文章

  1. hdu 1811 Rank of Tetris (并查集+拓扑排序)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1811Rank of Tetris (并查集 + 拓扑排序)

    /* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...

  3. HDU 1811:Rank of Tetris(并查集+拓扑排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description   自从Lele开发了Rating系 ...

  4. 并查集+拓扑排序 赛码 1009 Exploration

    题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...

  5. 【并查集+拓扑排序】【HDU1811】【Rank of Tetris】

    题意:给你3种关系 A=B,A>B,A<B 问是否排名方式唯一,或者存在矛盾 解 1.读入数据先处理 =号 用并查集的祖先作为代表元素,其他儿子节点都等于跟这个点重叠. 再读入 '< ...

  6. HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. [HDOJ1811]Rank of Tetris(并查集、拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811 求一堆数据的拓扑序. 处理:x>y就是x到y一条边,x<y就是y到x一条边.关键问题 ...

  8. HDU1811 并查集+拓扑排序

    题目大意: 判断是否能根据给定的规则将这一串数字准确排序出来 我们用小的数指向大的数 对于相等的情况下,将二者合并到同一个并查集中,最后抽象出来的图上面的每一个点都应该代表并查集的标号 #includ ...

  9. HDU 1811(并查集+拓扑排序)题解

    Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他 ...

随机推荐

  1. HDU 2955 Robberies(01背包变形)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. Firefox OS开发指南

    在海外社区Leanpub上线了<Firefox OS App开发>高速指南,指引开发人员尝试新技术. 这款<Firefox OS App开发>高速指南现已开放下载.HTML5开 ...

  3. Arcgis Engine(ae)接口详解(2):featureClass查询

    //属性查询~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //IQueryFilter代表查询条件,QueryFilterClass代表只限于属性查询(就是没有空间查询) ...

  4. android 编程小技巧(持续中)

    first:     Intent跳转一般存用于Activity类,可是若要在非activity类里跳转的话,解决方法是在startActivity(intent)前加mContext即上下文,终于为 ...

  5. fatal error C1083: 无法打开预编译头文件:“Debug\opencv.pch”: No such file or directory

    stdafx.cpp右键——属性,预编译头选“创建”,其它cpp选“使用”. 调试不能优化.

  6. ivy

    ivy https://ant.apache.org/ivy/ Apache Ivy™ is a popular dependency manager focusing on flexibility ...

  7. Delphi中取得汉字的首字母简单方法(十分巧妙)

    //从朝闻道的博客里转载,原文定义AHzStr: String,发现结果为空,后来改成AHzStr: AnsiString就可以了 function GetHzPy(const AHzStr: Ans ...

  8. (linux)wake_lock机制

      Android的休眠唤醒主要基于wake_lock机制,只要系统中存在任一有效的wake_lock,系统就不能进入深度休眠,但可以进行设备的浅度休眠操作.wake_lock一般在关闭lcd.tp但 ...

  9. 初探linux子系统集之led子系统(三)【转】

    本文转载自:http://blog.csdn.net/eastmoon502136/article/details/37822837 世界杯结束了,德国战车夺得了大力神杯,阿根廷最终还是失败了.也许3 ...

  10. POJ2195 Going Home —— 最大权匹配 or 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total ...