forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1944 Fiber Communications.java
51 lines (49 loc) · 1.06 KB
/
1944 Fiber Communications.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
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int nodeNumber = in.nextInt();
int pathNumber = in.nextInt();
int[] nodes = new int[(nodeNumber + 1) * 2];
int[][] paths = new int[pathNumber + 1][2];
for (int i = 1; i <= pathNumber; ++i) {
int u = in.nextInt();
int v = in.nextInt();
paths[i][0] = Math.min(u, v);
paths[i][1] = Math.max(u, v);
}
int rst = Integer.MAX_VALUE;
for (int i = 1; i <= nodeNumber; ++i) {
Arrays.fill(nodes, 0);
for (int j = 1; j <= pathNumber; ++j) {
int u = paths[j][0];
int v = paths[j][1];
if (u <= i) {
u += nodeNumber;
}
if (v <= i) {
v += nodeNumber;
}
if (u > v) {
int temp = v;
v = u;
u = temp;
}
++nodes[u];
--nodes[v];
}
int cur = 0;
int res = 0;
for (int j = 1; j <= nodeNumber; ++j) {
cur += nodes[i + j];
if (cur > 0) {
res++;
}
}
rst = Math.min(rst, res);
}
System.out.println(rst);
}
}