Submission #959756

# Submission time Handle Problem Language Result Execution time Memory
959756 2024-04-09T03:45:08 Z Tuanlinh123 Designated Cities (JOI19_designated_cities) C++17
100 / 100
325 ms 77536 KB
#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;

struct edge
{
    ll v, c, d;
    edge(){}
    edge(ll v, ll c, ll d): v(v), c(c), d(d){}
};

const ll maxn=200005;
priority_queue <ll> q[maxn];
ll ans[maxn], C[maxn], D[maxn];
pair<pll, pll> dp[maxn];
vector <edge> A[maxn];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n, sum=0, allup=0, root=0, crm=0; cin >> n;
    for (ll i=1; i<n; i++)
    {
        ll u, v, c, d; cin >> u >> v >> c >> d;
        A[u].pb({v, c, d}), A[v].pb({u, d, c});
        sum+=c, sum+=d;
    }
    function <void(ll, ll)> dfs=[&](ll u, ll pa)
    {
        ans[1]=max(ans[1], C[u]-D[u]), dp[u].fi={0, u};
        for (auto [v, c, d]:A[u])
        {
            if (v==pa) continue;
            C[v]=C[u]+c, D[v]=D[u]+d, allup+=d, dfs(v, u);
            pll val={dp[v].fi.fi+c, dp[v].fi.se};
            if (val>dp[u].fi) dp[u].se=dp[u].fi, dp[u].fi=val;
            else if (val>dp[u].se) dp[u].se=val;
        }
        ll val=C[u]-D[u]+dp[u].fi.fi+dp[u].fi.se;
        if (val>crm) root=dp[u].fi.se, crm=val;
    }; dfs(1, 1), ans[1]=sum-(ans[1]+allup), allup=0;
    function <void(ll, ll)> dfs2=[&](ll u, ll pa)
    {   
        vector <ll> num;
        for (auto [v, c, d]:A[u])
        {
            if (v==pa) continue;
            C[v]=C[u]+c, allup+=d;
            dfs2(v, u), num.pb(q[v].top()), q[v].pop();
        }
        if (!sz(num)) {q[u].push(C[u]); return;}
        sort(num.begin(), num.end(), greater<ll>());
        for (ll i=0; i<sz(num); i++)
            q[u].push(i?num[i]-C[u]:num[i]);
        for (auto [v, c, d]:A[u])
            if (v!=pa)
            {
                if (sz(q[u])<sz(q[v])) swap(q[u], q[v]);
                while (sz(q[v])) q[u].push(q[v].top()), q[v].pop();
            }
    }; C[root]=0, dfs2(root, root);
    for (ll i=2, s=0; i<=n; i++)
    {
        if (q[root].empty()) {ans[i]=ans[i-1]; continue;}
        s+=q[root].top(), q[root].pop();
        ans[i]=sum-(allup+s);
    }
    ll q; cin >> q;
    while (q--) {ll x; cin >> x; cout << ans[x] << "\n";}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 3 ms 14164 KB Output is correct
