답안 #134010

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134010 2019-07-21T22:56:07 Z lyc Potemkin cycle (CEOI15_indcyc) C++14
30 / 100
1000 ms 2040 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x) 
#define REP(i, n) for (int i = 0; i < n; ++i) 
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

const int MAXN = 1e3+5;
const int MAXR = 1e5+5;

int N, R;
vector<int> al[MAXN];
vector<int> ans;
bool vis[MAXN];

queue<int> q; int dist[MAXN], pa[MAXN];

void trace(int a, int b) {
    if (dist[a] < dist[b]) swap(a,b);
    while (dist[a] > dist[b]) ans.push_back(a), a = pa[a];
    vector<int> ans2;
    while (a != b) ans.push_back(a), ans2.push_back(b), a = pa[a], b = pa[b];
    ans.push_back(a);
    reverse(ALL(ans2));
    for (auto x : ans2) ans.push_back(x);
}

void solve(int u) {
    //cout << "\t\tSOLVE " << u << endl;
    FOR(i,1,N) dist[i] = -1, pa[i] = -1;
    dist[u] = 0; q.push(u);
    while (!q.empty() and ans.empty()) {
        int u = q.front(); q.pop();
        bool flag = true;
        for (auto v : al[u]) if (v != pa[u]) {
            //cout << u << " -> " << v << endl;
            if (~dist[v]) {
                flag = false;
                //cout << "\t\t\tTRACE " << u << " " << v << endl;
                trace(u,v);
                //cout << " \t\t\t:: "; for (auto x:ans) { cout << x << ' '; } cout << endl;
                if (SZ(ans) < 4) ans.clear();
                else break;
            }
        }
        if (flag) for (auto v : al[u]) if (v != pa[u]) {
            if (dist[v] == -1) dist[v] = dist[u]+1, pa[v] = u, q.push(v);
        }
    }
}

void dfs(int u, int p) {
    if (!ans.empty()) return;
    vis[u] = 1;
    for (auto v : al[u]) {
        if (!vis[v]) dfs(v, u);
        else if (v != p) {
            solve(v);
            if (!ans.empty()) return;
        }
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> R;
    FOR(i,1,R){
        int A, B; cin >> A >> B;
        al[A].push_back(B);
        al[B].push_back(A);
    }

    FOR(i,1,N) vis[i] = 0;
    FOR(i,1,N) if (!vis[i]) { 
        dfs(1,0);
        if (!ans.empty()) break;
    }

    if (ans.empty()) cout << "no" << '\n';
    else FOR(i,0,SZ(ans)-1) cout << ans[i] << (i==SZ(ans)-1 ? '\n' : ' ');
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 58 ms 476 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 504 KB Output is correct
2 Incorrect 85 ms 544 KB Expected integer, but "no" found
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 336 ms 376 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1061 ms 1316 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1079 ms 760 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1067 ms 2040 KB Time limit exceeded
2 Halted 0 ms 0 KB -