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
| #include <bits/stdc++.h>
using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define all(a) (a).begin(),(a).end() #define pb(x) push_back(x) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define caseT int _T;scanf("%d",&_T);for (int q=1; q<=_T; ++q) typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 1e5 + 7; struct edge { int to, nxt, id; edge() {} edge(int _to, int _nxt, int _id): to(_to), nxt(_nxt), id(_id) {} } E[N << 1]; int head[N], tot; int dfn[N], low[N], st[N], top, ts, ins[N], pbc_cnt, road[N][2]; stack<int>est; vector<int>pbc_edge[N]; set<int>p;
void init() { CLR(head, -1); tot = 0; } inline void add(int s, int t, int id) { E[tot] = edge(t, head[s], id); head[s] = tot++; } void tarjan(int u, int f) { dfn[u] = low[u] = ++ts; ins[u] = 1; st[top++] = u; int v; for (int i = head[u]; ~i; i = E[i].nxt) { v = E[i].to; if (!dfn[v]) { est.push(i); tarjan(v, u); low[u] = min(low[u], low[v]); if (low[v] >= dfn[u]) { ++pbc_cnt; int eid; do { eid = est.top(); est.pop(); pbc_edge[pbc_cnt].pb(E[eid].id); } while (E[eid].id != E[i].id); } } else if (dfn[v] < dfn[u] && v != f) { est.push(i); low[u] = min(low[u], dfn[v]); } } } int main(void) { int n, m, a, b, i; scanf("%d%d", &n, &m); init(); for (i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); add(a, b, i); add(b, a, i); road[i][0] = a, road[i][1] = b; } for (i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i, -1); vector<int>ans; for (i = 1; i <= pbc_cnt; ++i) { p.clear(); for (auto &eid : pbc_edge[i]) { p.insert(road[eid][0]); p.insert(road[eid][1]); } if (pbc_edge[i].size() == p.size()) ans.insert(ans.end(), all(pbc_edge[i])); } sort(all(ans)); int sz = ans.size(); printf("%d\n", sz); for (i = 0; i < sz; ++i) printf("%d%c", ans[i], " \n"[i == sz - 1]); return 0; }
|