Try Again

Fishing Net

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

判断是否是弦图。

检查一个图是否是弦图:

设已编号的节点集合为A,未编号的节点集合为B
开始时A为空,B包含所有节点。
for num=n-1 downto 0 do
{
  在B中找节点x,使与x相邻的在A集合中的节点数最多,将x编号为num,
  并从B移入A
}
第二步:检查
for num=0 to n-1 do
{
  对编号为num的节点x,设所有编号大于num且与x相邻的节点集合为C,
  在集合C中找出编号最小的节点y,如果集合C中存在不等于y的节点z,
  且y与z间没有边,则此图不是弦图,退出。
}

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
vector<int>a[1006],pb;
int d[1006],dis[1006][1006],q[1006],id[1006],n,m,u,v;
bool vis[1006];
void init()
{
    for(int i=1;i<=n;i++)
        a[i].clear();
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(d,0,sizeof(d));
}
bool mcs_check()
{
    for(int i=1;i<=n;i++)
    {
        pb.clear();
        for(int j=i+1;j<=n;j++)
        {
            if(dis[q[i]][q[j]]) pb.push_back(q[j]);
        }
        for(int j=1;j<pb.size();j++)
            if(!dis[pb[0]][pb[j]]) return false;
    }
    return true;
}
int main()
{
    while(scanf("%d%d",&n,&m) && n+m)
    {
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            dis[u][v]=dis[v][u]=1;
            a[u].push_back(v);
            a[v].push_back(u);
        }
        d[0]=-1;
        for(int i=n;i;i--)
        {
            int x=0;
            for(int j=1;j<=n;j++)
                if(!vis[j] && d[j]>d[x]) x=j;
            q[i]=x;vis[x]=1;id[x]=i;
            for(int j=0;j<a[x].size();j++)
                d[a[x][j]]++;
        }
        printf("%s\n\n",mcs_check()?"Perfect":"Imperfect");
    }
    return 0;
}

 

posted @ 2018-01-12 16:42  十年换你一句好久不见  阅读(499)  评论(0编辑  收藏  举报