Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include <iostream>#include <vector>#include <map>#include <algorithm>using namespace std;bool isValid(pair<int, int> node, int idx) {if (idx == 1) return node.first > 0 && node.second == 0;else return node.first >= 0 && node.second == 1;}int main(){int n;cin >> n;vector<pair<int, int> > node(n + 1, make_pair(0, 0));vector<pair<int, int> > line(n - 1);int a, b;for (int i = 1; i < n; i++) {cin >> a >> b;node[a] = make_pair(node[a].first + 1, node[a].second);node[b] = make_pair(node[b].first, node[b].second + 1);line[i - 1] = make_pair(a, b);}for (int i = 1; i < n; i++) {a = line[i - 1].first;b = line[i - 1].second;if (isValid(make_pair(node[a].first - 1, node[a].second), a) &&isValid(make_pair(node[b].first, node[b].second - 1), b)) {cout << i << " ";}