Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream in;
- ofstream out;
- int color[101];
- bool isBipartite(int n,int a[][101], int src)
- {
- for (int i = 1; i <= n; ++i)
- color[i] = -1;
- color[src] = 1;
- queue <int> q;
- q.push(src);
- while (!q.empty())
- {
- int u = q.front();
- q.pop();
- if (a[u][u] == 1)
- return false;
- for (int j = 1; j <= n; j++)
- {
- if (a[u][j] && color[j] == -1)
- {
- color[j] = 1 - color[u];
- q.push(j);
- }
- else if (a[u][j] && color[j] == color[u])
- return false;
- }
- }
- return true;
- }
- int n, m, x, y, i, j, k, a[101][101];
- int main()
- {
- in.open("bipartit1.in");
- out.open("bipartit1.out");
- in >> n >> m;
- for (i = 1; i <= m; i++)
- {
- in >> x >> y;
- a[x][y] = a[y][x] = 1;
- }
- bool ok;
- ok = isBipartite(n, a, 1);
- if (ok)
- {
- out << "DA" << endl;
- for (i = 1; i <= n; i++)
- if (color[1] == color[i])
- out << i << " ";
- out << endl;
- for (i = 1; i <= n; i++)
- if (color[1] != color[i])
- out << i << " ";
- }
- else
- out << "NU";
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement