Submission #824399

# Submission time Handle Problem Language Result Execution time Memory
824399 2023-08-14T05:22:20 Z 이성호(#10360) Robogolf (ROI19_golf) C++17
0 / 100
5000 ms 27004 KB
#include <iostream>
#include <set>
#include <numeric>
#include <vector>
#define int long long
using namespace std;

int get_half(vector<int> vc)
{
    int N = (int)vc.size();
    if (N == 1) return 0;
    set<int> s1;
    for (int j = 0; j < (1 << (N / 2)); j++) {
        int tmp = 0;
        for (int k = 0; k < N / 2; k++) {
            if (j >> k & 1) tmp += vc[k];
        }
        s1.insert(tmp);
    }
    int tot = accumulate(vc.begin(), vc.end(), 0LL);
    int ret = 1e18;
    int rst = N - N / 2;
    for (int j = 0; j < (1 << rst); j++) {
        int tmp = 0;
        for (int k = 0; k < rst; k++) {
            if (j >> k & 1) tmp += vc[k+N/2];
        }
        auto it = s1.lower_bound((tot+1)/2);
        if (it != s1.end()) {
            if (abs(ret - (tot - ret)) > abs((*it) - (tot - (*it)))) {
                ret = *it;
            }
        }
        if (it != s1.begin()) {
            --it;
            if (abs(ret - (tot - ret)) > abs((*it) - (tot - (*it)))) {
                ret = *it;
            }
        }
    }
    return ret;
}

int sz[200005];
vector<int> g[200005];
int a[200005], p[200005];
int N;

void getSize(int v, int p)
{
    sz[v] = a[v];
    for (int i:g[v]) if (i != p) {
        getSize(i, v);
        sz[v] += sz[i];
    }
}

int getCent(int v, int p, int tot)
{
    for (int i:g[v]) if (i != p) {
        if (sz[i] > tot/2) return getCent(i, v, tot);
    }
    return v;
}

signed main()
{
    cin >> N;
    for (int i = 1; i <= N; i++) cin >> a[i];
    for (int i = 2; i <= N; i++) {
        int q; cin >> q;
        g[q].push_back(i); g[i].push_back(q);
    }
    getSize(1, 0);
    int cent = getCent(1, 0, sz[1]);
    vector<int> vc;
    int tot = sz[1];
    getSize(cent, 0);
    for (int i:g[cent]) vc.push_back(sz[i]);
    int x = get_half(vc);
    int ans = x * (sz[cent] - a[cent] - x);
    ans += a[cent] * (sz[cent] - a[cent]);
    for (int i = 1; i <= N; i++) ans += a[i] * (a[i] - 1);
    for (int i = 1; i <= N; i++) {
        if (i != cent) ans += a[i] * (sz[i] - a[i]);
    }
    cout << ans << '\n';
    return 0;
}

Compilation message

golf.cpp: In function 'int main()':
golf.cpp:77:9: warning: unused variable 'tot' [-Wunused-variable]
   77 |     int tot = sz[1];
      |         ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5010 ms 27004 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5010 ms 27004 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5066 ms 4948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -