Submission #6736353


Source Code Expand

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;
constexpr double pi = acos(-1);
constexpr double EPS = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const int MAX_V = 300;

vector<int> depth(MAX_V);
vector<vector<int>> G(MAX_V);

void calc_depth(int cv, int dep){
    depth[cv] = dep;
    for(auto nv : G[cv]){
        calc_depth(nv, dep + 1);
    }

    return ;
}

vector<int> tmp_depth;
vector<bool> used(MAX_V);

void can_reach(int cv, int dep){
    if(used[cv]) return ;

    tmp_depth.emplace_back(dep);
    for(auto nv : G[cv]){
        can_reach(nv, dep + 1);
    }

    return ;
}

void go_to_child(int cv){
    used[cv] = true;

    for(auto nv : G[cv]){
        go_to_child(nv);
    }

    return ;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m, k; cin>>n>>m>>k;
    int root;
    for(int i=0; i<n; i++){
        int p; cin>>p;
        p--;
        if(p == -1){
            root = i;
            continue;
        }
        G[p].emplace_back(i);
    }

    calc_depth(root, 1);

    vector<int> ans;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            if(!used[j]){
                used[j] = true;

                tmp_depth.clear();
                can_reach(root, 1);

                int nxtneed = m - i - 1;

                if(tmp_depth.size() < nxtneed){
                    used[j] = false;
                    continue;
                }

                sort(tmp_depth.begin(), tmp_depth.end());

                int nxtmin = 0;
                for(int l=0; l<nxtneed; l++){
                    nxtmin += tmp_depth[l];
                }

                reverse(tmp_depth.begin(), tmp_depth.end());
                int nxtmax = 0;
                for(int l=0; l<nxtneed; l++){
                    nxtmax += tmp_depth[l];
                }

                if(nxtmin <= k - depth[j] && k - depth[j] <= nxtmax){
                    ans.emplace_back(j);
                    k -= depth[j];
                    go_to_child(j);
                    break;
                }
                else{
                    used[j] = false;
                }
            }
        }
    }

    if(ans.size() != m){
        cout << -1 << endl;
    }
    else{
        for(int i=0; i<m; i++){
            cout << ans[i] + 1 << " \n"[i == m-1];
        }
    }
}

Submission Info

Submission Time
Task F - Coins on the tree
User rodea0952
Language C++14 (GCC 5.4.1)
Score 400
Code Size 3266 Byte
Status AC
Exec Time 51 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 5 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 37 ms 256 KB
11.txt AC 11 ms 256 KB
12.txt AC 26 ms 256 KB
13.txt AC 39 ms 256 KB
14.txt AC 25 ms 256 KB
15.txt AC 14 ms 256 KB
16.txt AC 36 ms 256 KB
17.txt AC 4 ms 256 KB
18.txt AC 35 ms 256 KB
19.txt AC 7 ms 256 KB
20.txt AC 20 ms 256 KB
21.txt AC 9 ms 256 KB
22.txt AC 38 ms 256 KB
23.txt AC 30 ms 256 KB
24.txt AC 16 ms 256 KB
25.txt AC 34 ms 256 KB
26.txt AC 40 ms 256 KB
27.txt AC 43 ms 256 KB
28.txt AC 35 ms 256 KB
29.txt AC 51 ms 256 KB
30.txt AC 33 ms 256 KB
31.txt AC 2 ms 256 KB
32.txt AC 2 ms 256 KB
33.txt AC 1 ms 256 KB
34.txt AC 1 ms 256 KB
35.txt AC 8 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