답안 #110007

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
110007 2019-05-08T17:35:47 Z someone_aa Potemkin cycle (CEOI15_indcyc) C++17
30 / 100
63 ms 2532 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 1010;

bool edge[maxn][maxn];
vector<int>g[maxn];
int n, m;

class dsu {
public:
    int n, uparent[maxn], usize[maxn];

    void init(int _n) {
        n = _n;
        for(int i=0;i<=n;i++) {
            usize[i] = 1;
            uparent[i] = i;
        }
    }
    int root(int x) {
        while(x != uparent[x]) {
            x = uparent[x];
        }
        return x;
    }
    bool connected(int u, int v) {
        return root(u) == root(v);
    }
    void unite(int u, int v) {
        u = root(u);
        v = root(v);

        if(u == v) return;
        if(usize[u] > usize[v]) {
            uparent[v] = u;
            usize[u] += usize[v];
        }
        else {
            uparent[u] = v;
            usize[v] += usize[u];
        }
    }
};

dsu x;
bool visited[maxn];
int root;

bool f_edge[maxn];
int parent[maxn];

void find_path(int root, int i, int j) {
    memset(visited,false,sizeof(visited));
    memset(parent,-1,sizeof(parent));

    parent[i] = root;
    parent[root] = 0;

    visited[root] = true;
    visited[i] = true;
    queue<int>q;
    q.push(i);

    while(!q.empty()) {
        int curr = q.front();
        q.pop();

        for(int i:g[curr]) {
            if(!visited[i]) {
                visited[i] = true;
                parent[i] = curr;
                q.push(i);
            }
        }
    }

    int x = j;
    while(x) {
        cout<<x<<" ";
        x = parent[x];
    }
    return;
}

void dfs(int node) {
    if(visited[node] || f_edge[node]) return;
    visited[node] = true;

    for(int i:g[node]) {
        if(!visited[i]) {
            dfs(i);
            x.unite(i, node);
        }
    }
}

void solve() {
    bool found = false;
    for(root=2;root<=2;root++) {
        memset(f_edge, false,sizeof(f_edge));
        memset(visited,false,sizeof(visited));
        x.init(n);

        visited[root] = true;
        for(int i:g[root]) {
            f_edge[i] = true;
        }

        for(int i=1;i<=n;i++) {
            if(!visited[i]) {
                dfs(i);
            }
        }

        for(int i:g[root]) {
            for(int j:g[root]) {
                if(i < j && !edge[i][j] && x.connected(i, j)) {
                    find_path(root, i, j);
                    found = true;
                    break;
                }
            }
            if(found) break;
        }
        if(found) break;
    }
    if(!found) {
        cout<<"no\n";
    }
}

int main() {
    cin>>n>>m;
    int u, v;
    for(int i=0;i<m;i++) {
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
        edge[u][v] = edge[v][u] = true;
    }
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 384 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 512 KB Wrong adjacency
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 768 KB Output is correct
2 Incorrect 4 ms 664 KB Wrong adjacency
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 768 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 38 ms 2296 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 1792 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 63 ms 2532 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -