Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1811
Analysis: It is clear that the problem of topological sorting, one point is to deal with the issue of equal strength;
You can use and check sets to make a set of equal strength.
Describe the nature of the topological ordering:
1. If a point with a degree of 0 is greater than 1, the sort is not unique
2, if the total number of orders is less than the given number, there is a loop
Offer code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace Std;
const int MAXH=10000+10;
const int MAXE=20000+10;
typedef struct EDGE
{
int to,next;
};
typedef struct NODE
{
int u,v;
Char c[2];
};
Edge E[maxe];
Node N[maxe];
int head[maxh],fa[maxh],in[maxh],n,m,sum,cnt;
void init (int num)
{
for (int i=0;i<=num;i++)
{
Head[i]=-1;
Fa[i]=i;
in[i]=0;
}
cnt=0;
Sum=num;
}
void Add (int a,int b)
{
E[cnt].to=b;
E[cnt].next=head[a];
head[a]=cnt++;
}
int find (int x)
{
if (X!=fa[x])
Fa[x]=find (Fa[x]);
return fa[x];
}
void Union (int x,int y)
{
int A=find (x);
int B=find (y);
if (a!=b)
{
Fa[a]=b;
}
}
int main ()
{
BOOL Flag1,flag2;
int A, B;
while (~SCANF ("%d%d", &n,&m))
{
Init (n);
Flag1=false,flag2=false;
for (int i=0;i<m;i++)
{
scanf ("%d%s%d", &N[I].U,&N[I].C,&N[I].V);
cin>>n[i].u>>n[i].c>>n[i].v;
if (n[i].c[0]== ' = ')
{
Union (N[I].U,N[I].V);
sum--;
}
}
for (int i=0;i<m;i++)
{
if (n[i].c[0]== ' = ') continue;
A=find (N[I].U);
B=find (N[I].V);
if (a==b)
{
Flag1=true;
}
if (n[i].c[0]== ' > ')
{
Add (A, b);
in[b]++;
}
Else
{
Add (B,a);
in[a]++;
}
}
queue<int>q;
for (int i=0;i<n;i++)
{
A=find (i);
if (!in[a]&&i==fa[a])
{
Q.push (a);
}
}
while (! Q.empty ())
{
A=q.front ();
Q.pop ();
if (! Q.empty ()) flag2=true;
sum--;
for (int i=head[a];i+1;i=e[i].next)
{
in[e[i].to]--;
if (!in[e[i].to])
Q.push (e[i].to);
}
}
printf ("sum=%d\n", sum);
printf ("flag1=%d,flag2=%d,sum=%d\n", flag1,flag2,sum);
if (flag1| | SUM>0)
{
printf ("conflict\n");
}
else if (FLAG2)
{
printf ("uncertain\n");
}
Else
{
printf ("ok\n");
}
}
return 0;
}
HDU 1811 rank of Tetris "and search set + topology Sort"