Hud 1811 Rank of RIS (Topology Sorting + query set)

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. 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 topic contains multiple groups of tests. Please process it 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 question requirements in one row
 

Sample Input
3 3
0> 1
1 <2
0> 2
4
1 = 2
1> 3
2> 0
0> 1
3 3
1> 0
1> 2
2 <1

Sample Output
OK
CONFLICT
UNCERTAIN


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

Because the question data volume is large, it is certainly not allowed to store the relationship using the adjacent matrix and return to the super memory. Therefore, we choose to use the collar table to represent the problem, then I spent one afternoon Learning the vector in STL.

 


In the beginning, I did not consider the connection and so on, therefore, if you do not use the merge & query set to merge '=', you just need to manually set it to '=' to make the two points equally accessible (this does not work, not only is an error returned when 1 = 2 = 3, but when 1> 2, 2 = 3, 4> 3, WA will also be returned.) after it is introduced and checked, it will time out, after a long period of optimization, I finally stepped on it. 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.

 

#include <stdio.h>   #include <string.h>   #include <vector>   using namespace std;    #define N 10005   #define INT vector<int>::iterator     int 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");          else              printf("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 for simulation in the implementation of topological sorting. The number of inbound vertices is recorded at first, and the number of inbound vertices is recorded when the inbound degree is 0, if the queue size is greater than 1, the sorting is not unique. If the number of vertices to be sorted is less than n when the number of vertices to be sorted is zero, the sorting 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;}

 

Related Article

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.