Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 1e5 + 1;
- vector <int> g[MAXN], ans;
- short col[MAXN], cur_col = 1; // 0 - unvisited, 1 - part 1, 2 - part 2
- bool dfs(int v) // Поиск в глубину (true - Ok, false - цикл)
- {
- col[v] = cur_col;
- cur_col = cur_col % 2 + 1; //swap colors
- for(int to : g[v])
- {
- if(col[to] and col[to] != cur_col or col[to] == 0 and !dfs(to))
- return false;
- }
- cur_col = cur_col % 2 + 1; //swap colors
- return true;
- }
- string solve(int n)
- {
- for(int i = 1; i <= n; i++)
- if(col[i] == 0 and !dfs(i))
- {
- return "NO";
- }
- return "BIPARTITE";
- }
- int main()
- {
- // Ускорение ввода
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- // Ввод/вывод из файла
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- // Ввод
- int n, m;
- cin >> n >> m;
- while(m--)
- {
- int a, b;
- cin >> a >> b;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- cout << solve(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement