Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.0 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
 
const double PI = 3.14159265358979;
const ll INF =  1e18 + 7;
const ll MOD = 1e9 + 7;
const ll nax = 100005;
const int LOG = 25;


vector<int> adj[nax];
vector<int> color(nax, 0);
bool isPossible = true;

void dfs(int u, int col) {
    color[u] = col;

    for (int v : adj[u]) {
        if (color[v] == 0) {
            dfs(v, 3 - col);
        } else if (color[v] == col) {
            isPossible = false;
            return;
        }
    }
}

void solve() {
    int n , m;
    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        adj[x].emplace_back(y);
        adj[y].emplace_back(x);
    }

    for (int i = 1; i <= n; i++) {
        if (color[i] != 0) continue;
        dfs(i, 1);
    }

    cout << (isPossible ? "YES" : "NO");
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // int t; cin >> t; while(t--)
    solve();
    return 0;
}
Leave a Comment