Submission #4346688


Source Code Expand

#include<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 100;
const long long LLINF = 2e18 + 129;
const long long MOD = 1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

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;
    rep(i, n + 1){
        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;
    rep(i, n + 1){
        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;
    repr(i, n + 1){
        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;
    }
    rep(i, n){
        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);

    rep(i, n){
        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)){
        rep(i, m){
            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 3445 Byte
Status AC
Exec Time 20 ms
Memory 256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 42
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 2 ms 256 KB
07.txt AC 2 ms 256 KB
08.txt AC 1 ms 256 KB
09.txt AC 2 ms 256 KB
10.txt AC 14 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 16 ms 256 KB
23.txt AC 15 ms 256 KB
24.txt AC 7 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 20 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 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB