-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask4.java
170 lines (138 loc) · 4.04 KB
/
Task4.java
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
161
162
163
164
165
166
167
168
169
170
/* Kornilov Nikita, M3102, 22.02.2021 */
package Sem2.Lab8;
import java.io.*;
import java.util.*;
public class Task4 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
class Graph {
private final int V; //
private int E;
private ArrayList<Integer>[] adj;
public Graph(int V) {
this.V = V; this.E = 0;
adj = (ArrayList<Integer>[]) new ArrayList[V];
for (int v = 0; v < V; v++) {
adj[v] = new ArrayList<>();
}
}
public Graph() throws IOException {
this(nextInt());
int E = nextInt();
for (int i = 0; i < E; i++) {
int v = nextInt() - 1, w = nextInt() - 1;
addEdge(v, w);
}
}
public int getV() {
return V;
}
public int getE() {
return E;
}
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
E++;
}
public Iterable<Integer> adj(int v) {
return adj[v];
}
@Override
public String toString() {
StringBuilder s = new StringBuilder(String.format("%d vertices, %d edges\n", V, E));
for (int v = 0; v < V; v++) {
s.append(v).append(": ");
for (int w : this.adj(v)) {
s.append(w).append(" ");
}
s.append("\n");
}
return s.toString();
}
}
class CC {
private boolean[] marked;
private int[] id;
private int count;
public CC(Graph G) {
marked = new boolean[G.getV()];
id = new int[G.getV()];
for (int s = 0; s < G.getV(); s++) {
if (!marked[s]) {
dfs(G, s);
count++;
}
}
}
private void dfs(Graph G, int v) {
marked[v] = true;
id[v] = count;
for (int w :G.adj(v)) {
if (!marked[w]) {
dfs(G, w);
}
}
}
public boolean isConnected(int v, int w) {
return id[v] == id[w];
}
public int getId(int v) {
return id[v];
}
public int getCount() {
return count;
}
}
public static void main(String[] args) {
String fileName = "components";
new Task4().run(String.format("%s.in", fileName), String.format("%s.out", fileName));
}
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
String inputString = br.readLine();
if (inputString != null) {
in = new StringTokenizer(inputString);
} else {
return null;
}
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public void solve() throws IOException {
Graph graph = new Graph();
CC cc = new CC(graph);
int M = cc.getCount();
out.println(M);
// ArrayList<Integer>[] components;
// components = (ArrayList<Integer>[]) new ArrayList[M];
// for (int i : cc.id)
// out.print(i + 1 + " ");
// out.println();
// for (int i = 0; i < M; i++) {
// components[i] = new ArrayList<>();
// }
// for (int v = 0; v < graph.getV(); v++) {
// components[cc.getId(v)].add(v);
// }
//
for (int v = 0; v < graph.getV(); v++) {
out.print(cc.getId(v) + 1 + " ");
}
}
public void run(String inputFile, String outputFile) {
try {
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}