forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
49 lines (44 loc) · 843 Bytes
/
J.cpp
File metadata and controls
49 lines (44 loc) · 843 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector < pair<int,int> > a[100100];
vector <int> ans;
int visit(int x)
{
int node = 1;
for (auto u : a[x])
{
int y = u.first, edge = u.second;
int nodeChild = visit(y);
if (nodeChild > k)
{
cout << -1 << endl;
exit(0);
}
if (nodeChild * 2 >= k) ans.push_back(edge);
else node += nodeChild;
}
return node;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
int x, y;
cin >> n >> k;
for (int i = 1; i < n; i++)
{
cin >> x >> y;
a[x].push_back({y, i});
}
if (visit(1) > k)
{
cout << -1 << endl;
return 0;
}
cout << ans.size() << endl;
for (int x : ans)
cout << x << ' ';
}