-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBishu and his Girlfriend
70 lines (58 loc) · 1.5 KB
/
Bishu and his Girlfriend
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
import java.io.*;
import java.util.*;
class TestClass {
static class Graph
{
int v;
LinkedList<Integer>[] adj;
Graph(int v)
{
this.v=v;
adj=new LinkedList[v];
for(int i=0;i<adj.length;i++)
{
adj[i]=new LinkedList<Integer>();
}
}
void add_edge(int u,int v)
{
adj[u].add(v);
}
}
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int node=sc.nextInt();
Graph g= new Graph(node);
for(int i=0;i<node-1;i++)
{
int u=sc.nextInt();
int v=sc.nextInt();
g.add_edge(u,v);
}
int query=sc.nextInt();
int arr[]=new int[node+1];
while(query-->0)
{
int t=sc.nextInt();
arr[t]=1;
}
boolean visited[]=new boolean[node];
LinkedList<Integer> queue=new LinkedList<Integer>();
visited[1]=true;
queue.add(1);
while(queue.size()!=0)
{
int s=queue.poll();
Iterator<Integer> i = g.adj[s].iterator();
while(i.hasNext())
{
int n=i.next();
if(arr[n]==1)
{
System.out.println(n);
break;
}
}
}
}
}