HDU 1811 rank of Tetris "and search set + topology Sort"

Source: Internet
Author: User

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", &AMP;N[I].U,&AMP;N[I].C,&AMP;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&GT;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"

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.