uva 10199 – tourist guide

Contents

Problem

題目網址
中文網址

Solution

Articulation Point 練習題。
請參考:
http://www.csie.ntnu.edu.tw/~u91029/Component.html#1

Code

UVa 10199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
#define N 100
using namespace std;

int visited[N];
int low[N]; //用 back edge 能連到的最高祖先(step 小的)
int step; //currently order of dfs
int n;
bool adj[N][N];
string name[N];
vector<string> ans;
void (int p, int i) //p -> i
{
low[i] = visited[i] = ++step;
int child = 0;
bool ap = false;
for (int v = 0; v < n; ++v) //i -> v
{
if (adj[i][v] && v != p)
{
if (visited[v]) //back edge
low[i] = min(low[i], visited[v]);
else //tree edge
{
++child;
DFS(i, v);
low[i] = min(low[i], low[v]);
if (low[v] >= visited[i]) //i is AP, since it has no back edge.
ap = true;
}
}
}

/*
1.不是根,不是根的才能用 ap = true 判斷
2.根,要由子節點數量判斷是否為 AP
*/
if ((i != p && ap) || (p == i && child > 1))
ans.push_back(name[i]);
}
int main()
{
//freopen("test.out", "w", stdout);
char str1[35], str2[35];
int Case = 0;

while (scanf("%d", &n) && n)
{
getchar();
unordered_map<string, int> M;
for (int i = 0; i < n; ++i)
{
fgets(str1, 35, stdin);
str1[strlen(str1) - 1] = '