4 Correct 3 ms 14172 KB Output is correct
5 Correct 3 ms 14172 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 3 ms 14420 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14172 KB Output is correct
2 Correct 264 ms 51400 KB Output is correct
3 Correct 325 ms 75824 KB Output is correct
4 Correct 282 ms 50268 KB Output is correct
5 Correct 215 ms 50232 KB Output is correct
6 Correct 280 ms 52928 KB Output is correct
7 Correct 238 ms 49920 KB Output is correct
8 Correct 320 ms 74496 KB Output is correct
9 Correct 188 ms 51224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14172 KB Output is correct
2 Correct 310 ms 52044 KB Output is correct
3 Correct 266 ms 75600 KB Output is correct
4 Correct 240 ms 50116 KB Output is correct
5 Correct 235 ms 50448 KB Output is correct
6 Correct 247 ms 53860 KB Output is correct
7 Correct 173 ms 51308 KB Output is correct
8 Correct 272 ms 65988 KB Output is correct
9 Correct 173 ms 52352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 3 ms 14164 KB Output is correct
4 Correct 3 ms 14172 KB Output is correct
5 Correct 3 ms 14172 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 3 ms 14420 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 3 ms 14164 KB Output is correct
13 Correct 6 ms 14424 KB Output is correct
14 Correct 5 ms 14680 KB Output is correct
15 Correct 6 ms 14424 KB Output is correct
16 Correct 5 ms 14428 KB Output is correct
17 Correct 5 ms 14428 KB Output is correct
18 Correct 5 ms 14340 KB Output is correct
19 Correct 5 ms 14444 KB Output is correct
20 Correct 5 ms 14428 KB Output is correct
21 Correct 5 ms 14428 KB Output is correct
22 Correct 5 ms 14424 KB Output is correct
23 Correct 6 ms 14428 KB Output is correct
24 Correct 5 ms 14428 KB Output is correct
25 Correct 5 ms 14684 KB Output is correct
26 Correct 5 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14172 KB Output is correct
2 Correct 264 ms 51400 KB Output is correct
3 Correct 325 ms 75824 KB Output is correct
4 Correct 282 ms 50268 KB Output is correct
5 Correct 215 ms 50232 KB Output is correct
6 Correct 280 ms 52928 KB Output is correct
7 Correct 238 ms 49920 KB Output is correct
8 Correct 320 ms 74496 KB Output is correct
9 Correct 188 ms 51224 KB Output is correct
10 Correct 4 ms 14172 KB Output is correct
11 Correct 310 ms 52044 KB Output is correct
12 Correct 266 ms 75600 KB Output is correct
13 Correct 240 ms 50116 KB Output is correct
14 Correct 235 ms 50448 KB Output is correct
15 Correct 247 ms 53860 KB Output is correct
16 Correct 173 ms 51308 KB Output is correct
17 Correct 272 ms 65988 KB Output is correct
18 Correct 173 ms 52352 KB Output is correct
19 Correct 3 ms 14168 KB Output is correct
20 Correct 257 ms 51616 KB Output is correct
21 Correct 280 ms 75600 KB Output is correct
22 Correct 259 ms 50372 KB Output is correct
23 Correct 248 ms 51356 KB Output is correct
24 Correct 248 ms 50632 KB Output is correct
25 Correct 288 ms 51656 KB Output is correct
26 Correct 237 ms 50372 KB Output is correct
27 Correct 238 ms 49820 KB Output is correct
28 Correct 249 ms 53240 KB Output is correct
29 Correct 277 ms 51688 KB Output is correct
30 Correct 265 ms 51148 KB Output is correct
31 Correct 189 ms 50904 KB Output is correct
32 Correct 305 ms 66812 KB Output is correct
33 Correct 182 ms 51188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14168 KB Output is correct
2 Correct 4 ms 14168 KB Output is correct
3 Correct 3 ms 14164 KB Output is correct
4 Correct 3 ms 14172 KB Output is correct
5 Correct 3 ms 14172 KB Output is correct
6 Correct 4 ms 14172 KB Output is correct
7 Correct 3 ms 14420 KB Output is correct
8 Correct 3 ms 14172 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 3 ms 14172 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 3 ms 14172 KB Output is correct
13 Correct 264 ms 51400 KB Output is correct
14 Correct 325 ms 75824 KB Output is correct
15 Correct 282 ms 50268 KB Output is correct
16 Correct 215 ms 50232 KB Output is correct
17 Correct 280 ms 52928 KB Output is correct
18 Correct 238 ms 49920 KB Output is correct
19 Correct 320 ms 74496 KB Output is correct
20 Correct 188 ms 51224 KB Output is correct
21 Correct 4 ms 14172 KB Output is correct
22 Correct 310 ms 52044 KB Output is correct
23 Correct 266 ms 75600 KB Output is correct
24 Correct 240 ms 50116 KB Output is correct
25 Correct 235 ms 50448 KB Output is correct
26 Correct 247 ms 53860 KB Output is correct
27 Correct 173 ms 51308 KB Output is correct
28 Correct 272 ms 65988 KB Output is correct
29 Correct 173 ms 52352 KB Output is correct
30 Correct 3 ms 14164 KB Output is correct
31 Correct 6 ms 14424 KB Output is correct
32 Correct 5 ms 14680 KB Output is correct
33 Correct 6 ms 14424 KB Output is correct
34 Correct 5 ms 14428 KB Output is correct
35 Correct 5 ms 14428 KB Output is correct
36 Correct 5 ms 14340 KB Output is correct
37 Correct 5 ms 14444 KB Output is correct
38 Correct 5 ms 14428 KB Output is correct
39 Correct 5 ms 14428 KB Output is correct
40 Correct 5 ms 14424 KB Output is correct
41 Correct 6 ms 14428 KB Output is correct
42 Correct 5 ms 14428 KB Output is correct
43 Correct 5 ms 14684 KB Output is correct
44 Correct 5 ms 14428 KB Output is correct
45 Correct 3 ms 14168 KB Output is correct
46 Correct 257 ms 51616 KB Output is correct
47 Correct 280 ms 75600 KB Output is correct
48 Correct 259 ms 50372 KB Output is correct
49 Correct 248 ms 51356 KB Output is correct
50 Correct 248 ms 50632 KB Output is correct
51 Correct 288 ms 51656 KB Output is correct
52 Correct 237 ms 50372 KB Output is correct
53 Correct 238 ms 49820 KB Output is correct
54 Correct 249 ms 53240 KB Output is correct
55 Correct 277 ms 51688 KB Output is correct
56 Correct 265 ms 51148 KB Output is correct
57 Correct 189 ms 50904 KB Output is correct
58 Correct 305 ms 66812 KB Output is correct
59 Correct 182 ms 51188 KB Output is correct
60 Correct 3 ms 14172 KB Output is correct
61 Correct 294 ms 54516 KB Output is correct
62 Correct 304 ms 77536 KB Output is correct
63 Correct 302 ms 53076 KB Output is correct
64 Correct 281 ms 54472 KB Output is correct
65 Correct 275 ms 52972 KB Output is correct
66 Correct 276 ms 54208 KB Output is correct
67 Correct 325 ms 53188 KB Output is correct
68 Correct 258 ms 52800 KB Output is correct
69 Correct 269 ms 55908 KB Output is correct
70 Correct 295 ms 53992 KB Output is correct
71 Correct 253 ms 53760 KB Output is correct
72 Correct 219 ms 54004 KB Output is correct
73 Correct 311 ms 68768 KB Output is correct
74 Correct 209 ms 55944 KB Output is correct