HDU 1811 Rank of Tetris

Posted by m!tCh on Mon, 06 May 2019 09:55:04 +0200

Topic link:

http://acm.hdu.edu.cn/showproblem.php?pid=1811

Title:

Given a digraph of n points, tell you the relationship of m (a,b,c)(a,b,c)(a,b,c). A, C denotes node number, B belongs to'>','<','=', in one of the three types,'>'denotes a rank higher than c, less than C rank higher than a', ='indicates that the rank is the same, but in the set of elements that have a'=' relationship, the rank of smaller is higher. Ask if you c a n determine whether a higher rank is derived from If the rank is high to low, the output of "OK" will be achieved, and if there is no "CONFLICT" from high to low, the output of "UNCERTAIN" will be achieved if there is a rank, but it is not unique.

Analysis:

Topological sorting + searching is to reduce the elements with the'='relationship into a point, and then build a map, not edge-shrinking points, edge-building graph, (otherwise wa to doubt life); build a graph, then record the total number of points of the graph you build cnt, and then topological sorting, record the number of points encountered in the process of topological sorting num, if num is not equal to CNT obviously there are rings; For a problem with only one topological sorting path, suppose that in the process you delete a point a with a degree of zero, and then delete the edges (a,b)(a,c)... Then you find that more than two or more points connected by a are equal to 0. Assuming that B and C are equal, there is obviously no edge connection between B C. Because the entries are both 0, then who ranks higher in B and C will produce two kinds of topological sorting results. So there is only one result in the process of topological sorting. So at any time of topological sorting, in your queue (queue) The element entry in the column is 0) at most one point, not more than one; if neither of the two conditions exists, then it is OK.

Code:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;

#define  inf 0x7f7f7f7f
#define  maxn 100411
#define  N 20500
#define mod 100003

typedef long long ll;
typedef struct{
    int u,v,next,w;
}Edge;
Edge e[N];
int cnt,head[N];

inline void add(int u,int v){
    e[cnt].u=u;
    e[cnt].v=v;
    //e[cnt].w=w;
    // e[cnt].f=f;
    e[cnt].next=head[u];
}
inline void write(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}

inline int read(){
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')
            f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
int n,m,f[N],in[N],a[N],b[N];
char ch[N];
int find(int x){
    return x==f[x]?x:f[x]=find(f[x]);
}
void init(){
    for(int i=0;i<n;i++) {
        f[i] = i;
    }
}
void topsort(){
    queue<int>q;
    int flag=0,num1=0,num2=0;
    for(int i=0;i<n;i++) {
        if (find(i) == i) {
            if (!in[i])
                q.push(i);
            num2++;
        }
    }
    while(!q.empty()) {
        int u = q.front();q.pop();
        num1++;
        if (!q.empty())flag = 1;
        for (int i = head[u]; i!=-1;i = e[i].next){
            int v = e[i].v;
            in[v]--;
            if (!in[v])q.push(v);
        }
    }
    if(num1!=num2)cout<<"CONFLICT"<<endl;
    else if(flag==1)cout<<"UNCERTAIN"<<endl;
    else cout<<"OK"<<endl;
}
int main() {
    while(scanf("%d%d",&n,&m)!=EOF){
        cnt=0;
        init();
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(ch,0,sizeof(ch));
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        for(int i=0;i<m;i++){
            scanf("%d %c %d",&a[i],&ch[i],&b[i]);
            int fx=find(a[i]),fy=find(b[i]);
            if(ch[i]=='='){//Must merge the set before mapping
                f[fx]=fy;
            }
        }
        for(int i=0;i<m;i++){
            int fx=find(a[i]),fy=find(b[i]);
            if(ch[i]=='+')continue;
            else if(ch[i]=='>'){
                add(fx,fy);
                in[fy]++;
            }
            else if(ch[i]=='<'){
                add(fy,fx);
                in[fx]++;
            }
        }
        topsort();
    }
    return 0;
}

We insist on one thing, not because it will be effective, but because we firmly believe that it is right to do so.
—— Javier

Topics: PHP less