제출 #527083

#제출 시각아이디문제언어결과실행 시간메모리
527083joelauPotemkin cycle (CEOI15_indcyc)C++14
20 / 100
28 ms3156 KiB
#include <bits/stdc++.h>
using namespace std;

int N,M, p[1005], depth[1005], A[1005];
vector<int> lst[1005];
bitset<1005> visited;
vector< pair<int,int> > edges;

bool cmp (pair<int,int> a, pair<int,int> b) {
    if (a.first == b.first) return depth[a.second] < depth[b.second];
    else return depth[a.first] > depth[b.first];
}

void dfs (int x, int par, int d) {
    visited[x] = 1, p[x] = par, depth[x] = d;
    for (int v: lst[x]) if (!visited[v]) dfs(v,x,d+1);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> M;
    for (int i = 0; i < M; ++i) {
        int u,v; cin >> u >> v; u--, v--;
        lst[u].push_back(v), lst[v].push_back(u);
    }
    for (int i = 0; i < N; ++i) if (!visited[i]) dfs(i,-1,0);
    for (int u = 0; u < N; ++u) for (int v: lst[u]) if (depth[u] < depth[v] && depth[v]-depth[u] != 1) edges.emplace_back(u,v);
    sort(edges.begin(),edges.end(),cmp);
    memset(A,0,sizeof(A));
    for (auto x: edges) {
        if (depth[x.second]-depth[x.first] > 2) {
            bool can = true;
            for (int u = x.second; u != x.first && can; u = p[u]) if (A[u] != 0) can = false;
            if (can) {
                for (int u = x.second; u != x.first; u = p[u]) cout << u+1 << ' ';
                cout << x.first+1;
                return 0;
            }
        }
        A[x.second]++;
    }
    cout << "no";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...