http://acm.hdu.edu.cn/showproblem.php?pid=1811

拓扑排序和并差集

 #include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 10010
using namespace std;
int n,m,c;
int f[maxn],in[maxn];
vector<int>g[maxn];
struct node
{
int x,y;
char ch;
}p[maxn*];
void inti()
{
for(int i=; i<=n; i++)
{
f[i]=i; in[i]=;
g[i].clear();
}
} int find1(int x)
{
if(x==f[x]) return x;
return f[x]=find1(f[x]);
} void merge1(int a,int b)
{
int fa=find1(a);
int fb=find1(b);
if(fa!=fb)
{
f[fb]=fa;
}
} int topp()
{
int flag=;
queue<int>q;
for(int i=; i<n; i++)
{
if(!in[i]&&find1(i)==i) q.push(i);
}
int cnt=;
while(!q.empty())
{
if(q.size()>=) flag=-;
int x=q.front(); q.pop();
cnt++;
for(int i=; i<(int)g[x].size(); i++)
{
if((--in[g[x][i]])==) q.push(g[x][i]);
}
}
if(cnt<c) return ;
return flag;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
inti();
c=n;
for(int i=; i<m; i++)
{
scanf("%d%*c%c%*c%d",&p[i].x,&p[i].ch,&p[i].y);
if(p[i].ch=='=')
{
merge1(p[i].x,p[i].y); c--;
}
}
bool flag=false;
for(int i=; i<m; i++)
{
if(p[i].ch=='=')
{
continue;
}
int x1=find1(p[i].x); int y1=find1(p[i].y);
if(x1==y1) flag=true;
if(p[i].ch=='<')
{
if(find(g[x1].begin(),g[x1].end(),y1)==g[x1].end())
{
g[x1].push_back(y1);
in[y1]++;
}
}
else if(p[i].ch=='>')
{
if(find(g[y1].begin(),g[y1].end(),x1)==g[y1].end())
{
g[y1].push_back(x1);
in[x1]++;
}
}
}
bool flag1=false;
if(!flag)
{
int cc=topp();
if(cc==) flag=true;
else if(cc==-) flag1=true;
}
if(flag)
{
printf("CONFLICT\n");
}
else if(flag1)
{
printf("UNCERTAIN\n");
}
else printf("OK\n");
}
return ;
}

hdu 1811 Rank of Tetris的更多相关文章

  1. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

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

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

    题目链接: 传送门 Rank of Tetris Time Limit: 1000MS     Memory Limit: 32768 K Description 自从Lele开发了Rating系统, ...

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

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

  5. HDU 1811 Rank of Tetris 拓补排序+并查集

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

  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. hdu 1811 Rank of Tetris (拓扑 & 并查集)

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

  8. hdu 1811 Rank of Tetris - 拓扑排序 - 并查集

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

  9. HDU 1811 Rank of Tetris 【拓扑排序 + 并查集】

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

随机推荐

  1. MySQL 学习笔记 (limit offset)

    select * from table limit (10000,10);这样是很慢的,因为要定位 比较快的写法是 select * from table where id >=(select ...

  2. vbox安装mac os x

    http://zhiwei.li/text/2013/12/%E5%9C%A8virtualbox4-3-4%E4%B8%AD%E5%AE%89%E8%A3%85mavericks/ http://g ...

  3. Linux企业级项目实践之网络爬虫(9)——通过URL抓取网页内容

    基本URL包含模式(或称协议).服务器名称(或IP地址).路径和文件名,如"协议://授权/路径?查询".完整的.带有授权部分的普通统一资源标志符语法看上去如下:协议://用户名: ...

  4. 【转】Android中BindService方式使用的理解

    原文网址:http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html 最近学习了一下Android里面的Service的应 ...

  5. C++基础回顾1(数据类型, 控制语句, 数组)

    最近两天打开本科学校的C++教材,快速回顾了一下C++方面的内容.虽然书本内容比较基础,但是还是有些知识点值得自己强化记忆.分几篇文章,加上自己的理解记录如下. 先回顾面向过程的部分. C++数据类型 ...

  6. poj 2566 Bound Found(尺取法 好题)

    Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...

  7. java 字符串为空问题

    java 字符串为空问题 String testStr = null; System.out.println(testStr); if (testStr == null) { System.out.p ...

  8. poj 2388 Who&#39;s in the Middle

    Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31149   Accepted: 1 ...

  9. JavaLearning:JAVA IO Piped流

    package org.fun.io; import java.io.IOException; import java.io.PipedInputStream; import java.io.Pipe ...

  10. ViewPager实现页卡的3种方法(谷歌组件)

    ----方法一:---- 效果图: 须要的组件: ViewPager+PagerTabStrip 布局文件代码: <!--xmlns:android_custom="http://sc ...