-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva12442.java
More file actions
68 lines (65 loc) · 2.37 KB
/
Copy pathUva12442.java
File metadata and controls
68 lines (65 loc) · 2.37 KB
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
import java.util.* ;
import java.io.* ;
/**
* 12442 - Forwarding Emails
* @author mostafa
* Descroption : in this problem , it must have cycles so every vertex have one neighbour
* if you are going to count every node , you getting TLE .
* you might use a vector to store cycle vertex values
* so you don't need to calculate it again and again ..
* if you use recursion DFS method , it helps you a lot .
*/
public class Uva12442
{
static int [] graph ;
static int [] lengths ;
static boolean [] visited ;
static int N ;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = sc.nextInt();
for (int i = 1 ; i <= tc ; i++)
{
N = sc.nextInt()+1 ;
graph = new int[N];
visited = new boolean[N];
lengths = new int[N]; // you have to save lengths , otherwise you'r going to get TLE
Arrays.fill(lengths, -1); // (Time Limit Exceeded)
for(int k = 1 ; k < N ; k++)
{
int u = sc.nextInt();
int v = sc.nextInt();
graph[u] = v;
}
int maxLength = Integer.MIN_VALUE ;
int vertex = -1 ;
for(int k = 1 ; k < N ; k++) // start from down to top , otherwise will get WA(Wrong Answer)
{ // it stated in problem "If there is more than one correct answer,"
if(lengths[k]==-1) // ", output the smallest number"
DFS(k);
if(lengths[k]>maxLength)
{
maxLength = lengths[k];
vertex = k ;
}
}
sb.append("Case "+i+": "+vertex+"\n");
}
pr.print(sb.toString());
pr.close();
sc.close();
}
// special DFS Using Recursion
public static int DFS(int vertex)
{
int sum = 0 ;
visited[vertex] = true ;
if(!visited[graph[vertex]])
sum += 1 + DFS(graph[vertex]);
visited[vertex] = false ; // clean visited array with default values(false)
return lengths[vertex] = sum ;
}
}