Algorithm: first re-number the graph, and then check according to the new number
[Cpp]
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int n, m;
Bool g [1024] [1024], used [1024];
Int lable [1024], set [1024]; // The new number of the lable storage graph and the number calculated by the set storage string graph.
Void Relable (){
Memset (used, false, sizeof (used ));
Used [1] = true;
For (int num = n-1; num> 0; num --){
Memset (lable, 0, sizeof (lable ));
For (int I = 1; I <= n; I ++ ){
If (! Used [I]) {
For (int j = 1; j <= n-num; j ++ ){
If (g [I] [set [n-j + 1])
Lable [I] ++;
}
}
}
Int maxv = 0, max;
For (int I = 1; I <= n; I ++ ){
If (lable [I]> maxv ){
Maxv = lable [I];
Max = I;
}
}
Set [num] = max;
Used [max] = true;
}
}
Bool check (){
Int temp [1024];
For (int I = 1; I <= n; I ++ ){
Memset (temp, 0, sizeof (temp ));
Int t = 0;
For (int j = I + 1; j <= n; j ++ ){
If (g [set [I] [set [j]) {
T ++;
Temp [t] = set [j];
}
}
For (int j = 2; j <= t; j ++ ){
If (! G [temp [j] [temp [1])
Return false;
}
}
Return true;
}
Int main (){
While (scanf ("% d", & n, & m) = 2 ){
If (n = 0 & m = 0) break;
Memset (g, false, sizeof (g ));
Int a, B;
For (int I = 0; I <m; I ++ ){
Scanf ("% d", & a, & B );
G [a] [B] = g [B] [a] = true;
}
Set [n] = 1;
Relable ();
If (check () printf ("Perfect \ n ");
Else printf ("Imperfect \ n ");
Printf ("\ n ");
}
Return 0;
}