Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Created by Manuver Gujjar on 25/09/19.
- // Copyright © 2019 Manuver Gujjar. All rights reserved.
- #include "bits/stdc++.h"
- using namespace std;
- vector<vector<int>> adj;
- int dfs() {
- long n = adj.size();
- int start = 1;
- vector<bool> vis(n, false);
- vis[start] = true;
- stack<pair<int, int>> s;
- s.push({start, 0});
- while(not s.empty()) {
- auto top = s.top();
- s.pop();
- for(auto i : adj[top.first]) {
- if(vis[i] == true) {
- if(i == top.second) continue;
- return 0;
- }
- vis[i] = true;
- s.push({i, top.first});
- }
- }
- for(int i=1; i<n; i++) if(vis[i] == false) return 0;
- return 1;
- }
- int main() {
- long n, m;
- cin >> n >> m;
- adj.resize(n + 1);
- for(int i=0; i<m; i++) {
- int a, b;
- cin >> a >> b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- cout << (dfs() ? "YES\n" : "NO\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement