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;
}
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的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 962D - Merge Equals

codeforces 962D Merge Equals

CodeForces - 665C Simple Strings (水题)

Codeforces 665C - Simple Strings

codeforces1276AAs Simple as One and Two

CF962D Merge Equals