HUD 1811 rank of RIS (Topology Sorting + query set + STL)

Source: Internet
Author: User
Rank of RIS

Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3671 accepted submission (s): 1026


Problem description Since Lele developed the rating system, his Tetris career has become even more powerful, and soon he pushed the game to the world.

To better suit those fans' preferences, Lele came up with a new idea: he will create a global ranking of Tetris experts, which will be updated on a regular basis and the name hall will be better than the Forbes ranking. We don't know how to rank based on rating from high to low. If two people share the same rating, then sort by the RP of these people from high to low.

Finally, Lele is about to take action and rank n people. For convenience, everyone has been numbered, from 0 to N-1, and the larger the number, the higher the RP.
At the same time, Lele obtained some (m) rating information from the paparazzi. There are three possible conditions for such information: "A> B", "A = B", and "A <B", respectively, indicating that the rating of A is higher than B and equal to B, smaller than B.

Now, Lele doesn't ask you to help him create the master list. He just wants to know whether the master list can be determined based on the information. If yes, "OK" is output ". Otherwise, determine the cause of the error, whether it is because the information is incomplete (output "uncertain") or because the information contains conflicts (output "Conflict ").
Note: If the information contains conflicts and the information is incomplete, "Conflict" is output ".


Input this question contains multiple groups of tests, please process until the end of the file.
The first line of each test contains two integers, N, and M (0 <= n <= strong, 0 <= m <= 20000), indicating the number of people to be ranked and the number of relationships obtained.
Next there are m rows, indicating the relationships respectively


Output for each group of tests, output according to the requirements of the question in one row


Sample Input

3 30 > 11 < 20 > 24 41 = 21 > 32 > 00 > 13 31 > 01 > 22 < 1
 


Sample output

OKCONFLICTUNCERTAIN
 

This is a good topic that increases the difficulty because of the equal signs.

The data volume of the question is large, and the relationship cannot be stored using the adjacent matrix. It will exceed the memory. Therefore, we decided to use the collar table and learned the vector in STL for one afternoon. In the beginning, the connection and other conditions were not taken into account, so we didn't use the and query set to merge '= ', instead, read '=' manually to make the inbound degrees of the two points equal (this does not work, not only when 1 = 2 = 3, an error is returned, 1
> 2, 2 = 3, 4> 3, WA will also be introduced. After the equal sign is introduced and queried to process the equal sign, it will time out. After a long period of optimization, we finally stepped on, so sometimes it is necessary to learn. Here are some data that is prone to errors (for reference only ):

4 3

1> 0

1 = 2

3> 2

OK

5 4

1> 0

1 = 2

2 = 3

3 <4

OK

7 6

1> 0

1 = 2

2 = 3

2> 5

3> 6

4> 3

Nucertain

My problem-solving ideas: when reading data, such as data, we encounter '=' and use the query set for processing. (This is not good because of the order of '=, data needs to be repeatedly merged multiple times), and then determined using a simulated method (some tips are not used here ), when the ranking is determined, only one vertex has a 0 inbound degree. If both vertices have a 0 inbound degree, the ranking cannot be determined. If a ring appears in the directed graph, conflict. (When the conditions are insufficient, you cannot stop the judgment directly because an error may occur later)

#include <stdio.h>#include <string.h>#include <vector>using namespace std;#define N 10005#define INT vector<int>::iteratorint n, m, cnt, all, bir[N];int rec[N * 2][2], vis[N], num[N];vector<int> G[N];void handle(int a, int b){if (a == b)return;for (INT i = G[a].begin(); i != G[a].end(); i++)G[b].push_back(*i);num[a] = b;all++;}int find(int x){return x != num[x]?num[x] = find(num[x]):x;}int topo(int k, int sum){vis[k] = 1;int ok = 0;for (INT i = G[k].begin(); i != G[k].end(); i++){int a = find(*i);if (vis[a])return -1;ok = topo(a, sum + 1);if (ok)return ok;}if (sum + all == n)return 1;vis[k] = 0;return 0;}int main(){int a, b;char c;while (scanf("%d%d", &n, &m) != EOF){// Init.all = cnt = 0;memset(rec, 0, sizeof(rec));memset(bir, 0, sizeof(bir));for (int i = 0; i < n; i++)G[num[i] = i].clear();// Read.for (int i = 0; i < m; i++){scanf("%d %c %d", &a, &c, &b);if (c == '<')G[b].push_back(a);else if (c == '>')G[a].push_back(b);else{rec[cnt][0] = a;rec[cnt][1] = b;cnt++;}}// Handle.for (int i = 0; i < cnt; i++)handle(find(rec[i][0]), find(rec[i][1]));// Judge.int ok = 0;for (int i = 0; i < n; i++){a = find(i);if (bir[a])continue;memset(vis, 0, sizeof(vis));ok = topo(a, 1);if (ok)break;bir[a] = 1;}if (ok == 1)printf("OK\n");else if (ok == -1)printf("CONFLICT\n");elseprintf("UNCERTAIN\n");}return 0;}

I studied other people's code and wrote it based on the advantages of my own code. I mainly changed the Data Reading point, that is, when I got '=, merge the two points first and then query the Set (note that the same points have been processed, which affects the future judgment). After all the data is read, process '<' and '>' (locate the root node and operate on the root node ). In addition, the queue is referenced in the implementation of topological sorting for simulation. At first, the number of inbound degrees of each vertex is recorded. When the inbound degree is 0, if the queue size is greater than 1, the sorting is not unique.
When less than N vertices are determined to be sorted, this is a conflict.

#include <stdio.h>#include <string.h>#include <vector>#include <queue>using namespace std;#define N 10005int n, m, cnt;int X[2 * N], Y[2 * N], far[N], son[N];char C[2 * N];vector<int> G[N];int get(int x){return x != far[x]?far[x] = get(far[x]):x;}bool Union(int a, int b){if (a == b)return false;far[a] = b;return true;}int main(){while (scanf("%d%d", &n, &m) != EOF){// Init.memset(son, 0, sizeof(son));for (int i = 0; i < n; ++i)G[far[i] = i].clear();cnt = n;        // Read.for (int i = 0; i < m; i++){scanf("%d %c %d", &X[i], &C[i], &Y[i]);if (C[i] == '=' && Union(get(X[i]), get(Y[i])))cnt--;}// Handle.for (int i = 0; i < m; i++){int a = get(X[i]), b = get(Y[i]);if (C[i] == '<'){G[b].push_back(a);son[a]++;}else if (C[i] == '>'){G[a].push_back(b);son[b]++;}}queue<int> que;int ok = 0;for (int i = 0; i < n; i++)if (!son[i] && i == get(i))que.push(i);while (!que.empty()){if (que.size() > 1)ok = 1;int t = que.front();que.pop();cnt--;for (int i = 0; i < G[t].size(); i++)if (--son[G[t][i]] == 0)que.push(G[t][i]);}if (cnt > 0)printf("CONFLICT\n");else if (ok)printf("UNCERTAIN\n");elseprintf("OK\n");}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.