codeforces 962 F Simple Cycles Edges
Posted gredcomet
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 962 F Simple Cycles Edges相关的知识,希望对你有一定的参考价值。
求简单环,即求点=边数的点双分量,加上判断点和边的模板即可
const int maxm = 1e5+5; int head[maxm<<1], edgecnt, dfn[maxm], low[maxm], bcc_cnt, bccnum[maxm], dfs_clock, s[maxm], top; vector<int> bcc[maxm], ans; struct edge{ int u, v, nex; } edges[maxm<<1]; void addedge(int u, int v) { edges[++edgecnt].u = u; edges[edgecnt].v = v; edges[edgecnt].nex = head[u]; head[u] = edgecnt; } void tarjan(int u, int fa) { dfn[u] = low[u] = ++dfs_clock; int child = 0, v; for(int i = head[u]; i; i = edges[i].nex) { v = edges[i].v; if(v == fa) continue; if(!dfn[v]) { s[++top] = i; tarjan(v, u); low[u] = min(low[u], low[v]); if(low[v] >= dfn[u]) { bcc[++bcc_cnt].clear(); int vet = 0; while(true) { int num = s[top--]; bcc[bcc_cnt].push_back((num+1)/2); if(bccnum[edges[num].u] != bcc_cnt) { vet++; bccnum[edges[num].u]=bcc_cnt; } if(bccnum[edges[num].v] != bcc_cnt) { vet++; bccnum[edges[num].v]=bcc_cnt; } if(edges[num].u == u && edges[num].v == v) break; } if(vet == bcc[bcc_cnt].size()) for(int i = 0; i < bcc[bcc_cnt].size(); ++i) ans.push_back(bcc[bcc_cnt][i]); } } else if(dfn[v] < dfn[u]){ s[++top] = i; low[u] = min(low[u], dfn[v]); } } } void run_case() { int n, m, u, v; cin >> n >> m; for(int i = 1; i <= m; ++i) { cin >> u >> v; addedge(u, v), addedge(v, u); } for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i, -1); sort(ans.begin(), ans.end()); cout << ans.size() << " "; for(int i : ans) cout << i << " "; } int main() { ios::sync_with_stdio(false), cin.tie(0); run_case(); return 0; }
以上是关于codeforces 962 F Simple Cycles Edges的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 962D - Merge Equals
CodeForces - 665C Simple Strings (水题)
Codeforces 665C - Simple Strings