CSES - HIIT Open 2019 - Results
Submission details
Task:Many Cycles
Sender:tykkipeli
Submission time:2019-05-25 14:12:29 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.06 sdetails
#2ACCEPTED0.02 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<int> adj[1000];
int prevv[1000];
bool visited[1000];
bool inCycle[1000];
bool found = false;
set<int> setti[1000];

void dfs(int v, int e) {
    if (found) return;
    visited[v] = true;
    prevv[v] = e;
    for (int next : adj[v]) {
        if (next == e) continue;
        if (!visited[next]) {
            dfs(next,v);
        } else {
            if (setti[v].find(next) != setti[v].end()) continue;
            if (inCycle[next]) {
                found = true;
                return;
            }
            setti[next].insert(v);
            inCycle[next] = true;
            int x = v;
            while(x != next) {
                if (inCycle[x]) {
                    found = true;
                    return;
                }
                inCycle[x] = true;
                x = prevv[x];
            }
        }
    }
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n,m;
        cin >> n >> m;
        for (int j = 1; j <= n; j++) {
            adj[j].clear();
            setti[j].clear();
            visited[j] = false;
            inCycle[j] = false;
        }
        found = false;
        for (int j = 0; j < m; j++) {
            int a,b;
            cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        for (int node = 1; node <= n; node++) {
            if (!visited[node]) {
                dfs(node,0);
            }
        }
        if (found) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
        
    }
    
}










Test details

Test 1

Verdict: ACCEPTED

input
1000
100 78
97 68
75 90
58 80
...

correct output
YES
YES
YES
YES
NO
...

user output
YES
YES
YES
YES
NO
...

Test 2

Verdict: ACCEPTED

input
11
2 1
1 2
6 6
1 2
...

correct output
NO
NO
NO
YES
YES
...

user output
NO
NO
NO
YES
YES
...