-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva12442.cpp
More file actions
54 lines (49 loc) · 848 Bytes
/
Uva12442.cpp
File metadata and controls
54 lines (49 loc) · 848 Bytes
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
#include <iostream>
#include <vector>
/**
* 12442 - Forwarding Emails
* @author mostafa
*/
using namespace std ;
int dfs(int u);
vector<int> graph ;
vector<bool> visited ;
vector<int> lengths ;
int main()
{
int TC , N , u , v ;
cin>>TC ;
for(int k = 1 ; k <= TC ; k++)
{
cin>>N ;
graph.assign(N,-1);
visited.assign(N,false);
lengths.assign(N,-1);
for(int i = 0 ; i < N ; i++)
{
cin>>u>>v ;
graph[u-1] = v-1 ;
}
int maxLength = -1 , vertex = -1 ;
for(int i = 0 ; i < N ; i++)
{
if(lengths[i]==-1) dfs(i);
if(lengths[i]>maxLength)
{
maxLength = lengths[i];
vertex = i ;
}
}
cout<<"Case "<<k<<": "<<vertex+1<<endl ;
}
return 0 ;
}
int dfs(int u)
{
int sum = 0 ;
visited[u] = true ;
if(!visited[graph[u]])
sum += 1 + dfs(graph[u]);
visited[u] = false ;
return lengths[u] = sum ;
}