码迷,mamicode.com
首页 > Web开发 > 详细

ZOJ 1015 Fishing Net(弦图判定)

时间:2015-02-05 20:16:52      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:

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):

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int MAXV = 1010;
 5 
 6 bool mat[MAXV][MAXV], vis[MAXV];
 7 int label[MAXV], num[MAXV];
 8 int n, m;
 9 
10 void MaximumCardinalitySearch() {
11     memset(vis + 1, 0, n * sizeof(bool));
12     memset(label + 1, 0, n * sizeof(int));
13     for(int i = n; i > 0; --i) {
14         int u = -1;
15         for(int v = 1; v <= n; ++v) if(!vis[v])
16             if(u == -1 || label[u] < label[v]) u = v;
17         vis[u] = true;
18         num[i] = u;
19         for(int v = 1; v <= n; ++v) if(!vis[v] && mat[u][v])
20             label[v]++;
21     }
22 }
23 
24 bool isPrefect() {
25     for(int u = 1; u <= n; ++u) {
26         int t = u + 1;
27         while(t <= n && !mat[num[u]][num[t]]) ++t;
28         if(t > n) continue;
29         for(int v = t + 1; v <= n; ++v) if(mat[num[u]][num[v]])
30             if(!mat[num[t]][num[v]]) return false;
31     }
32     return true;
33 }
34 
35 int main() {
36     while(scanf("%d%d", &n, &m) != EOF) {
37         if(n == 0 && m == 0) break;
38         memset(mat, 0, sizeof(mat));
39         for(int i = 0, u, v; i < m; ++i) {
40             scanf("%d%d", &u, &v);
41             mat[u][v] = mat[v][u] = true;
42         }
43         MaximumCardinalitySearch();
44         puts(isPrefect() ? "Perfect" : "Imperfect");
45         puts("");
46     }
47 }
View Code

 

ZOJ 1015 Fishing Net(弦图判定)

标签:

原文地址:http://www.cnblogs.com/oyking/p/4275726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!