Skip to content

Commit bbfac2f

Browse files
authored
Merge pull request #407 from AlgorithmWithGod/khj20006
[20250702] BOJ / G3 / 대도시 구축 / 권혁준
2 parents 09a6cb2 + 15be523 commit bbfac2f

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, M;
57+
static int[][] infos;
58+
59+
public static void main(String[] args) throws Exception {
60+
61+
io = new IOController();
62+
63+
init();
64+
solve();
65+
66+
io.close();
67+
68+
}
69+
70+
public static void init() throws Exception {
71+
72+
N = io.nextInt();
73+
M = io.nextInt();
74+
infos = new int[M][];
75+
for(int i=0;i<M;i++) infos[i] = new int[]{io.nextInt(), io.nextInt()};
76+
77+
}
78+
79+
static void solve() throws Exception {
80+
81+
Arrays.sort(infos, (a,b) -> a[0]==b[0] ? a[1]-b[1] : a[0]-b[0]);
82+
long ans = (long)N*(N+1)/2 + N-2;
83+
84+
if(M == 1) {
85+
if(infos[0][0] == 1) {
86+
if(infos[0][1] == 2) ans += 2;
87+
else ans++;
88+
}
89+
}
90+
else if(M == 2) {
91+
int a = infos[0][0], b = infos[0][1];
92+
int aa = infos[1][0], bb = infos[1][1];
93+
if(a == 1 && aa == 1) {
94+
if(b == 2) {
95+
if(bb == 3) ans += 4;
96+
else ans += 3;
97+
}
98+
else ans += 2;
99+
}
100+
else if(a == 1) {
101+
if(b == 2) {
102+
if(aa == 2 && bb == 3) ans += 3;
103+
else ans += 2;
104+
}
105+
else {
106+
if(aa == 2 && b == bb) {
107+
if(b == 3) ans += 3;
108+
else ans += 2;
109+
}
110+
else ans++;
111+
}
112+
}
113+
}
114+
115+
io.write(ans + "\n");
116+
117+
}
118+
119+
}
120+
```

0 commit comments

Comments
 (0)