/** * author: nok0 * created: 2020.10.12 10:46:08 **/ #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include using namespace std; #pragma region Macros #define FOR(i, l, r) for(int i = (l); i < (r); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FOR(i, 1, n + 1) #define RFOR(i, l, r) for(int i = (l); i >= (r); --i) #define RREP(i, n) RFOR(i, n - 1, 0) #define RREPS(i, n) RFOR(i, n, 1) #define pb push_back #define eb emplace_back #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template using V = vector; template using VV = V>; using ll = long long; using ld = long double; using pii = pair; using pll = pair; #define VEC(type, name, size) \ V name(size); \ IN(name) #define VVEC(type, name, h, w) \ VV name(h, V(w)); \ IN(name) #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DOUBLE(...) \ DOUBLE __VA_ARGS__; \ IN(__VA_ARGS__) #define LD(...) \ LD __VA_ARGS__; \ IN(__VA_ARGS__) void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(long double &a) { cin >> a; } void scan(char a[]) { scanf("%s", a); } void scan(string &a) { cin >> a; } template void scan(V &); template void scan(pair &); template void scan(V &a) { for(auto &i : a) scan(i); } template void scan(pair &p) { scan(p.first); scan(p.second); } template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &... tail) { scan(head); IN(tail...); } template inline void print(T x) { cout << x << '\n'; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, vector &v) { for(T &in : v) is >> in; return is; } template ostream &operator<<(ostream &os, const V &v) { REP(i, SZ(v)) { if(i) os << " "; os << v[i]; } return os; } //debug template void view(const V &v) { cerr << "{ "; for(const auto &e : v) { cerr << e << ", "; } cerr << "\b\b }"; } template void view(const VV &vv) { cerr << "{\n"; for(const auto &v : vv) { cerr << "\t"; view(v); cerr << "\n"; } cerr << "}"; } template void view(const V> &v) { cerr << "{\n"; for(const auto &c : v) cerr << "\t(" << c.first << ", " << c.second << ")\n"; cerr << "}"; } template void view(const map &m) { cerr << "{\n"; for(auto &t : m) cerr << "\t[" << t.first << "] : " << t.second << "\n"; cerr << "}"; } template void view(const pair &p) { cerr << "(" << p.first << ", " << p.second << ")"; } template void view(const set &s) { cerr << "{ "; for(auto &t : s) { view(t); cerr << ", "; } cerr << "\b\b }"; } template void view(T e) { cerr << e; } #ifdef LOCAL void debug_out() {} template void debug_out(Head H, Tail... T) { view(H); cerr << ", "; debug_out(T...); } #define debug(...) \ do { \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \ debug_out(__VA_ARGS__); \ cerr << "\b\b]\n"; \ } while(0) #else #define debug(...) (void(0)) #endif template V press(V &x) { V res = x; sort(all(res)); res.erase(unique(all(res)), res.end()); REP(i, SZ(x)) { x[i] = lower_bound(all(res), x[i]) - res.begin(); } return res; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; } inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; } inline void err(bool b = true) { if(b) { cout << -1 << '\n'; exit(0); } } template inline void fin(bool b = true, T e = 0) { if(b) { cout << e << '\n'; exit(0); } } template T divup(T x, T y) { return (x + (y - 1)) / y; } template T pow(T a, long long n, T e = 1) { T ret = e; while(n) { if(n & 1) ret *= a; a *= a; n >>= 1; } return ret; } struct iofast { iofast() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } iofast_; const int inf = 1e9; const ll INF = 1e18; #pragma endregion struct Edge { int to; long long cost; Edge() = default; Edge(int to_, long long cost_) : to(to_), cost(cost_) {} bool operator<(const Edge &a) const { return cost < a.cost; } bool operator>(const Edge &a) const { return cost > a.cost; } friend std::ostream &operator<<(std::ostream &s, Edge &a) { s << "to:" << a.to << " cost:" << a.cost; return s; } }; struct Graph : private std::vector> { using std::vector>::vector; private: const long long INF = 1e18; bool isweight = 0; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } std::vector used, order, low, articulation; std::vector bridge; int lowlinkdfs(int idx, int k, int par) { used[idx] = 1; order[idx] = k++; low[idx] = order[idx]; bool is_articulation = false, neko = false; int cnt = 0; for(auto &e : (*this)[idx]) { if(e.to == par and !exchange(neko, true)) { continue; } if(!used[e.to]) { ++cnt; k = lowlinkdfs(e.to, k, idx); low[idx] = std::min(low[idx], low[e.to]); is_articulation |= par >= 0 and low[e.to] >= order[idx]; if(order[idx] < low[e.to]) { auto p = std::minmax(idx, e.to); bridge.emplace_back(p.first, p.second); } } else { low[idx] = std::min(low[idx], order[e.to]); } } is_articulation |= par == -1 and cnt > 1; if(is_articulation) articulation.push_back(idx); return k; } public: void read(int e = -1, bool weight = 0, bool directed = false, int idx = 1) { if(e == -1) e = size() - 1; isweight |= weight; while(e--) { int u, v; long long cost = 1; std::cin >> u >> v; if(weight) std::cin >> cost; u -= idx, v -= idx; (*this)[u].emplace_back(v, cost); if(!directed) (*this)[v].emplace_back(u, cost); } } void add(int u, int v, int cost = 1, bool directed = false, int idx = 1) { u -= idx, v -= idx; (*this)[u].emplace_back(v, cost); if(!directed) (*this)[v].emplace_back(u, cost); } std::vector Dijkstra(int s) { std::vector dist(size(), INF); if(isweight) { using pll = std::pair; std::priority_queue, std::greater> que; dist[s] = 0; que.push(pll(0, s)); while(!que.empty()) { pll p = que.top(); que.pop(); int v = p.second; if(dist[v] < p.first) continue; for(int i = 0; i < (*this)[v].size(); i++) { Edge e = (*this)[v][i]; if(dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist[v] + e.cost; que.push(pll(dist[e.to], e.to)); } } } return dist; } else { std::vector dist(size(), INF); std::queue que; dist[s] = 0; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(auto nv : (*this)[v]) { if(dist[nv.to] != INF) continue; dist[nv.to] = dist[v] + 1; que.push(nv.to); } } return dist; } } std::vector BellmanFord(int s) { int n = size(); std::vector res(n, INF); res[s] = 0; for(int loop = 0; loop < n - 1; loop++) { for(int v = 0; v < n; v++) { if(res[v] == INF) continue; for(auto &e : (*this)[v]) { res[e.to] = std::min(res[e.to], res[v] + e.cost); } } } std::queue que; std::vector chk(n, 0); for(int v = 0; v < n; v++) { if(res[v] == INF) continue; for(auto &e : (*this)[v]) { if(res[e.to] > res[v] + e.cost) { res[e.to] = res[v] + e.cost; if(!chk[e.to]) { que.push(e.to); chk[e.to] = 1; } } } } while(!que.empty()) { int now = que.front(); que.pop(); for(auto &e : (*this)[now]) { if(!chk[e.to]) { chk[e.to] = 1; que.push(e.to); } } } for(int i = 0; i < n; i++) if(chk[i]) res[i] = -INF; return res; } std::vector> WarshallFloyd() { int n = size(); std::vector dist(n, std::vector(n, INF)); for(int i = 0; i < n; i++) { dist[i][i] = 0; } for(int i = 0; i < n; i++) { for(auto e : (*this)[i]) { chmin(dist[i][e.to], e.cost); } } for(int k = 0; k < n; k++) for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) chmin(dist[i][j], dist[i][k] + dist[k][j]); return dist; } //{{端点、端点},直径の大きさ} std::pair, long long> Diameter() { auto vec = Dijkstra(0); int v1 = -1, v2 = -1; long long dia = -1; for(int i = 0; i < int(size()); i++) if(chmax(dia, vec[i])) { v1 = i; } vec = Dijkstra(v1); dia = -1; for(int i = 0; i < int(size()); i++) if(chmax(dia, vec[i])) { v2 = i; } std::pair, long long> res = {{v1, v2}, dia}; return res; } long long Prim() { long long res = 0; std::priority_queue, std::greater> que; for(auto &e : (*this)[0]) que.push(e); std::vector chk(size(), 0); chk[0] = 1; while(!que.empty()) { auto e = que.top(); que.pop(); if(chk[e.to]) continue; res += e.cost; chk[e.to] = 1; for(auto &e : (*this)[e.to]) { que.push(e); } } return res; } std::pair>, std::vector> lowlink() { int n = size(), k = 0; used.assign(n, 0); order.assign(n, 0); low.assign(n, 0); for(int i = 0; i < n; i++) { if(!used[i]) k = lowlinkdfs(i, k, -1); } return std::pair(bridge, articulation); } }; int main() { INT(n); Graph G(n); map mp; V<> res(n, 1); REP(i, n) { INT(a, b); if(a > b) swap(a, b); mp[{a - 1, b - 1}] = i; G.add(a, b); } auto bridge = G.lowlink().first; for(auto p : bridge) res[mp[p]] = 0; int k = 0; REP(i, n) k += res[i]; print(k); REP(i, n) if(res[i]) cout << i + 1 << " "; cout << endl; }