Submission #658096

#TimeUsernameProblemLanguageResultExecution timeMemory
658096FatihSolakPotemkin cycle (CEOI15_indcyc)C++17
100 / 100
272 ms10096 KiB
#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 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...