Submission #978177

# Submission time Handle Problem Language Result Execution time Memory
978177 2024-05-09T02:24:39 Z LOLOLO Traffic (IOI10_traffic) C++17
0 / 100
12 ms 23900 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 1e6 + 10;
vector <int> ed[N];
ll f[N], a[N], sum[N];
 
void dfs(int u, int v) {
    sum[u] = a[u];
    if (u != v) {
        auto it = find(all(ed[u]), v);
        ed[u].erase(it);
    }

    for (auto x : ed[u]) {
        dfs(x, u);
        sum[u] += sum[x];
        f[u] = max(f[u], sum[x]);
    }
}
 
//before -> u is v's parent
void reroot(int u, int v) {
    f[v] = max(f[v], f[u] + a[u]);
}
 
ll ans = 1e18, id = 0;
void dfs2(int u) {
    if (ans > f[u]) {
        ans = f[u];
        id = u;
    }
 
    sort(all(ed[u]), [&] (int x, int y) {return sum[x] > sum[y];});
    for (int i = 0; i < sz(ed[u]); i++) {
        int x = ed[u][i];
        ll p1 = f[u], p2 = f[x];
        if (sz(ed[u]) == 1) {
            f[u] = 0;
        } else {
            if (i == 0) {
                f[u] = sum[ed[u][1]];
            } else {
                f[u] = sum[ed[u][0]];
            }
        }
 
        reroot(u, x);
        dfs2(x);
        f[u] = p1, f[x] = p2;
    }
}
 
int LocateCentre(int n, int P[], int S[], int D[]) {
    ans = 1e18;
    for (int i = 0; i < n - 1; i++) {
        ed[S[i]].pb(D[i]);
        ed[D[i]].pb(S[i]);
    }
 
    for (int i = 0; i < n; i++)
        a[i] = P[i];
 
    dfs(0, 0);
    dfs2(0);
    return id;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23896 KB Output is correct
2 Incorrect 12 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23896 KB Output is correct
2 Incorrect 12 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23896 KB Output is correct
2 Incorrect 12 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23896 KB Output is correct
2 Incorrect 12 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -