Rank of Tetris Hang electrically topological sort added disjoint-set

Rating system developed since Lele, his career Tetris is even more powerful, he soon put the game into over the world.

In order to better meet the preferences of those enthusiasts, Lele want a new idea: he would make a global Tetris master list, updated regularly, their places also sound than the Forbes rich list. How about rankings, it goes without saying all know Rating descending according to row, if two people have the same Rating, then press RP descending these people to row. 

Finally, Lele to begin action on the N individual ranking. For convenience, each have been numbered, respectively, from 0 to N-1, and the larger the number, the higher the RP. 
At the same time made some Lele (M a) information regarding Rating from the paparazzi in. This information may have three conditions, namely, "A> B", "A = B", "A <B", respectively Rating A is greater than B, B is equal to, less than B. 

Now Lele does not allow you to help him make the master list, he just wanted to know, depending on whether the information can determine this master list, then it is output "OK". Otherwise, you determine the cause of the error, in the end because of incomplete information (output "UNCERTAIN"), or because the information contained in these conflicts (output "CONFLICT"). 
Note that if the message contains both conflict and incomplete information, it outputs "CONFLICT". 

Input this title has multiple sets of test, to deal with the end of the file. 
Each test first line contains two integers N, M (0 <= N <= 10000,0 <= M <= 20000), each represent the number of relationships to be obtained and ranked. 
Then there are M rows, each represent these relationships 
Output For each test, on a line by topic in claim Sample Input Output

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

Sample Output

The OK 
The CONFLICT 
the UNCERTAIN 
ideas: first with disjoint-set to equal elements are connected together, as a processing element, and then the root node in the ordering of each element of the topology, the number of the sorting process if a queue element is not equal to a , indicating insufficient condition, if not successful shoot, indicating conflict
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int N=1E5+7;

int arr1[N];
int arr2[N];
char arr3[N];
int pre[N];
vector<int >ve[N];
int in[N];


int find(int x){
    if(x==pre[x]) return x;
    return pre[x]=find(pre[x]);
}


int main(){
    int n,m;
    while(cin>>n>>m){ 
        memset(in,0,sizeof(in));
        for(int i=0;i<N;i++){
            pre[i]=i;
            ve[i].clear();
        }
        queue<int >que;
        memset(arr1,0,sizeof(arr1));
        memset(arr2,0,sizeof(arr2));
        int n1=n;
        for(int i=1;i<=m;i++){
            scanf("%d %c %d",&arr1[i],&arr3[i],&arr2[i]);
            if(arr3[i]=='='){
                int fx=find(arr1[i]);
                int fy=find(arr2[i]);                
                if(fx!=fy){
                    pre[fx]=fy;
                    n1--;
                }
            }
        }
            
        
        for(int i=1;i<=m;i++){
            if(arr3[i]=='=') continue;
            
            int fx=find(arr1[i]);
            int fy=find(arr2[i]);
            
//            if(fx==fy){
//                puts("CONFLICT");
//                continue ;
//            }
             if(arr3[i]=='>'){
                ve[fx].push_back(fy);
                in[fy]++;
            }
            
            else {
                ve[fy].push_back(fx);
                in[fx]++;
            }
        }
        
        for(int i=0;i<n;i++){
            if(in[i]==0 && find(i)==i){
                que.push(i);
            }
        }
        int sum=0;
        bool uncertain=false;
        while(que.size()){
            if(que.size()>1) uncertain=true;                        
            int xx=que.front();
            que.pop();
            sum++;
            for(int i=0;i<ve[xx].size();i++){
                in[ve[xx][i]]--;
                if(in[ve[xx][i]]==0){
                    que.push(ve[xx][i]);
                }
            }
        }
        if(sum!=n1) puts("CONFLICT");
        else if(uncertain) puts("UNCERTAIN");
        else puts("OK");
    }
    return 0;
}

 




Guess you like

Origin www.cnblogs.com/Accepting/p/11318438.html