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 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(弦图判定)的更多相关文章
- bzoj 1242: Zju1015 Fishing Net 弦图判定
1242: Zju1015 Fishing Net弦图判定 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 214 Solved: 81[Submit ...
- [bzoj1242] Zju1015 Fishing Net弦图判定
弦图判定..MCS算法. 先选一个点,然后每次拿 相邻已选点最多 的未选点. 选完之后判断一下是否是完美消除序列. #include<cstdio> #include<iostrea ...
- ●BZOJ 1006 [HNOI2008]神奇的国度(弦图最小染色数)○ZOJ 1015 Fishing Net
●赘述题目 给出一张弦图,求其最小染色数. ●题解 网上的唯一“文献”:<弦图与区间图>(cdq),可以学习学习.(有的看不懂) 摘录几个解决改题所需的知识点: ●子图和诱导子图(一定要弄 ...
- ZOJ 1015 Fishing Net(判断弦图)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=15 题意:给定一个图.判断是不是弦图? 思路:(1)神马是弦图?对于一 ...
- ZOJ 1015 弦图判定
一些定义: 弦图是一种特殊图:它的所有极小环都只有3个顶点. 单纯点:该顶点与其邻接点在原图中的导出子图是一个完全图. 图G的完美消去序列:一个顶点序列a1a2a3...an,使得对于每个元素ai,a ...
- bzoj 1242 弦图判定 MCS
题目大意: 给定一张无向图,判断是不是弦图. 题解: 今天刚学了<弦图与区间图> 本来写了一个60行+的学习笔记 结果因为忘了保存重启电脑后被还原了... 那就算了吧. MCS最大势算法, ...
- bzoj1242(弦图判定)
cdqppt地址:https://wenku.baidu.com/view/a2bf4ad9ad51f01dc281f1df.html: 代码实现参考的http://blog.csdn.net/u01 ...
- 【ZOJ】1015 Fishing Net
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1015 题意:给出一个n个点的无向图,询问是否为弦图,弦图定义为对于图中任意 ...
- 弦图的判定MCS算法(zoj1015)
题意:裸的弦图的判定: 弦图定义:给出一个无向连通图,如果每个环中都存在至少一条弦(环中存在不相邻的两点直接相连)这样的图叫做弦图: 转载:http://blog.csdn.net/crux_d/ar ...
随机推荐
- 获取jQuery对象的第N个DOM元素 && table常用css样式
获取jQuery对象的第N个DOM元素 1.$(selector).get(N-1) 2.$(selector)[N-1] 注意:.index()方法返回的是一个数,相当于C#中的IndexOf() ...
- 构建json数据post到接口的若干条规则
接受数据接口: public ActionResult PostDownloadLog(PostDownloadLog postDownloadLogs) PostDownLoadLogL类 publ ...
- 内存和flash存储的区别
http://bbs.21ic.com/icview-202550-1-1.html 一.内存 我们一般说的内存指的是DRAM,其主要特点是断电会丢失数据,可读写 二.ROM 断电不会丢失数据,数据只 ...
- java分享第三天(异常)
异常的处理办法之一 捕获异常(try,catch,finally) 1 try语句指定了一段代码,该段代码就是一次捕获并处理的范围.在执行过程中,当任意一条语句产生异常时,就会跳过该段中后面的代码.代 ...
- HTTP头部解析
当我们打开一个网页时,浏览器要向网站服务器发送一个HTTP请求头,然后网站服务器根据HTTP请求头的内容生成当次请求的内容发送给浏览器.你明白HTTP请求头的具体含意吗?下面一条条的为你详细解读,先看 ...
- iOS - JSON 数据解析
iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 解决方法
Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires ...
- cmd 更改字体
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont,在右面找到936值双击打开,数 ...
- *HDU3496 背包DP
Watch The Movie Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HTML 父元素与子元素之间的margin-top问题
问题: 父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. 代码如下: <div ...