Submission #658096

# Submission time Handle Problem Language Result Execution time Memory
658096 2022-11-12T07:33:22 Z FatihSolak Potemkin cycle (CEOI15_indcyc) C++17
100 / 100
272 ms 10096 KB
#include <bits/stdc++.h>
#define N 1005
using namespace std;
bool edge[N][N];
int vis[N][N];
int par[N][N];
int n;
void dfs(int a,int b){
    vis[a][b] = 1;
    for(int i = 1;i<=n;i++){
        if(i == a || edge[a][i] || !edge[b][i])continue;
        if(vis[b][i] == 1){
            vector<int> cycle;
            cycle.push_back(b);
            cycle.push_back(a);
            while(a != i){
                cycle.push_back(par[a][b]);
                int tmp = par[a][b];
                b = a;
                a = tmp;
            }
            int sz = cycle.size();
            int l = 0, r = sz-1;
            for(int i = 0;i<sz;i++){
                for(int j = i+2;j<sz;j++){
                    if(edge[cycle[i]][cycle[j]]){
                        if(r - l > j - i){
                            l = i;
                            r = j;
                        }
                    }
                }
            }
            for(int i = l;i<=r;i++){
                cout << cycle[i] << ' ';
            }
            exit(0);
        }
        else if(!vis[b][i]){
            par[b][i] = a;
            dfs(b,i);
        }
    }
    vis[a][b] = 2;
}
void solve(){
    int r;
    cin >> n >> r;
    vector<pair<int,int>> edges;
    for(int i = 0;i<r;i++){
        int a,b;
        cin >> a >> b;
        edge[a][b] = 1;
        edge[b][a] = 1;
        edges.push_back({a,b});
    }
    for(auto u:edges){
        if(!vis[u.first][u.second])
            dfs(u.first,u.second);
    }
    cout << "no";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef Local
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    #ifdef Local
    cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3116 KB Output is correct
2 Correct 4 ms 2772 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 9 ms 3024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1876 KB Output is correct
2 Correct 4 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 9116 KB Output is correct
2 Correct 23 ms 7396 KB Output is correct
3 Correct 130 ms 9944 KB Output is correct
4 Correct 41 ms 9560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6136 KB Output is correct
2 Correct 59 ms 7528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2768 KB Output is correct
2 Correct 61 ms 4080 KB Output is correct
3 Correct 12 ms 2568 KB Output is correct
4 Correct 272 ms 10096 KB Output is correct