The idea of doing a problem:
1 chord chart, watched a weekend have wood there! Too weak, the algorithm perfectly eliminates the sequence according to the maximum potential algorithm (MCS) given by CDQ's ppt. After the Sumbit 19 times, for WA provides a lot of denominator ah .... Write more points for yourself to back up it.
2 Useful information:
3 theorem: A graph is a chord image if and only if it has a perfect elimination sequence. So first get the perfect elimination sequence:
4 How to determine if you are getting the perfect elimination sequence:
Paste code: (v*v complexity ... )
Copy Code code as follows:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace Std;
const int MAXN=1000+10;
int GRA[MAXN][MAXN];
int n, m;
int LABEL[MAXN], TEMP[MAXN], NUM[MAXN];
void Numbervertex ()
{
int I, J;
Label[n]=0, Num[n]=1;
for (i=n; i>=1; i--)
{
int Mm=-1, POS;
For (J=1 j<=n; j + +)
{
if (!num[j] && label[j]>mm)
{
MM=LABEL[J];
Pos=j;
}
}
Num[pos]=i;
For (J=1 j<=n; j + +)
{
if (!num[j] && (gra[pos][j) | | | gra[j][pos])
label[j]++;
}
}
return;
}
int check ()
{
int I, j, Flag=1;
for (I=1; i<=n && flag; i++)
{
memset (temp,0,sizeof (temp));
int len=0;
For (J=1 j<=n; j + +)
{
if (Num[i]<num[j] && gra[I [j])
{
Temp[len++]=j;
}
}
For (J=1 j<len; j + +)//In this WA day there are wood ...
if (num[temp[0]]>num[temp[j])
Swap (temp[0], temp[j]);
For (J=1 j<len; j + +)
if (!gra[temp[0] [temp[j]])
{
flag=0;
Break
}
}
return flag;
}
int main ()
{
while (scanf ("%d%d", &n,&m)!=eof)
{
if (n==0 && m==0)
Break
memset (label,0,sizeof (label));
memset (num,0,sizeof (num));
memset (gra,0,sizeof (GRA));
for (int i=0; i<m; i++)
{
int x, y;
scanf ("%d%d", &x, &y);
Gra[x][y]=gra[y][x]=1;
}
Numbervertex ();
if (check ())
Puts ("perfect\n");
Else
Puts ("imperfect\n");
}
return 0;
}