[HDU] [1811] Rank of Tetris

Problem Description

Loading...

Problem Analysis

For this problem, Topological Sort only needs to determine whether sorting is possible and whether the ordering is unique.

The difference from the standard case is that equal elements exist here.

Since equal elements are ordered among themselves by index, for other elements these equal elements are identical and can be treated as a single unit, so consider using Union-Find (Disjoint Set Union).

When checking, only elements with i==find(i) need to be considered. To determine uniqueness, a queue can be used: if the queue contains only one element at all times, the ordering is unique. If after sorting some elements remain unsorted (num>0), the relations contain a cycle, so sorting is impossible.

An Adjacency List is still used to store edge and vertex information.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,headcnt;
int cnt[10200];
int head[10200];
int par[10200];
struct node {
	int next;
	int to;
} edge[20200];
struct cmpop {
	int x;
	char op;
	int y;
} a[20200];
void add(int u,int v) {
	edge[headcnt].to=v;
	edge[headcnt].next=head[u];
	head[u]=headcnt++;
	cnt[v]++;
}
int find(int m) {
	if(par[m]==m)
		return m;
	else
		return par[m]=find(par[m]);
}
bool unite(int x,int y) {
	x=find(x);
	y=find(y);
	if(x==y)
		return false;
	par[y]=x;
	return true;
}
int main() {
	int m;
	while(scanf("%d %d",&n,&m)!=EOF) {
		memset(head,-1,sizeof(head));
		memset(cnt,0,sizeof(cnt));
		headcnt=0;
		for(int i=0; i<n; i++)
			par[i]=i;
		int num=n;
		for(int i=0; i<m; i++) {
			scanf("%d %c %d",&a[i].x,&a[i].op,&a[i].y);
			if(a[i].op=='=')
				if(unite(a[i].x,a[i].y))
					num--;
		}
		for(int i=0; i<m; i++) {
			if(a[i].op!='=') {
				int x=find(a[i].x),y=find(a[i].y);
				if(a[i].op=='>')
					add(x,y);
				else
					add(y,x);
			}
		}
		queue<int>q;
		for(int i=0; i<n; i++) {
			if(!cnt[i]&&i==find(i)) {
				q.push(i);
			}
		}
		bool flag=false;
		while(!q.empty()) {
			if(q.size()>1)
				flag=true;
			int top=q.front();
			q.pop();
			num--;
			for(int i=head[top]; i!=-1; i=edge[i].next) {
				cnt[edge[i].to]--;
				if(!cnt[edge[i].to])
					q.push(edge[i].to);
			}
		}
		if(num>0)
			printf("CONFLICT\n");
		else if(flag)
			printf("UNCERTAIN\n");
		else
			printf("OK\n");
	}
	return 0;
}

Loading...