[HDOJ1811]Rank of Tetris(并查集、拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811
求一堆数据的拓扑序。
处理:x>y就是x到y一条边,x<y就是y到x一条边。关键问题是处理x=y的情况。
假如x=y,就有问题了。假如不处理的话,可能会被当成少处理一个点而使结果编程UNCERTAIN。所以我们考虑用并查集来解决这个问题。
选谁当祖先?题中又给了一个其他的量叫做RP值,这个RP值的规律是序号越大RP值越大。这样我们可以在合并的时候,尽可能地将RP值大的数当成本集合的祖先。
/*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Edge {
int u, v, next;
Edge() {}
Edge(int uu, int vv) : u(uu), v(vv) { next = -; }
}Edge;
const int maxn = ;
const int maxm = ;
int n, m;
Edge edge[maxm];
int head[maxn];
int hcnt;
int pre[maxn];
int in[maxn];
int num, q[maxn], front, tail;
int uu[maxn], vv[maxn];
char cc[maxn][]; void adde(int u, int v) {
edge[hcnt] = Edge(u, v);
edge[hcnt].next = head[u];
head[u] = hcnt++;
in[v]++;
} int find(int x) {
RT x == pre[x] ? x : pre[x] = find(pre[x]);
} int unite(int x, int y) {
int fx = find(x);
int fy = find(y);
if(fx == fy) RT ;
if(x > y) pre[fy] = fx;
else pre[fx] = fy;
return ; } int topo() {
front = tail = ;
Rep(i, n) {
if(in[i] == && i == find(i)) {
q[tail++] = i;
}
}
int ret = ;
while(front < tail) {
if(tail - front > ) ret = ;
int u = q[front++];
--num;
for(int i = head[u]; ~i; i=edge[i].next) {
int v = edge[i].v;
if(--in[v] == ) {
q[tail++] = v;
}
}
}
if(num > ) printf("CONFLICT\n");
else if(ret) printf("UNCERTAIN\n");
else printf("OK\n");
} int main() {
// FRead();
int u, v;
while(~Rint(n) && ~Rint(m)) {
Cls(in); Clr(head, -); hcnt = ;
num = n;
Rep(i, n+) pre[i] = i;
Rep(i, m) {
Rint(uu[i]); Rs(cc[i]); Rint(vv[i]);
if(cc[i][] == '=') {
if(unite(uu[i], vv[i])) num--;
}
}
Rep(i, m) {
if(cc[i][] != '=') {
u = find(uu[i]); v = find(vv[i]);
if(cc[i][] == '>') adde(u, v);
if(cc[i][] == '<') adde(v, u);
}
}
topo();
}
RT ;
}
[HDOJ1811]Rank of Tetris(并查集、拓扑排序)的更多相关文章
- hdu 1811 Rank of Tetris (并查集+拓扑排序)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu1811 Rank of Tetris 并查集+拓扑排序
#include <stdio.h> #include <string.h> #include <vector> #include <queue> us ...
- hdu 1811Rank of Tetris (并查集 + 拓扑排序)
/* 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B ...
- HDU 1811:Rank of Tetris(并查集+拓扑排序)
http://acm.hdu.edu.cn/showproblem.php?pid=1811 Rank of Tetris Problem Description 自从Lele开发了Rating系 ...
- 并查集+拓扑排序 赛码 1009 Exploration
题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...
- HDU 1811 Rank of Tetris(并查集+拓扑排序 非常经典)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【并查集+拓扑排序】【HDU1811】【Rank of Tetris】
题意:给你3种关系 A=B,A>B,A<B 问是否排名方式唯一,或者存在矛盾 解 1.读入数据先处理 =号 用并查集的祖先作为代表元素,其他儿子节点都等于跟这个点重叠. 再读入 '< ...
- HDU 1811(并查集+拓扑排序)题解
Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球.为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
随机推荐
- Linq小技巧
遍历集合ForEach: listAll.Items.Clear(); List<Users> list = DP.UsersDAO.GetInfoList(); list.ForEach ...
- mysql语句大全
转自:http://www.cnblogs.com/yunf/archive/2011/04/12/2013448.html 1.说明:创建数据库 CREATE DATABASE database ...
- C++中的多重继承与虚继承的问题
1.C++支持多重继承,但是一般情况下,建议使用单一继承. 类D继承自B类和C类,而B类和C类都继承自类A,因此出现下图所示情况: A A \ / B C ...
- python pip和easy_install使用方式(转载)
easy_install 跟 pip 都是Python 的套件管理程式,有了它们,在使用 Python 开发程式的时候会带来不少方便. easy_install 和pip 有什麼不一样?据 pip 官 ...
- C# File
http://msdn.microsoft.com/zh-cn/library/system.io.file(v=vs.110).aspx using System; using System.IO; ...
- 帝国cms如何调用栏目别名作为分类标题?[!--classname--]标签不能用
用帝国cms建站安全性和生成速度会比dedecms好些,但ecms有个比较不方便的地方就是后台默认模板栏目那边没有一个seo标题设置的输入框,列表模板用的是[!--pagetitle--]标签,那么分 ...
- zju 2972 Hurdles of 110m(简单的dp)
题目 简单的dp,但是我还是参考了网上的思路,具体我没考虑到的地方见代码 #include<stdio.h> #include<iostream> #include<st ...
- 【hadoop2.6.0】倒排索引遇到问题了
想实现书上倒排索引的例子,但是我不会java想用C++写,如果用hadoop streaming 那么输入必须是标准输入的形式, 那么我怎么获得每个文件的文件名呢? 查了一下,还有一种方法叫做hado ...
- POJ 1775
#include <iostream> using namespace std; ,,,,,,,,,}; bool boo; void DFS(int time,int sum); int ...
- Javascript 绝对定位和相对定位
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...