ZOJ 1015 Fishing Net Chord Graph MCs

Source: Internet
Author: User
Tags nets


An all-in-one graph is a chord chart when and only if there is a perfect elimination sequence.

MCS Maximum potential: http://wenku.baidu.com/view/07f4be196c175f0e7cd13784.html



Fishing Net Time limit: Seconds Memory Limit: 32768 KB

In a highly modernized fishing village, inhabitants there make a living on fishery. Their major tools, fishing nets, is produced and fixed by computer. After catching fishes all time, together with plenty of fishes, they would bring back the shabby fishing nets, which might be full of leaks. Then they has to inspect those nets. If there exist large leaks, they has to repair them before launching out again.

Obviously, the smaller the leaks in the fishing nets is, the more fishes they would catch. So after coming back, those fishermen would input the information of the fishing nets into the computer to check whether th E nets has leaks.

The checking principle is very simple:the computer regards each fishing net as a simple graph constructed by nodes and Ed Ges. In the graph, if any circle whose length (the number of edges) is larger than 3 must have at least one chord, the computer Would output "Perfect" indicating that the fishnet have no leaks. Otherwise, "Imperfect" would be displayed and the computer would 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 and different nodes on the circle, but it does isn't belong to the set of edges on the CI Rcle.


Input

The input file contains several test cases representing different fishing nets. The last test case in the input file was followed by a line containing 0 0.

The first line of all test case contains-integers, n and m, indicating the number of nodes and edges on the net Respe ctively, 1 <= n <= 1000. It is followed by M lines accounting for the details of the edges. Each of the line consists of integers xi and Yi, indicating there are an edge between node XI and Node Yi.


Output

For the test case, the display its checking results. The word "imperfect" suggests that the corresponding fishing net are 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

Source:Asia 2001, Shanghai (Mainland China)



/* ***********************************************author:ckbosscreated time:2015 May 09 Saturday 09:34 05 seconds 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, &AMP;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;}



ZOJ 1015 Fishing Net Chord Graph MCs

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.