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. Bootstrap整合ASP.NET MVC验证、jquery.validate.unobtrusive

    没什么好讲的,上代码: (function ($) { var defaultOptions = { validClass: 'has-success', errorClass: 'has-error ...

  2. sencha 安装、学习

     sencha touch 是Extjs 的手机版,Extjs是创建富客户端的AJAX应用中的重量级框架,sencha touch当然就是面向触摸设备的重量级js框架,在做基于桌面的网页时经常用的js ...

  3. Defining custom settings in Odoo

    Unfortunately Odoo documentation doesn’t seem to include any information about adding new configurat ...

  4. MRP运算生成采购单时间的逻辑

    由MRP运算产生的采购单日期,由生产单指定的安排计划日期.公司设置里的采购提前期和隐藏的供应商供货提前期三个字段共同决定. 可以很容易的在系统中找到,供应商供货提前期,需要在产品视图中将字段selle ...

  5. House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. EF 关系规则(一对一、一对多、多对多...)

    转自: http://www.cnblogs.com/dudu/archive/2011/07/11/ef_one-to-one_one-to-many_many-to-many.html Entit ...

  7. 采用Hibernate框架的研发平台如何能够真正兼容Oracle和sqlServer数据库

    都说Hibernate框架的使用可以很容易的让你的研发平台支持多种不同类型的数据库,但实践表明,这里的“容易”,是相对的. 想让研发平台支持多种数据库,并不是一件简单的事,也可以这么说:并不是只要使用 ...

  8. jquery_事件与动画

    事件绑定 bind(type[,data],fn)(无限触发) type:事件类型包括jquery中已有事件也可以自定义事件 data:可选参数,作为event.data属性传递给事件对象的额外数据对 ...

  9. Trie树

    一.什么是trie树 1.Trie树 (特例结构树)   Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串( ...

  10. ios-滚动视图滚动取消键盘

    _scroll.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;_SearchTable.keyboardDismissMode ...