目录

CF962F-Simple Cycles Edges

CF962F-Simple Cycles Edges

题目:

题目描述:

You are given an undirected graph, consisting of $ n $ vertices and $ m $ 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.

输入格式:

The first line contain two integers $ n $ and $ m $ $ (1 \le n \le 100,000 $ , $ 0 \le m \le \min(n \cdot (n - 1) / 2, 100,000)) $ — the number of vertices and the number of edges.

Each of the following $ m $ lines contain two integers $ u $ and $ v $ ( $ 1 \le u, v \le n $ , $ u \neq v $ ) — the description of the edges.

输出格式:

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.

样例:

样例输入 1:

1
2
3
4
3 3
1 2
2 3
3 1

样例输出 1:

1
2
3
1 2 3

样例输入 2:

1
2
3
4
5
6
7
8
6 7
2 3
3 4
4 2
1 2
1 5
5 6
6 1

样例输出 2:

1
2
6
1 2 3 5 6 7

样例输入 3:

1
2
3
4
5
6
7
5 6
1 2
2 3
2 4
4 3
2 5
5 3

样例输出 3:

1
0

思路:

实现:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "ybwhead/ios.h"
int n, m;
const int maxn = 1e5 + 10;
int tot = 1, head[maxn];
struct edge
{
    int v, nxt, tr;
} e[maxn << 1];
void __ADD(int u, int v)
{
    e[++tot].v = v;
    e[tot].nxt = head[u];
    head[u] = tot;
}
void add(int a, int b)
{
    __ADD(a, b);
    __ADD(b, a);
}
int ans;
int vis[maxn];
int a[maxn];
int dep[maxn];
int tof[maxn];
int f[maxn];
int sum[maxn];
void dfs(int u, int fa)
{
    vis[u] = 1;
    dep[u] = dep[fa] + 1;
    f[u] = fa;
    for (int i = head[u]; i; i = e[i].nxt)
    {
        int v = e[i].v;
        if (v == fa)
            continue;
        // cout << u << ' ' << v << endl;
        if (vis[v])
        {
            if (dep[u] > dep[v])
                sum[v]--, sum[u]++;
        }
        else
            dfs(v, u), e[i].tr = e[i ^ 1].tr = 1, tof[v] = i;
    }
}
vector<int> ans1;
void dfs1(int u, int fa, int deep)
{
    // cout << u << vis[5] << endl;
    vis[u] = 1;
    // cout << u << " " << deep << " " << sum[u] << endl;
    deep = sum[u] == 1 ? deep : dep[u] + 1;
    for (int i = head[u]; i; i = e[i].nxt)
    {
        int v = e[i].v;
        if (v == fa)
            continue;
        if (!vis[v])
            dfs1(v, u, deep);
        else
        {
            if (dep[v] + 1 >= deep && dep[u] > dep[v])
            {
                int uu = u, vv = v;
                ans1.push_back(i / 2);
                while (uu != vv)
                {
                    ans1.push_back(tof[uu] / 2);
                    // cout << uu << ' ' << v << endl;
                    uu = f[uu];
                }
            }
        }
    }
}
void dfs2(int u, int fa)
{
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt)
    {
        int v = e[i].v;
        if (v == fa)
            continue;
        if (!vis[v])
        {
            dfs2(v, u);
            sum[u] += sum[v];
        }
    }
    // cout << u << " " << sum[u] << endl;
}
int main()
{
    yin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        yin >> a >> b;
        add(a, b);
    }
    for (int i = 1; i <= n; i++)
        if (!vis[i])
            dfs(i, 0);
    memset(vis, 0, sizeof(vis));
    // cout << vis[5] << endl;
    for (int i = 1; i <= n; i++)
        if (!vis[i])
            dfs2(i, 0);
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++)
        if (!vis[i])
            dfs1(i, 0, 1);
    sort(ans1.begin(), ans1.end());
    yout << ans1.size() << endl;
    for (int i = 0; i < ans1.size(); i++)
        yout << ans1[i] << " ";
    return 0;
}