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("");
}
}