标签:
一个无向图是弦图当且仅当有一个完美消除序列.
MCS最大势:http://wenku.baidu.com/view/07f4be196c175f0e7cd13784.html
In a highly modernized fishing village, inhabitants there make a living on fishery. Their major tools, fishing nets, are produced and fixed by computer. After catching fishes each time,
together with plenty of fishes, they will bring back the shabby fishing nets, which might be full of leaks. Then they have to inspect those nets. If there exist large leaks, they have to repair them before launching out again.
Obviously, the smaller the leaks in the fishing nets are, the more fishes they will catch. So after coming back, those fishermen will input the information of the fishing nets into the computer to check whether the nets have leaks.
The checking principle is very simple: The computer regards each fishing net as a simple graph constructed by nodes and edges. In the graph, if any circle whose length (the number of edges) is larger than 3 must has at least one chord, the computer will output
"Perfect" indicating that the fishnet has no leaks. Otherwise, "Imperfect" will be displayed and the computer will try to repair the net.
Note: A circle is a closed loop, which starts from one node, passes through other distinct nodes and back to the starting node. A chord is an edge, which connects two different nodes on the circle, but it does not belong to the set of edges on the circle.
Input
The input file contains several test cases representing different fishing nets. The last test case in the input file is followed by a line containing 0 0.
The first line of each test case contains two integers, n and m, indicating the number of nodes and edges on the net respectively, 1 <= n <= 1000. It is followed by m lines accounting for the details of the edges. Each line consists of two integers xi and yi,
indicating there is an edge between node xi and node yi.
Output
For each test case, display its checking results. The word "Imperfect" suggests that the corresponding fishing net is leaking, while the word "Perfect" stands for a fishing net in good condition.
Follow the output for each net with a blank line.
Sample Input
4 4
1 2
2 3
3 4
4 1
3 3
1 2
2 3
3 1
0 0
Output for the Sample Input
Imperfect
Perfect
/* *********************************************** Author :CKboss Created Time :2015年05月09日 星期六 09时34分05秒 File Name :ZOJ1015.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; const int maxn=1111; int n,m; vector<int> G[maxn]; bool vis[maxn]; bool mp[maxn][maxn]; int lab[maxn]; int seq[maxn]; void MCS() { memset(lab,0,sizeof(lab)); memset(vis,false,sizeof(vis)); for(int i=n;i>=1;i--) { int u=-1; for(int j=1;j<=n;j++) { if(!vis[j]&&(u==-1||lab[j]>lab[u])) u=j; } seq[i]=u; vis[u]=true; for(int j=0,sz=G[u].size();j<sz;j++) { int v=G[u][j]; lab[v]++; } } } bool check_MCS() { for(int i=1;i<=n;i++) { vector<int> vc; for(int j=i+1;j<=n;j++) { if(mp[seq[i]][seq[j]]==true) vc.push_back(seq[j]); } for(int j=1,sz=vc.size();j<sz;j++) { if(mp[vc[0]][vc[j]]==false) return false; } } return true; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(int i=0;i<=n+10;i++) G[i].clear(); memset(mp,false,sizeof(mp)); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); mp[u][v]=mp[v][u]=true; } MCS(); if(check_MCS()==true) puts("Perfect\n"); else puts("Imperfect\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/45599223