ZOJ 1015 Fishing Net 弦图MCS

一个无向图是弦图当且仅当有一个完美消除序列.

MCS最大势:http://wenku.baidu.com/view/07f4be196c175f0e7cd13784.html

Fishing Net


Time Limit: 10 Seconds      Memory Limit: 32768 KB



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


Source: Asia 2001, Shanghai (Mainland China)

/* ***********************************************
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;
}
时间: 2025-01-07 00:49:20

ZOJ 1015 Fishing Net 弦图MCS的相关文章

ZOJ 1015 Fishing Net(判断弦图)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=15 题意:给定一个图.判断是不是弦图? 思路:(1)神马是弦图?对于一个无向图,若该图的任意一个长度大于3的环中存在一条边连接这个环上不相邻的两点,则此图称作弦图. (2)什么是团?团是原图的一个子图,子图就是包含了原图的某些点,那么就要包含这些点之间的边.并且团不是一般的子图而是一个完全子图,就是这个子图的任意两个顶点之间都有边.下面的ABCD就是原图的一个团. (

bzoj 1242: Zju1015 Fishing Net 弦图判定

1242: Zju1015 Fishing Net弦图判定 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 214  Solved: 81[Submit][Status][Discuss] Description 在 一个高度信息化的渔村,鱼网的制作和修补都是由电脑完成.众所周知,鱼网是由网组成的(废话),网组成的东西叫网眼.如果网眼够小,就能捕到很多鱼:如果 网眼太大,鱼就会全部漏走.每次捕鱼回来,鱼网都会烂得很厉害,小网眼会变成网眼,那鱼网就需

[ZOJ 1015]Fishing Net(MCS弦图的判定)

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

ZOJ 1015 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

ZOJ 1015 弦图判定

一些定义: 弦图是一种特殊图:它的所有极小环都只有3个顶点. 单纯点:该顶点与其邻接点在原图中的导出子图是一个完全图. 图G的完美消去序列:一个顶点序列a1a2a3...an,使得对于每个元素ai,ai在ai.ai+1.ai+2...an的导出子图中是一个单纯点. 弦图有一个性质:任何一个弦图都至少存在一个单纯点(该点和其邻接点组成一个完全图) 弦图另一个性质:一个图是弦图当且仅当其存在完美消去序列.(归纳证明) 最大势算法(msc):若原图是弦图,则该算法计算出的序列是完美消去序列. 算法大致

【ZOJ】1015 Fishing Net

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1015 题意:给出一个n个点的无向图,询问是否为弦图,弦图定义为对于图中任意长度>3的环一定存在环上不相邻的点有边相连(n<=1000) #include <bits/stdc++.h> using namespace std; const int N=1005; int n, m, ihead[N], cnt, tag[N], pos[N]; bool vi

BZOJ 1006 HNOI2008 奇妙的国度 弦图最小染色 MCS算法

题目大意:给定一个弦图,求最小染色 弦图相关问题,详细见陈丹琦09年讲稿<弦图与区间图> PPT里有一个问题没说清楚 就是MCS算法的O(m+n)怎么来的 那个在 http://tieba.baidu.com/p/2891159900 有jcvb神犇具体的解答 至于染色怎样标号,时间戳标记暴力硬扫就可以 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

BZOJ 1006 HNOI2008 神奇的国度 弦图最小染色 MCS算法

题目大意:给定一个弦图,求最小染色 弦图相关问题,具体见陈丹琦09年讲稿<弦图与区间图> PPT里有一个问题没说清楚 就是MCS算法的O(m+n)怎么来的 那个在 http://tieba.baidu.com/p/2891159900 有jcvb神犇详细的解答 至于染色如何标号,时间戳标记暴力硬扫即可 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

弦图 完美消除序列 MCS算法

对于普通图的两个性质: 最大团数 ≤ 最小色数 最大独立集 ≤ 最小团覆盖 而在弦图就变成了: 最大团数=最小色数 最大独立集=最小团覆盖 (虽然不知道有什么用 完美消除序列: 对与序列中的点vi,排在vi后面并且和vi相连的点是一个团 一个图存在完美消除序列是它是弦图的充要条件 那么完美消除序列有什么用呢?用处可大啦 求弦图的最大团数/最小色数的时候,只要在完美消除序列上从后往前贪心染色即可. 而求最大独立集/最小团覆盖的时候,只要在完美消除序列上从前往后贪心取点即可. 那么就来了一系列的问题