
出處
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=945
題意
思路
判斷二分圖
程式碼
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
|
#include <queue> #include <vector> #ifdef LOCAL #include "stloutput.h" #endif #define INF 1000000000
using namespace std;
int () { int n, l; while (cin >> n, n) { vector<vector<int>> v(n); cin >> l; for (int i = 0; i < l; i++) { int a, b; cin >> a >> b; v[a].push_back(b); }
queue<int> q; vector<int> color(n, -1); q.push(0); color[0] = 0; bool check = true; while (!q.empty() && check) { int tmp = q.front(); q.pop(); for (int i = 0; i < v[tmp].size(); i++) { if (color[v[tmp][i]] == -1) { q.push(v[tmp][i]); color[v[tmp][i]] = 1 - color[tmp]; } else if (color[v[tmp][i]] == color[tmp]) { check = false; break; } } } check ? cout << "BICOLORABLE." << endl : cout << "NOT BICOLORABLE." << endl; } }
|
近期评论