Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<int>> g;
- vector<bool> visited;
- vector<int> cycle;
- int ans = 0;
- void svaznost_chek(int v, int prev)
- {
- if (ans || cycle[v])
- return;
- cycle[v] = -1;
- for (auto v_i : g[v])
- {
- if (cycle[v_i] < 0 && v_i != prev)
- {
- ans = 1;
- return;
- }
- svaznost_chek(v_i, v);
- }
- cycle[v] = 1;
- }
- int main()
- {
- int n, m;
- cin >> n >> m;
- visited.resize(n, false);
- g.resize(n);
- cycle.resize(n, 0);
- for (int i = 0; i < m; ++i)
- {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- // проверка на связность
- svaznost_chek(0, 0);
- if (!ans)
- {
- for (int j = 0; j < n; ++j)
- {
- if (cycle[j] == 0)
- {
- cout << "NO";
- break;
- }
- }
- cout << "YES";
- }
- else
- cout << "NO";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment