-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpick.java
More file actions
160 lines (156 loc) · 3.89 KB
/
pick.java
File metadata and controls
160 lines (156 loc) · 3.89 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Stack;
@SuppressWarnings("unchecked")
public class pick {
// Digraph class taken from the textbook
static class Digraph
{
private final int V;
private int E;
private LinkedList<Integer>[] adj;
private static final String NEWLINE = System.getProperty("line.separator");
public Digraph(int V)
{
this.V = V;
this.E = 0;
adj = (LinkedList<Integer>[]) new LinkedList[V];
for (int v = 0; v < V; v++)
adj[v] = new LinkedList<Integer>();
}
public int V() { return V; }
public int E() { return E; }
public void addEdge(int v, int w)
{
adj[v].add(w);
E++;
}
public Iterable<Integer> adj(int v)
{ return adj[v];
}
public Digraph reverse()
{
Digraph R = new Digraph(V);
for (int v = 0; v < V; v++)
for (int w : adj(v))
R.addEdge(w, v);
return R;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V + " vertices, " + E + " edges " + NEWLINE);
for (int v = 0; v < V; v++) {
s.append(String.format("%d: ", v));
for (int w : adj[v]) {
s.append(String.format("%d ", w));
}
s.append(NEWLINE);
}
return s.toString();
}
}
// DirectedCycle class taken from the textbook
static class DirectedCycle
{
private boolean[] marked;
private int[] edgeTo;
private Stack<Integer> cycle;
private boolean[] onStack;
public DirectedCycle(Digraph G)
{
onStack = new boolean[G.V()];
edgeTo = new int[G.V()];
marked = new boolean[G.V()];
for (int v = 1; v < G.V(); v++)
if (!marked[v]) dfs(G, v);
}
private void dfs(Digraph G, int v)
{
onStack[v] = true;
marked[v] = true;
for (int w : G.adj(v))
if (this.hasCycle()){
return;
}
else if (!marked[w])
{ edgeTo[w] = v; dfs(G, w); }
else if (onStack[w])
{
cycle = new Stack<Integer>();
for (int x = v; x != w; x = edgeTo[x])
cycle.push(x);
cycle.push(w);
cycle.push(v);
}
onStack[v] = false;
}
public boolean hasCycle()
{ return cycle != null; }
public Iterable<Integer> cycle()
{ return cycle; }
}
// DFS class taken from the textbook
static class DepthFirstOrder
{
private boolean[] marked;
private Stack<Integer> reversePost;
public DepthFirstOrder(Digraph G)
{
reversePost = new Stack<Integer>();
marked = new boolean[G.V()];
for (int v = 1; v < G.V(); v++)
if (!marked[v]) dfs(G, v);
}
private void dfs(Digraph G, int v)
{
marked[v] = true;
for (int w : G.adj(v))
if (!marked[w])
dfs(G, w);
reversePost.push(v);
}
public Iterable<Integer> reversePost()
{ return reversePost; }
}
// Topological class taken from the textbook
static class Topological
{
private Iterable<Integer> order;
public Topological(Digraph G)
{
DepthFirstOrder dfs = new DepthFirstOrder(G);
order = dfs.reversePost();
}
public Iterable<Integer> order()
{ return order; }
public boolean isDAG()
{ return order == null; }
}
// This is the beginning of the code writen by me.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s1 = scan.nextLine();
String [] array = s1.split(" ");
int numsticks = Integer.parseInt(array[0]);
int numlines = Integer.parseInt(array[1]);
Digraph d = new Digraph(numsticks+1);
while(scan.hasNextLine()) {
String s = scan.nextLine();
String [] arr = s.split(" ");
d.addEdge(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]));
}
DirectedCycle dc = new DirectedCycle(d);
if(dc.hasCycle() == true) {
System.out.println("IMPOSSIBLE");
}
else {
Topological t = new Topological(d.reverse());
StringBuilder sb = new StringBuilder();
for(int w:t.order()) {
sb.append(w);
sb.append("\n");
}
System.out.print(sb);
}
}
}