Skip to content

Commit 2deec7d

Browse files
authored
[BOJ] 12893 적의 적 (G4)
1 parent 1a5f6f7 commit 2deec7d

File tree

1 file changed

+66
-0
lines changed
  • 박예진/0주차/유니온파인드

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//https://www.acmicpc.net/problem/12893
2+
#include <iostream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <queue>
6+
#include <cstring>
7+
using namespace std;
8+
9+
/*
10+
이분그래프
11+
유니온파인드
12+
v + N = v 반대 상태
13+
*/
14+
15+
int N, M;
16+
int parent[4001];
17+
18+
int getParent(int x) {
19+
if (parent[x] == x) return x;
20+
return parent[x] = getParent(parent[x]);
21+
}
22+
23+
void unionParent(int x, int y) {
24+
int px = getParent(x);
25+
int py = getParent(y);
26+
27+
if (px < py) parent[py] = px;
28+
else parent[px] = py;
29+
}
30+
31+
int main(){
32+
ios_base::sync_with_stdio(false);
33+
cin.tie(NULL); cout.tie(NULL);
34+
35+
cin >> N >> M;
36+
for(int i = 1; i <= 2*N; i++)
37+
parent[i] = i;
38+
39+
bool flag = true;
40+
41+
for(int i = 0; i < M; i++){
42+
int u, v;
43+
cin >> u >> v;
44+
45+
// 이미 같은 편이면 모순
46+
if (getParent(u) == getParent(v)) {
47+
flag = false;
48+
break;
49+
}
50+
51+
// 적의 적 연결
52+
unionParent(u, v + N);
53+
unionParent(v, u + N);
54+
55+
// 자기 자신과 적이 되면 모순
56+
if (getParent(u) == getParent(u + N) ||
57+
getParent(v) == getParent(v + N)) {
58+
flag = false;
59+
break;
60+
}
61+
}
62+
63+
cout << (flag ? 1 : 0);
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)