forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10000.cpp
79 lines (73 loc) · 1.33 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
#define SZ(a) ((int)(a).size())
typedef struct _node
{
int n, length;
vector<int> adj;
} node;
static vector<node> G;
static pair<int, int> result;
static int n, start, a, b, counter = 0;
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.empty())
{
if (length > result.first)
{
result.first = length;
result.second = start;
}
else if (length == result.first)
{
if (start < result.second)
{
result.second = start;
}
}
}
else
{
int end = SZ(G[start - 1].adj);
FOR(i, 0, end)
{
dfs(G[start - 1].adj[i], length + 1);
}
}
}
int main()
{
while (scanf("%d", &n) == 1 && n)
{
G.clear();
counter++;
FOREQ(i, 1, n)
{
node N;
N.n = i;
N.length = 0;
G.push_back(N);
}
scanf("%d", &start);
while (scanf("%d%d", &a, &b) && a && b)
{
G[a - 1].adj.push_back(b);
}
result.first = 0;
result.second = 0;
dfs(start, 0);
printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n",
counter, start, result.first, result.second);
}
return 0;
}