首页 > 其他 > 详细

codeforces 962 F Simple Cycles Edges

时间:2020-02-04 14:14:40      阅读:82      评论:0      收藏:0      [点我收藏+]

求简单环,即求点=边数的点双分量,加上判断点和边的模板即可

技术分享图片
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() << "\n";
    for(int i : ans)
        cout << i << " ";
    
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    run_case();
    return 0;
}
View Code

 

You are given an undirected graph, consisting of nn vertices and mm edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).

A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn‘t allow to visit a vertex more than once in a cycle.

Determine the edges, which belong to exactly on one simple cycle.

Input

The first line contain two integers nn and mm (1n100000(1≤n≤1000000mmin(n(n1)/2,100000))0≤m≤min(n⋅(n−1)/2,100000)) — the number of vertices and the number of edges.

Each of the following mm lines contain two integers uu and vv (1u,vn1≤u,v≤nuvu≠v) — the description of the edges.

Output

In the first line print the number of edges, which belong to exactly one simple cycle.

In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.

Examples
input
Copy
3 3
1 2
2 3
3 1
output
Copy
3
1 2 3
input
Copy
6 7
2 3
3 4
4 2
1 2
1 5
5 6
6 1
output
Copy
6
1 2 3 5 6 7
input
Copy
5 6
1 2
2 3
2 4
4 3
2 5
5 3
output
Copy
0

 

codeforces 962 F Simple Cycles Edges

原文:https://www.cnblogs.com/GRedComeT/p/12259130.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!