Fishing Net(ZOJ-1015)(LexBFS实现)

Problem Description

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

Sample Output

Imperfect

Perfect

题意:多组数据,每组数据给出 n 个点 m 条边,如果图是弦图的话,输出 Perfect,如果图不是弦图的话,输出 ImPerfect

思路:弦图判定模版题,套用 LexBFS 模版即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

struct Edge{
    int to;
    Edge(){}
    Edge(int to):to(to){}
};
struct Node{
    int id,num;
    Node(){}
    Node(int id,int num):id(id),num(num){}
    friend bool operator<(Node a,Node b) {
        return a.num<b.num;
    }
};
int n,m;
int G[N][N];
vector<Edge>edge[N];
int link[N],tot;//标号序列
int order[N];//完美消除序列
int num[N];//点编号
int bucket[N];//更新一个点的标号序列时的桶
bool vis[N];

void bfs(int x){//维护标号序列
    memset(num,0,sizeof(num));
    memset(link,0,sizeof(link));
    memset(vis,0,sizeof(vis));
    tot=n;

    priority_queue<Node> Q;
    Q.push(Node(x,1));
    while(!Q.empty()) {
        Node now=Q.top();
        Q.pop();
        if(!vis[now.id]){
            vis[now.id]=true;
            link[tot]=now.id;
            order[now.id]=tot;
            tot--;
            if(tot==0)
                break;
        }
        for(int i=0;i<edge[now.id].size();i++) {
            int to=edge[now.id][i].to;
            num[to]++;
            if(!vis[to])
               Q.push(Node(to,num[to]));
        }
    }
}
bool check() {
    bfs(1);//维护标号序列

    for(int i=1;i<=n;i++){
        int minn=n+1;
        int tail=0;//桶的指针
        int value;
        for(int j=0;j<edge[link[i]].size();j++) {
            int to=edge[link[i]][j].to;
            if(order[to]>i) {
                if(minn>order[to]) {
                    minn=order[to];
                    value=to;
                }
                bucket[tail++]=to;//入桶
            }
        }
        for(int j=0;j<tail;j++) {
            if(bucket[j]!=value&&G[value][bucket[j]]==0)
                return false;
        }
    }
    return true;
}
int main() {
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
        for(int i=1;i<=n;i++)
            edge[i].clear();
        memset(G,0,sizeof(G));
        for(int i=0;i<m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            edge[x].push_back(y);
            edge[y].push_back(x);
            G[x][y]=1;
            G[y][x]=1;
        }
        if(check())
            printf("Perfect\n\n");
        else
            printf("Imperfect\n\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值