CSES - HIIT Open 2019 - Results
Submission details
Task:Many Cycles
Sender:tykkipeli
Submission time:2019-05-25 14:07:38 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#10.04 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);
        }
        dfs(1,0);
        if (found) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
        
    }
    
}










Test details

Test 1

Verdict:

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

correct output
YES
YES
YES
YES
NO
...

user output
YES
NO
NO
NO
NO
...
Truncated

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
...