import java util class point int value int cap T1 comp low -1 ArrayLis

 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
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.*;
class point {
//int value;
int cap=0,T1,comp,low=-1;
ArrayList<Integer> list = new ArrayList<Integer>();
}
public class Factorial{
static int time=1, count=1;//global
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int i=0,j=0,n=sc.nextInt(),first,second,m=sc.nextInt(),sizelen=0;
point [] graph=new point[n];
for (i=0;i<n;i++) {
graph[i]=new point();
}
for(i=0;i<m;i++){
first=sc.nextInt(); second=sc.nextInt();
graph[first].cap++;
graph[first].list.add(second);
}
Tarjan(n,graph);
point []cond=new point[n];
for (i=0;i<n;i++) {
cond[i]=new point();
}
for(i=0;i<n;i++)
{
cond[graph[i].comp].list.add(i);
if(cond[graph[i].comp].low==-1 || i<cond[graph[i].comp].low)
cond[graph[i].comp].low=i;
}
int [] base=new int[n];
List<Integer> Base = new ArrayList<>();
for(i=0;i<n;i++)
base[i]=1;
for(i=0;i<n;i++){
sizelen=graph[i].cap;
for(j=0;j<sizelen;j++){
if(graph[i].comp!=graph[graph[i].list.get(j)].comp)
base[graph[graph[i].list.get(j)].comp]=0;
}
}
for(i=0;i<n;i++){
if(base[i]==1){
Base.add(cond[i].low);
}
}
Collections.sort(Base);
sizelen=Base.size();
for(i=0;i<sizelen;i++)
System.out.println(Base.get(i));
}
public static void Tarjan(int n,point[] graph)
{
int i=0;
for(i=0;i<n;i++) {
graph[i].T1 = 0;
graph[i].comp = 0;
}
Stack <point> s = new Stack <point>();
for(i=0;i<n;i++) {
if(graph[i].T1 == 0)
VisitVertex_Tarjan(i,graph,s);
}
}
public static void VisitVertex_Tarjan(int i,point [] graph, Stack <point> s)
{
int j=0;
graph[i].T1=time;
graph[i].low=time;
point u;
time++;
s.push(graph[i]);
for(;j<graph[i].cap;j++)
{
if(graph[graph[i].cap].T1==0)
VisitVertex_Tarjan(graph[i].cap, graph, s);
if((graph[graph[i].cap].comp==0) && graph[i].low > graph[graph[i].cap].low)
graph[i].low = graph[graph[i].cap].low;
}
if(graph[i].T1==graph[i].low){
do{
u=s.pop();
u.comp=count;
}
while(u != graph[i]);
count++;
}
}
}