Submission #4362488
Source Code Expand
#include<bits/stdc++.h> using namespace std; const int IINF = 1e9 + 100; int n, m, k, root; vector<int> p, dis, dis_cnt, ans; vector< vector<int> > g; vector<bool> used; void calc_depth(int v, int depth){ dis[v] = depth; ++dis_cnt[depth]; for(auto x : g[v]){ if(dis[x] > depth + 1){ calc_depth(x, depth + 1); } } return; } bool ancestor(int v){ if(used[v]){ return false; } if(v == root){ return true; } return ancestor(p[v]); } void descendant(int v, bool mode){ if(used[v]){ return; } if(mode){ --dis_cnt[dis[v]]; }else{ ++dis_cnt[dis[v]]; } for(auto x : g[v]){ descendant(x, mode); } return; } bool check(int node, int num, int ope){ if(used[node] || ope + dis[node] > k || !ancestor(node)){ return false; } descendant(node, true); int node_cnt = 0; for(int i = 0 ; i <= n ; ++i){ node_cnt += dis_cnt[i]; } if(node_cnt < m - num - 1){ descendant(node, false); return false; } int ope_min = 0, ope_max = 0; node_cnt = m - num - 1; for(int i = 0 ; i <= n ; ++i){ ope_min += i * min(dis_cnt[i], max(node_cnt, 0)); node_cnt = max(node_cnt - dis_cnt[i], 0); } node_cnt = m - num - 1; for(int i = n ; i >= 0 ; --i){ ope_max += i * min(dis_cnt[i], max(node_cnt, 0)); node_cnt = max(node_cnt - dis_cnt[i], 0); } if(ope_min <= k - ope - dis[node] && k - ope - dis[node] <= ope_max){ return true; }else{ descendant(node, false); return false; } } bool solve(int num, int ope){ if(num == m){ return ope == k; } for(int i = 0 ; i < n ; ++i){ if(check(i, num, ope)){ ans.push_back(i); used[i] = true; if(solve(num + 1, ope + dis[i])){ return true; } ans.pop_back(); used[i] = false; descendant(i, false); } } return false; } int main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m >> k; p.resize(n); dis.resize(n, IINF); dis_cnt.resize(n + 1); g.resize(n); used.resize(n); for(int i = 0 ; i < n ; ++i){ cin >> p[i]; if(p[i] == 0){ root = i; continue; } --p[i]; g[p[i]].push_back(i); } calc_depth(root, 1); if(solve(0, 0)){ for(int i = 0 ; i < m ; ++i){ cout << ans[i] + 1; if(i < m - 1){ cout << " "; }else{ cout << endl; } } }else{ cout << -1 << endl; } return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Coins on the tree |
User | mhrb |
Language | C++14 (GCC 5.4.1) |
Score | 400 |
Code Size | 2908 Byte |
Status | AC |
Exec Time | 21 ms |
Memory | 256 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 400 / 400 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | s1.txt, s2.txt |
All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, s1.txt, s2.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01.txt | AC | 1 ms | 256 KB |
02.txt | AC | 1 ms | 256 KB |
03.txt | AC | 1 ms | 256 KB |
04.txt | AC | 1 ms | 256 KB |
05.txt | AC | 1 ms | 256 KB |
06.txt | AC | 3 ms | 256 KB |
07.txt | AC | 2 ms | 256 KB |
08.txt | AC | 2 ms | 256 KB |
09.txt | AC | 3 ms | 256 KB |
10.txt | AC | 15 ms | 256 KB |
11.txt | AC | 4 ms | 256 KB |
12.txt | AC | 10 ms | 256 KB |
13.txt | AC | 15 ms | 256 KB |
14.txt | AC | 11 ms | 256 KB |
15.txt | AC | 5 ms | 256 KB |
16.txt | AC | 15 ms | 256 KB |
17.txt | AC | 4 ms | 256 KB |
18.txt | AC | 16 ms | 256 KB |
19.txt | AC | 4 ms | 256 KB |
20.txt | AC | 12 ms | 256 KB |
21.txt | AC | 6 ms | 256 KB |
22.txt | AC | 17 ms | 256 KB |
23.txt | AC | 15 ms | 256 KB |
24.txt | AC | 8 ms | 256 KB |
25.txt | AC | 16 ms | 256 KB |
26.txt | AC | 17 ms | 256 KB |
27.txt | AC | 17 ms | 256 KB |
28.txt | AC | 16 ms | 256 KB |
29.txt | AC | 21 ms | 256 KB |
30.txt | AC | 15 ms | 256 KB |
31.txt | AC | 1 ms | 256 KB |
32.txt | AC | 1 ms | 256 KB |
33.txt | AC | 1 ms | 256 KB |
34.txt | AC | 1 ms | 256 KB |
35.txt | AC | 2 ms | 256 KB |
36.txt | AC | 2 ms | 256 KB |
37.txt | AC | 1 ms | 256 KB |
38.txt | AC | 1 ms | 256 KB |
39.txt | AC | 1 ms | 256 KB |
40.txt | AC | 2 ms | 256 KB |
s1.txt | AC | 1 ms | 256 KB |
s2.txt | AC | 1 ms | 256 KB |