BOJ 20040 사이클 게임

1 개요[ | ]

BOJ 20040 사이클 게임

2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int parent[500000];

int Find(int u) {
    if (parent[u] == u) return u;
    return parent[u] = Find(parent[u]);
}

bool Union(int u, int v) {
    u = Find(u);
    v = Find(v);
    if (u == v) return true;
    parent[u] = v;
    return false;
}

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        parent[i] = i;
    }
    int u, v, answer;
    for (int i=1; i<=m; i++) {
        cin >> u >> v;
        if(Union(u, v)) {
            cout << i;
            return;
        }
    }
    cout << 0;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}