標籤:
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)
My Code實現用的是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(弦圖判定)