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.

题目大意:给一个n个点的无向图,判断是否弦图。

思路:首先可以参考陈丹琦的《弦图与区间图》,反正我看这个是没看懂。

还可以看《Graph-theoretic algorithms》http://pan.baidu.com/s/1eQnJpfW(一部分中文翻译:http://wenku.baidu.com/view/bf0faa21af45b307e871976d.html)

我的代码实现用的是Maximum Cardinality Search(最大势算法),不过貌似没有见到证明……不过看上去跟Lexicographic BFS(字典序广度优先搜索)差不多,上面有证明,大概是拓展?

考虑到不知道边数和重边带来的影响,这里选择使用矩阵表示图。

Notes:

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

②MCS算法可以导出一幅图的消除序列,它是完美消除序列当且仅当图是弦图。

代码(330MS):

 #include <bits/stdc++.h>
using namespace std; const int MAXV = ; bool mat[MAXV][MAXV], vis[MAXV];
int label[MAXV], num[MAXV];
int n, m; void MaximumCardinalitySearch() {
memset(vis + , , n * sizeof(bool));
memset(label + , , n * sizeof(int));
for(int i = n; i > ; --i) {
int u = -;
for(int v = ; v <= n; ++v) if(!vis[v])
if(u == - || label[u] < label[v]) u = v;
vis[u] = true;
num[i] = u;
for(int v = ; v <= n; ++v) if(!vis[v] && mat[u][v])
label[v]++;
}
} bool isPrefect() {
for(int u = ; u <= n; ++u) {
int t = u + ;
while(t <= n && !mat[num[u]][num[t]]) ++t;
if(t > n) continue;
for(int v = t + ; v <= n; ++v) if(mat[num[u]][num[v]])
if(!mat[num[t]][num[v]]) return false;
}
return true;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
memset(mat, , sizeof(mat));
for(int i = , u, v; i < m; ++i) {
scanf("%d%d", &u, &v);
mat[u][v] = mat[v][u] = true;
}
MaximumCardinalitySearch();
puts(isPrefect() ? "Perfect" : "Imperfect");
puts("");
}
}

ZOJ 1015 Fishing Net(弦图判定)的更多相关文章

  1. bzoj 1242: Zju1015 Fishing Net 弦图判定

    1242: Zju1015 Fishing Net弦图判定 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 214  Solved: 81[Submit ...

  2. [bzoj1242] Zju1015 Fishing Net弦图判定

    弦图判定..MCS算法. 先选一个点,然后每次拿 相邻已选点最多 的未选点. 选完之后判断一下是否是完美消除序列. #include<cstdio> #include<iostrea ...

  3. ●BZOJ 1006 [HNOI2008]神奇的国度(弦图最小染色数)○ZOJ 1015 Fishing Net

    ●赘述题目 给出一张弦图,求其最小染色数. ●题解 网上的唯一“文献”:<弦图与区间图>(cdq),可以学习学习.(有的看不懂) 摘录几个解决改题所需的知识点: ●子图和诱导子图(一定要弄 ...

  4. ZOJ 1015 Fishing Net(判断弦图)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=15 题意:给定一个图.判断是不是弦图? 思路:(1)神马是弦图?对于一 ...

  5. ZOJ 1015 弦图判定

    一些定义: 弦图是一种特殊图:它的所有极小环都只有3个顶点. 单纯点:该顶点与其邻接点在原图中的导出子图是一个完全图. 图G的完美消去序列:一个顶点序列a1a2a3...an,使得对于每个元素ai,a ...

  6. bzoj 1242 弦图判定 MCS

    题目大意: 给定一张无向图,判断是不是弦图. 题解: 今天刚学了<弦图与区间图> 本来写了一个60行+的学习笔记 结果因为忘了保存重启电脑后被还原了... 那就算了吧. MCS最大势算法, ...

  7. bzoj1242(弦图判定)

    cdqppt地址:https://wenku.baidu.com/view/a2bf4ad9ad51f01dc281f1df.html: 代码实现参考的http://blog.csdn.net/u01 ...

  8. 【ZOJ】1015 Fishing Net

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1015 题意:给出一个n个点的无向图,询问是否为弦图,弦图定义为对于图中任意 ...

  9. 弦图的判定MCS算法(zoj1015)

    题意:裸的弦图的判定: 弦图定义:给出一个无向连通图,如果每个环中都存在至少一条弦(环中存在不相邻的两点直接相连)这样的图叫做弦图: 转载:http://blog.csdn.net/crux_d/ar ...

随机推荐

  1. Maven_非法字符: '\ufeff' 解决方案

    Idea在maven打包时报非法字符: '\ufeff' ,但打开报错的类看没有问题,后来发现是隐蔽字符BOM的问题,解决办法是用Notepad++打开这个类,然后改变编码格式为UTF-8  无DOM ...

  2. C#后台如何获取客户端访问系统型号

    ASP.NET获取客户端.服务器端基础信息 . 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名:P ...

  3. Oracle中"行转列"的实现方式

    在报表的开发当中,难免会遇到行转列的问题. 以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用"行转列": scott的emp的原始数据为: ...

  4. Windows Phone Data Protection

    To encrypt the PIN // Convert the PIN to a byte[]. byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Tex ...

  5. jdbc模拟电话本。

    1 项目描述 该项目是用于日常生活中记录联系人信息的一款小工具. 实现了对联系人的姓名.年龄.性别.电话号码.住址的添加及修改.查找.删除.排序等功能.该项目是以windows控制台为运行平台,所有的 ...

  6. Window.document对象 轮播练习

    Window.document对象 一.找到元素:     docunment.getElementById("id"):根据id找,最多找一个:     var a =docun ...

  7. python中的进程、线程(threading、multiprocessing、Queue、subprocess)

    Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...

  8. javascript 变量,作用域,内存管理小结

    js的变量保存两种类型的数据——基本数据类型与引用类型.具有以下几点特征:   变量: 1)基本类型值在内存中占固定大小的空间,因此被保存在栈内存中; 2) 把保存基本类型值得变量赋给另一个变量,会创 ...

  9. Java代理模式

    java代理模式及动态代理类 1.      代理模式 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目 ...

  10. 一封给JVM懵懂者的情书【不看错过一生幸福】

    别说你懂我 你只是在意Java你把我留在家里身和心却始终在她那里难道我只是她的附属品?错,我是我,我是JVM,没有我就没有他! 如果你想懂我或者不管你是否懂我我都在这里等你---[深入JVM内核—原理 ...