【并查集+拓扑排序】【HDU1811】【Rank of Tetris】
题意:给你3种关系 A=B,A>B,A<B 问是否排名方式唯一,或者存在矛盾
解
1.读入数据先处理 =号 用并查集的祖先作为代表元素,其他儿子节点都等于跟这个点重叠。 再读入 ‘<’ ‘>’ 来建图进行拓扑排序
2.将所有入度为0的点加入队列,再从队列中取出一个点,对其所连的边的入度进行-1,如果使入度=0则加入队列,直至队列为空
3.如果进入队列的点
小于 总点数(非N,N-重点) 则有矛盾
如果有队列中元素>=2的时刻
则证明不存在唯一的排名方式
其余
OK;
代码如下:
/*
WA1 调试数据
2 2
0 = 1
0 = 1
错误输出 CONFLIET
正确输出 OK 错误原因 if(tot<N) printf("CONFLICT\n"); 忘记了重点(合并的点),所以总共要遍历的点少于N
*/
//Warning 虽然这里没必要 不过还是要的记得清空队列
//结果 Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
//结果 13306745 2015-03-31 21:40:45 Accepted 1811 31MS 1972K 2378 B C++ ZHOU #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include<queue>
#define oo 0x13131313
const int maxn=10005;
int N,M; struct fa
{
int a,b;
char c;
};
fa A[4*maxn];
struct Edge{
int to;
Edge *next;
};
struct Node{
int num;
Edge *first;
};
Edge E[maxn*4],*EE=E+1;
Node Graph[maxn];
int father[maxn];
using namespace std;
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void Union(int a,int b)
{
int aa=find(a),bb=find(b);
if(aa!=bb) father[aa]=bb;
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
void Link(int u,int v)
{
EE->to=v,EE->next=Graph[u].first,Graph[u].first=EE++;
Graph[v].num++;
}
void CLEAR()
{
memset(Graph,0,sizeof(Graph));
EE=E+1;
for(int i=0;i<=maxn-1;i++)
father[i]=i;
}
void input()
{
CLEAR();
for(int i=1;i<=M;i++)
{
scanf("%d %c %d\n",&A[i].a,&A[i].c,&A[i].b);
if(A[i].c=='=') Union(A[i].a,A[i].b);
}
for(int i=1;i<=M;i++)
{
if(A[i].c=='<') Link(find(A[i].a),find(A[i].b));
else if(A[i].c=='>') Link(find(A[i].b),find(A[i].a));
}
}
void solve()
{ int ok=1;
int tot=0;
int kk=0;
queue <int> Q;
while(!Q.empty()) Q.pop();
for(int i=0;i<N;i++)
if(father[i]==i)
{
kk++;
if(Graph[i].num==0)
Q.push(i);
}
while(!Q.empty())
{
if(Q.size()>=2) ok=0;
int t=Q.front();Q.pop();tot++;
for(Edge *p=Graph[t].first;p;p=p->next)
{
Graph[p->to].num--;
if(Graph[p->to].num==0) Q.push(p->to);
}
} if(tot<kk) printf("CONFLICT\n");
else if(ok==0) printf("UNCERTAIN\n");
else printf("OK\n");
}
int main()
{
// init();
while(cin>>N>>M)
{
input();
solve();
}
return 0;
}
【并查集+拓扑排序】【HDU1811】【Rank of Tetris】的更多相关文章
- HDU 1811:Rank of Tetris(并查集+拓扑排序)
http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description 自从Lele开发了Rating系 ...
- 并查集+拓扑排序 赛码 1009 Exploration
题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...
- hdu1811 Rank of Tetris 并查集+拓扑排序
#include <stdio.h> #include <string.h> #include <vector> #include <queue> us ...
- HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU1811 并查集+拓扑排序
题目大意: 判断是否能根据给定的规则将这一串数字准确排序出来 我们用小的数指向大的数 对于相等的情况下,将二者合并到同一个并查集中,最后抽象出来的图上面的每一个点都应该代表并查集的标号 #includ ...
- hdu 1811 Rank of Tetris (并查集+拓扑排序)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1811Rank of Tetris (并查集 + 拓扑排序)
/* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
随机推荐
- iOS 系统架构 && 常用 framework
整理自互联网,感谢原文作者! 1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch lay ...
- Team Formation(思维)
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- Java String类具体解释
Java String类具体解释 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,非常多时候,我们对它既熟悉又陌生. 类结构: public fin ...
- MHA自动切换流程
MHA的全名叫做mysql-master-ha,配置后可以在10-30秒内完成master自动切换,切换过程如下: 1. 检测master的状态,方法是一秒一次“ SELECT 1 As Value” ...
- javax inect
Spring 3 and JSR-330 @Inject and @Named example By mkyong | September 16, 2012 | Viewed : 86,399 tim ...
- VLC-Android和VLC几个关键宏定义的分析
在用SourceInsight分析VLC-Android源码过程中,有几个宏定义在源代码中一直没有找到出处,比如 HAVE_DYNAMIC_PLUGINS和__PLUGIN__,以及MODULE_NA ...
- sql必知必会(第四版) 学习笔记二 视图
本书用到的几个表的建表sql语句如下: --销售产品供应商 CREATE TABLE Vendors ( vend_id varchar(20) not null, vend_name varchar ...
- asp.net 常用的3中身份验证
1. windows验证: IIS根据应用程序的设置来进行身份验证,要使用这中验证方式,必须禁止使用匿名用户登录. 2. Forms验证: 通过Cookies来保存用户凭证,对未登录的用户 重定向到自 ...
- Android 判断文件的类型
import java.util.HashMap; import java.util.Iterator; /** * 判断文件的类型 */ public class MediaFileUtil { p ...
- c# 使用oledb 写入导出excel设置单元格为成数字格式 设置了不起作用
使用oledb 导出过程中,如果excel安装版本低于2010,无论怎么设置.导出的都是文本格式. 用代码-使用数据-分列,解决