forked from ksaveljev/UVa-online-judge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10000.cpp
65 lines (52 loc) · 1.24 KB
/
10000.cpp
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
#include <iostream>
#include <vector>
using namespace std;
typedef struct _node {
int n;
int length;
vector<int> adj;
} node;
vector<node> G;
pair<int,int> result;
void dfs(int start, int length) {
if (G[start-1].length > length)
return;
else if (G[start-1].length < length)
G[start-1].length = length;
if (G[start-1].adj.size() == 0) {
if (length > result.first) {
result.first = length;
result.second = start;
} else if (length == result.first) {
if (start < result.second) {
result.second = start;
}
}
} else {
for (int i = 0; i < G[start-1].adj.size(); i++) {
dfs(G[start-1].adj[i], length + 1);
}
}
}
int main (void) {
int n, start, a, b, counter = 0;
while (cin >> n && n) {
G.clear();
counter++;
for (int i = 1; i <= n; i++) {
node N;
N.n = i;
N.length = 0;
G.push_back(N);
}
cin >> start;
while (cin >> a >> b && a && b) {
G[a-1].adj.push_back(b);
}
result.first = 0;
result.second = 0;
dfs(start, 0);
cout << "Case " << counter << ": The longest path from " << start << " has length " << result.first << ", finishing at " << result.second << "." << endl << endl;
}
return 0;
}