Submission #52671

#TimeUsernameProblemLanguageResultExecution timeMemory
52671Alexa2001Potemkin cycle (CEOI15_indcyc)C++17
100 / 100
584 ms4228 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int Nmax = 1005; bool edge[Nmax][Nmax], S[Nmax], used[Nmax]; int comp[Nmax], t[Nmax]; int x, y, n, m, i, C; vector<int> v[Nmax], comps[Nmax]; void dfs(int node) { used[node] = 1; comp[node] = C; for(auto it : v[node]) if(!used[it]) dfs(it); } void path(int start, int finish) { memset(t, -1, sizeof(t)); t[start] = 0; queue<int> q; q.push(start); int node; while(q.size() && t[finish] == -1) { node = q.front(); q.pop(); for(auto it : v[node]) if(t[it] == -1) { if(it == finish) { t[finish] = node; break; } else if(S[it]) continue; t[it] = node; q.push(it); } } assert(t[finish] != -1); while(finish) { cout << finish << ' '; finish = t[finish]; } } void solve() { int node, i; for(node = 1; node <= n; ++node) { memset(used, 0, sizeof(used)); memset(S, 0, sizeof(S)); memset(comp, 0, sizeof(comp)); for(auto it : v[node]) used[it] = 1, S[it] = 1; used[node] = 1, S[node] = 1; C = 0; for(i=1; i<=n; ++i) if(!used[i]) { ++C; dfs(i); } for(auto x : v[node]) { comps[x].clear(); for(auto y : v[x]) if(!S[y]) comps[x].push_back(comp[y]); sort(comps[x].begin(), comps[x].end()); comps[x].erase( unique(comps[x].begin(), comps[x].end()), comps[x].end() ); } for(auto x : v[node]) for(auto y : v[node]) if(x < y && !edge[x][y]) { if(comps[x].size() > comps[y].size()) swap(x, y); for(auto val : comps[x]) if(binary_search(comps[y].begin(), comps[y].end(), val)) { path(x, y); cout << node << '\n'; return; } } } cout << "no\n"; } int main() { // freopen("input", "r", stdin); // freopen("output", "w", stdout); cin.sync_with_stdio(false); cin >> n >> m; for(i=1; i<=m; ++i) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); edge[x][y] = edge[y][x] = 1; } solve(); 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...