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. QQ 微信 新浪 无法 分享 收集

    1.网络请求报错.升级Xcode 7.0发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Secur ...

  2. HTTP Methods: GET v.s POST

    HTTP works as a request-response protocol between a client and server. A web browser may be the clie ...

  3. CSS3是怎么实现全景特效?

    很有意思的全景动画:(直接上代码) html部分: <div class="panorama"></div> css部分: .panorama{ width ...

  4. document.body.clientHeight 和 document.documentElement.clientHeight的区别

    document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...

  5. css 实现三角形的原理

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. Js navigator.onLine 获取设备是否可以上网、连接网络

    http://zccst.iteye.com/blog/2194229 获取用户的联网状态 if (navigator && navigator.onLine === false) { ...

  7. iPhone的設置——FaceTime頁面

    這裏說的是蘋果的Hand off功能,系統升級後,蘋果的多部設備可以更好的“連續互通”.有電話打進來,iPhone.iPad和Mac都能收到,用戶可以任意選擇一款設備接電 話.同樣,iMessage也 ...

  8. TFTP服务

    $ mkdir /tftpboot$ cp exynos4412-fs4412.dtb uImage /tftpboot$ sudo chmod 777 tftpboot(~下)$ sudo chmo ...

  9. JS开发windows phone8.1系列之3

    http://msdn.microsoft.com/zh-cn/library/windows/apps/dn629638.aspx 这部分主要是使用页面导航 管理方式,在程序的default.htm ...

  10. MySQL创建数据库并赋予权限

    1.创建一个mysql用户并设置密码create user 'MySql用户名'@'localhost' identified by '密码';2.限制账户资源grant usage on *.* t ...