CODE THANKS FESTIVAL 2018(Parallel)

Submission #10039466

Source codeソースコード

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
const long double PI = acos(-1.0L);
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

void dfs(int v, vector<vector<int>> &G, vector<bitset<300>> &H) {
    H[v][v] = 1;
    for (auto nv : G[v]) {
        dfs(nv, G, H);
        H[v] |= H[nv];
    }
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,M,K; cin >> N >> M >> K;
    K -= M;
    vector<vector<int>> G(N);
    int s;
    rep(i,N) {
        int p; cin >> p;
        p--;
        if (p < 0) {
            s = i;
            continue;
        }
        G[p].emplace_back(i);
    }

    vector<int> dist(N);
    queue<int> que;
    que.emplace(s);
    while (!que.empty()) {
        int v = que.front(); que.pop();
        for (auto nv : G[v]) {
            dist[nv] = dist[v]+1;
            que.emplace(nv);
        }
    }

    vector<bitset<300>> H(N);
    dfs(s, G, H);
    bitset<300> t;
    vector<int> ans;
    rep(i,M) {
        bool ok = false;
        rep(j,N) {
            if (t[j]) continue;
            auto u = t | H[j];
            vector<int> A;
            rep(k,N) {
                if (u[k]) continue;
                A.emplace_back(dist[k]);
            }
            int sz = A.size();
            if (sz < M-1-i) continue;
            sort(all(A));
            int mini = 0, maxi = 0;
            rep(k,M-1-i) {
                mini += A[k];
                maxi += A[sz-1-k];
            }
            if (mini <= K-dist[j] && K-dist[j] <= maxi) {
                ans.emplace_back(j+1);
                K -= dist[j];
                t |= H[j];
                ok = true;
                break;
            }
        }
        if (!ok) {
            cout << -1 << endl;
            return 0;
        }
    }

    for (auto i : ans) cout << i << " ";
    cout << endl;
}

Submission

Task問題 F - Coins on the tree
User nameユーザ名 minato_
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 400
Source lengthソースコード長 2454 Byte
File nameファイル名
Exec time実行時間 62 ms
Memory usageメモリ使用量 384 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - s1.txt,s2.txt
All 400 / 400 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

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
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 6 ms 256 KB
07.txt AC 3 ms 256 KB
08.txt AC 2 ms 256 KB
09.txt AC 4 ms 256 KB
10.txt AC 42 ms 256 KB
11.txt AC 12 ms 256 KB
12.txt AC 30 ms 256 KB
13.txt AC 46 ms 256 KB
14.txt AC 31 ms 256 KB
15.txt AC 16 ms 256 KB
16.txt AC 41 ms 256 KB
17.txt AC 5 ms 256 KB
18.txt AC 40 ms 256 KB
19.txt AC 9 ms 256 KB
20.txt AC 26 ms 256 KB
21.txt AC 11 ms 256 KB
22.txt AC 45 ms 256 KB
23.txt AC 37 ms 256 KB
24.txt AC 21 ms 256 KB
25.txt AC 40 ms 256 KB
26.txt AC 46 ms 256 KB
27.txt AC 51 ms 384 KB
28.txt AC 40 ms 256 KB
29.txt AC 62 ms 256 KB
30.txt AC 41 ms 256 KB
31.txt AC 2 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 3 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