-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12442.cpp
82 lines (61 loc) · 1.41 KB
/
12442.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
80
81
82
/*
Author: Andreea Musat
Date: 20 sept 2017
Find starting point of largest path in a particular graph (every node has a single neighbour)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3873
*/
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
ostream& operator<<(ostream& os, vector<int> v)
{
for (size_t i = 0; i < v.size(); i++)
os << v[i] << " ";
os << "\n";
return os;
}
int dfs(const vector<int>& graph, vector<int>& dist, vector<bool>& visited, int s)
{
visited[s] = true;
int neighbour = graph[s], len = 0;
if (!visited[neighbour])
len = 1 + dfs(graph, dist, visited, neighbour);
visited[s] = false;
return dist[s] = len;
}
int main()
{
int T, n, i, u, v, cnt = 1;
vector<int> graph;
vector<int> dist;
vector<bool> visited;
cin >> T;
while (T--)
{
cin >> n;
graph.clear(); dist.clear(); visited.clear();
graph.resize(n); dist.resize(n, INF); visited.resize(n, false);
for (i = 0; i < n; i++)
{
cin >> u >> v;
graph[u - 1] = v - 1;
}
dist.resize(n, INF);
int max_res = INT_MIN, crt_res;
int starting_point = -1;
for (i = 0; i < n; i++)
{
if (dist[i] == INF)
{
crt_res = dfs(graph, dist, visited, i);
if (crt_res > max_res)
{
max_res = crt_res;
starting_point = i;
}
}
}
cout << "Case " << cnt++ << ": " << starting_point + 1 << "\n";
}
return 0;
}