[uva] 11396 – claw decomposition

出處

https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2391

題意

思路

二分圖判斷

程式碼

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

#include <queue>
#include <vector>
#ifdef LOCAL
#include "stloutput.h"
#endif
#define INF 1000000000

using namespace std;

int () {
int V;
while (cin >> V, V) {
int a, b;
vector<vector<int>> g(V);
while (cin >> a >> b, a | b) {
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> color(V, -1);
queue<int> q;
q.push(0);
color[0] = 0;
bool bi = true;
while (!q.empty() && bi) {
int tmp = q.front();
q.pop();
for (auto i : g[tmp]) {
if (color[i] == -1) {
color[i] = 1 - color[tmp];
q.push(i);
} else if (color[i] == color[tmp]) {
bi = false;
break;
}
}
}
bi ? cout << "YES" << endl : cout << "NO" << endl;
}
}