整理一下乱七八糟的知识点
#include<cstring> #include<cstdio> using namespace std; const int maxn = 1010; const int inf = 100000000; struct EDGE { int v, next; }edge[maxn * maxn]; bool use[maxn][maxn] , used[maxn]; int n , record[maxn] , cost[maxn] , m , rank[maxn]; int cnt, head[maxn]; void addedge(int u ,int v) { edge[cnt].v = v; edge[cnt].next = head[u]; head[u] = cnt++; } void init() { cnt = 0; memset(head, -1, sizeof(head)); memset(use , false , sizeof(use)); } bool solve() { memset(used , false , sizeof(used)); memset(cost , 0 , sizeof(cost)); int i , u , minimum , x , j; for(i = n; i >= 1; i--) { minimum = 0; u = 0; for(j = 0; j < n; j++) { if(!used[j] && cost[j] > minimum) { minimum = cost[j]; u = j; } } used[u] = true; record[i] = u; rank[u] = i; for(int p = head[u]; p != -1; p = edge[p].next) if(!used[edge[p].v]) cost[edge[p].v]++; } for(i = 1; i <= n; i++) { x = record[i]; minimum = inf; for(int p = head[x]; p != -1; p = edge[p].next) { if(rank[edge[p].v] > i && rank[edge[p].v] < minimum) { u = edge[p].v; minimum = rank[u]; } } if(minimum != inf) { for(int p = head[x]; p != -1; p = edge[p].next) { if(rank[edge[p].v] > i && u != edge[p].v) { if(!use[u][edge[p].v]) return false; } } } } return true; } int main() { int i , a , b; while(scanf("%d%d" , &n , &m) != EOF) { if(n == 0 && m == 0) break; init(); for(i = 0; i < m; i++) { scanf("%d%d" , &a , &b); addedge(a-1, b-1); addedge(b-1, a-1); use[a - 1][b - 1] = use[b - 1][a - 1] = true; } if(solve()) printf("Perfect\n\n"); else printf("Imperfect\n\n"); } }