Submission #223986

# Submission time Handle Problem Language Result Execution time Memory
223986 2020-04-17T00:24:16 Z osaaateiasavtnl Designated Cities (JOI19_designated_cities) C++14
100 / 100
529 ms 59112 KB
#include<bits/stdc++.h>
using namespace std;
 
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
 
const int N = 2e5 + 7;
const ll INF = 1e18;
int n;
vector <ii> g[N];
vector <ii> tree[N];
void build(int u, int p) {  
    tree[u].clear();
    for (auto e : g[u]) {
        int v = e.f, c = e.s;
        if (v != p) {
            tree[u].app(mp(v, c));
            build(v, u);
        }       
    }   
}
int to[N];
ll h[N];
void ladder(int u, ll cur, ll &sum) {
    h[u] = cur;
    to[u] = -1;
    for (auto e : tree[u]) {
        ladder(e.f, cur + e.s, sum);
        sum += e.s;
        h[u] = max(h[u], h[e.f]);
        if (to[u] == -1 || h[to[u]] < h[e.f])
            to[u] = e.f;
    }   
}   
void print(int u, int ded, ll cur, vector <pair <ll, int> > &a, bool root) {
    if (tree[u].empty())
        a.app(mp(cur, ded));
    else
        a.app(mp(0, ded));
                    
    for (auto e : tree[u]) {
        int d = ded;
        if (root) 
            d = e.f;
 
        ll c = cur;
        if (to[u] != e.f)
            c = 0;
        c += e.s;            
 
        print(e.f, d, c, a, 0);
    }   
}   
 
ll ans[N];
vector <pair <ll, int> > a;
void solve(int c) {    
    build(c, c);
    ll sum = 0;
    ladder(c, 0, sum);
    a.clear();
    print(c, c, 0, a, 1);
    sort(all(a)); reverse(all(a));
 
    ans[1] = min(ans[1], sum);
 
    int l = -1;
    for (int i = 1; i < a.size(); ++i) {
        if (a[i].s != a[0].s) {
            l = i;
            break;
        }   
    }   
    sum -= a[0].f;
    for (int i = 1; i < a.size(); ++i) {
        sum -= a[i].f;
        if (l <= i) {
            ans[i + 1] = min(ans[i + 1], sum);
        }       
        else {
            ans[i + 1] = min(ans[i + 1], sum + a[i].f - a[l].f);
        }   
    }   
}
 
namespace Root {
ll sum[N];
void dfs1(int u, int p) {
    for (auto e : g[u]) {
        int v = e.f, c = e.s;
        if (v != p) {
            dfs1(v, u);
            sum[u] += sum[v] + c;
        }   
    }   
}   
ll diam = INF;
int root = 1;
pair <ll, int> h[N];
void dfs2(int u, int p, ll up) {
    for (auto e : g[u]) {
        int v = e.f, c = e.s;
        if (v == p) {
            up += c;
        }   
    }   
    ans[1] = min(ans[1], up + sum[u]);
    h[u] = mp(0, u);
    for (auto e : g[u]) {
        int v = e.f, c = e.s;
        if (v != p) {
            dfs2(v, u, up + sum[u] - sum[v] - c);
            auto t = h[v];
            t.f += c;
                        
            ll nn = up + sum[u] - h[u].f - t.f;
            if (nn < diam) {
                diam = nn;
                root = t.s;
            }   

            h[u] = max(h[u], t);
        }   
    }   
}
int getroot() {
    dfs1(1, 1);
    dfs2(1, 1, 0);
    return root;
} 
};
 
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v, a, b;
        cin >> u >> v >> a >> b;
        g[u].app(mp(v, a));
        g[v].app(mp(u, b));
    }   
    for (int i = 0; i < N; ++i)
        ans[i] = INF;
 
    int root = Root::getroot();    
    solve(root);

    #ifdef HOME
    cout << "root " << root << endl;
    #endif
       
    int q;
    cin >> q;
    while (q--) {
        int k;
        cin >> k;
        cout << ans[k] << endl;
    }   
    
    #ifdef HOME
    cerr << Time << endl;
    #endif
}

Compilation message

