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

 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
原文地址:https://www.cnblogs.com/oyking/p/4275726.html