designated_cities.cpp: In function 'void solve(int)':
designated_cities.cpp:75:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < a.size(); ++i) {
                     ~~^~~~~~~~~~
designated_cities.cpp:82:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < a.size(); ++i) {
                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 11392 KB Output is correct
2 Correct 10 ms 11264 KB Output is correct
3 Correct 11 ms 11392 KB Output is correct
4 Correct 10 ms 11264 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 9 ms 11264 KB Output is correct
7 Correct 11 ms 11264 KB Output is correct
8 Correct 10 ms 11264 KB Output is correct
9 Correct 10 ms 11264 KB Output is correct
10 Correct 13 ms 11264 KB Output is correct
11 Correct 11 ms 11264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 11264 KB Output is correct
2 Correct 314 ms 33984 KB Output is correct
3 Correct 377 ms 56152 KB Output is correct
4 Correct 438 ms 34008 KB Output is correct
5 Correct 322 ms 33764 KB Output is correct
6 Correct 370 ms 36720 KB Output is correct
7 Correct 255 ms 33128 KB Output is correct
8 Correct 362 ms 55784 KB Output is correct
9 Correct 206 ms 30436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 11264 KB Output is correct
2 Correct 316 ms 34032 KB Output is correct
3 Correct 375 ms 58928 KB Output is correct
4 Correct 305 ms 34156 KB Output is correct
5 Correct 339 ms 34280 KB Output is correct
6 Correct 326 ms 37484 KB Output is correct
7 Correct 210 ms 32324 KB Output is correct
8 Correct 355 ms 47084 KB Output is correct
9 Correct 242 ms 30568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 11392 KB Output is correct
2 Correct 10 ms 11264 KB Output is correct
3 Correct 11 ms 11392 KB Output is correct
4 Correct 10 ms 11264 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 9 ms 11264 KB Output is correct
7 Correct 11 ms 11264 KB Output is correct
8 Correct 10 ms 11264 KB Output is correct
9 Correct 10 ms 11264 KB Output is correct
10 Correct 13 ms 11264 KB Output is correct
11 Correct 11 ms 11264 KB Output is correct
12 Correct 10 ms 11264 KB Output is correct
13 Correct 12 ms 11520 KB Output is correct
14 Correct 12 ms 11776 KB Output is correct
15 Correct 13 ms 11520 KB Output is correct
16 Correct 12 ms 11648 KB Output is correct
17 Correct 14 ms 11648 KB Output is correct
18 Correct 12 ms 11648 KB Output is correct
19 Correct 12 ms 11520 KB Output is correct
20 Correct 12 ms 11648 KB Output is correct
21 Correct 12 ms 11648 KB Output is correct
22 Correct 12 ms 11648 KB Output is correct
23 Correct 12 ms 11648 KB Output is correct
24 Correct 12 ms 11520 KB Output is correct
25 Correct 13 ms 11776 KB Output is correct
26 Correct 12 ms 11520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 11264 KB Output is correct
2 Correct 314 ms 33984 KB Output is correct
3 Correct 377 ms 56152 KB Output is correct
4 Correct 438 ms 34008 KB Output is correct
5 Correct 322 ms 33764 KB Output is correct
6 Correct 370 ms 36720 KB Output is correct
7 Correct 255 ms 33128 KB Output is correct
8 Correct 362 ms 55784 KB Output is correct
9 Correct 206 ms 30436 KB Output is correct
10 Correct 10 ms 11264 KB Output is correct
11 Correct 316 ms 34032 KB Output is correct
12 Correct 375 ms 58928 KB Output is correct
13 Correct 305 ms 34156 KB Output is correct
14 Correct 339 ms 34280 KB Output is correct
15 Correct 326 ms 37484 KB Output is correct
16 Correct 210 ms 32324 KB Output is correct
17 Correct 355 ms 47084 KB Output is correct
18 Correct 242 ms 30568 KB Output is correct
19 Correct 11 ms 11392 KB Output is correct
20 Correct 467 ms 34028 KB Output is correct
21 Correct 529 ms 59112 KB Output is correct
22 Correct 308 ms 34028 KB Output is correct
23 Correct 314 ms 34028 KB Output is correct
24 Correct 308 ms 33952 KB Output is correct
25 Correct 379 ms 34004 KB Output is correct
26 Correct 318 ms 33972 KB Output is correct
27 Correct 300 ms 33768 KB Output is correct
28 Correct 324 ms 36844 KB Output is correct
29 Correct 322 ms 34404 KB Output is correct
30 Correct 289 ms 33896 KB Output is correct
31 Correct 244 ms 32868 KB Output is correct
32 Correct 358 ms 48108 KB Output is correct
33 Correct 199 ms 30432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 11392 KB Output is correct
2 Correct 10 ms 11264 KB Output is correct
3 Correct 11 ms 11392 KB Output is correct
4 Correct 10 ms 11264 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 9 ms 11264 KB Output is correct
7 Correct 11 ms 11264 KB Output is correct
8 Correct 10 ms 11264 KB Output is correct
9 Correct 10 ms 11264 KB Output is correct
10 Correct 13 ms 11264 KB Output is correct
11 Correct 11 ms 11264 KB Output is correct
12 Correct 11 ms 11264 KB Output is correct
13 Correct 314 ms 33984 KB Output is correct
14 Correct 377 ms 56152 KB Output is correct
15 Correct 438 ms 34008 KB Output is correct
16 Correct 322 ms 33764 KB Output is correct
17 Correct 370 ms 36720 KB Output is correct
18 Correct 255 ms 33128 KB Output is correct
19 Correct 362 ms 55784 KB Output is correct
20 Correct 206 ms 30436 KB Output is correct
21 Correct 10 ms 11264 KB Output is correct
22 Correct 316 ms 34032 KB Output is correct
23 Correct 375 ms 58928 KB Output is correct
24 Correct 305 ms 34156 KB Output is correct
25 Correct 339 ms 34280 KB Output is correct
26 Correct 326 ms 37484 KB Output is correct
27 Correct 210 ms 32324 KB Output is correct
28 Correct 355 ms 47084 KB Output is correct
29 Correct 242 ms 30568 KB Output is correct
30 Correct 10 ms 11264 KB Output is correct
31 Correct 12 ms 11520 KB Output is correct
32 Correct 12 ms 11776 KB Output is correct
33 Correct 13 ms 11520 KB Output is correct
34 Correct 12 ms 11648 KB Output is correct
35 Correct 14 ms 11648 KB Output is correct
36 Correct 12 ms 11648 KB Output is correct
37 Correct 12 ms 11520 KB Output is correct
38 Correct 12 ms 11648 KB Output is correct
39 Correct 12 ms 11648 KB Output is correct
40 Correct 12 ms 11648 KB Output is correct
41 Correct 12 ms 11648 KB Output is correct
42 Correct 12 ms 11520 KB Output is correct
43 Correct 13 ms 11776 KB Output is correct
44 Correct 12 ms 11520 KB Output is correct
45 Correct 11 ms 11392 KB Output is correct
46 Correct 467 ms 34028 KB Output is correct
47 Correct 529 ms 59112 KB Output is correct
48 Correct 308 ms 34028 KB Output is correct
49 Correct 314 ms 34028 KB Output is correct
50 Correct 308 ms 33952 KB Output is correct
51 Correct 379 ms 34004 KB Output is correct
52 Correct 318 ms 33972 KB Output is correct
53 Correct 300 ms 33768 KB Output is correct
54 Correct 324 ms 36844 KB Output is correct
55 Correct 322 ms 34404 KB Output is correct
56 Correct 289 ms 33896 KB Output is correct
57 Correct 244 ms 32868 KB Output is correct
58 Correct 358 ms 48108 KB Output is correct
59 Correct 199 ms 30432 KB Output is correct
60 Correct 10 ms 11264 KB Output is correct
61 Correct 365 ms 34528 KB Output is correct
62 Correct 403 ms 56544 KB Output is correct
63 Correct 358 ms 34528 KB Output is correct
64 Correct 372 ms 34404 KB Output is correct
65 Correct 460 ms 34144 KB Output is correct
66 Correct 353 ms 34528 KB Output is correct
67 Correct 335 ms 34176 KB Output is correct
68 Correct 354 ms 34400 KB Output is correct
69 Correct 371 ms 37344 KB Output is correct
70 Correct 361 ms 34952 KB Output is correct
71 Correct 331 ms 34400 KB Output is correct
72 Correct 297 ms 34324 KB Output is correct
73 Correct 397 ms 48348 KB Output is correct
74 Correct 252 ms 32448 KB Output is correct