Submission #112237

# Submission time Handle Problem Language Result Execution time Memory
112237 2019-05-18T03:08:30 Z thebes Designated Cities (JOI19_designated_cities) C++14
100 / 100
705 ms 41764 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MN = 2e5+5;
ll N, Q, i, ans[MN], tot, cur, x, y, a, b, dist[MN], w[2*MN], par[MN];
vector<pair<ll,ll>> adj[MN], ord;
struct pq{bool operator()(const pair<ll,ll>&i,const pair<ll,ll>&j){return i.second>j.second;}};
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,pq> q;
ll meh(ll n,ll p){
    ll cur = 0;
    for(auto v : adj[n]){
        if(v.first==p) continue;
        cur+=w[v.second^1]+meh(v.first,n);
    }
    return cur;
}
void dfs(ll n,ll p){
    ans[1]=max(ans[1],cur);
    for(auto v : adj[n]){
        if(v.first==p) continue;
        cur += w[v.second]-w[v.second^1]; dfs(v.first,n);
        cur -= w[v.second]-w[v.second^1];
    }
}
pair<ll,ll> dfs2(ll n,ll p){
    pair<ll,ll> tmp(n,0);
    for(auto v : adj[n]){
        if(v.first==p) continue;
        auto tt = dfs2(v.first, n);
        tt.second += w[v.second];
        if(tt.second<tmp.second) ord.push_back(tt);
        else{swap(tmp,tt); ord.push_back(tt);}
    }
    return tmp;
}
int main(){
    memset(ans,-1,sizeof(ans));
    for(scanf("%lld",&N),i=1;i<N;i++){
        scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
        adj[x].push_back({y,2*i-2});
        adj[y].push_back({x,2*i-1});
        w[2*i-2]=a; w[2*i-1]=b;
        tot += a+b;
    }
    cur=meh(1, 0); dfs(1, 0); ans[1]=tot-ans[1];
    memset(dist,-1,sizeof(dist));
    q.push({1, 0});
    while(q.size()){
        auto t = q.top(); q.pop();
        if(dist[t.first]!=-1) continue;
        dist[t.first]=t.second; b = t.first;
        for(auto v : adj[t.first]){
            q.push({v.first,w[v.second]+t.second});
        }
    }
    memset(dist,-1,sizeof(dist));
    memset(par,-1,sizeof(par));
    q.push({b,0}); par[b]=0;
    while(q.size()){
        auto t = q.top(); q.pop();
        if(dist[t.first]!=-1) continue;
        dist[t.first]=t.second; a = t.first;
        for(auto v : adj[t.first]){
            q.push({v.first,w[v.second]+t.second});
            if(par[v.first]==-1) par[v.first]=t.first;
        }
    }
    ll lst = -1; cur = 0;
    while(a){
        for(auto v : adj[a]){
            if(v.first==lst||v.first==par[a])
                cur += w[v.second^1];
            else{
                cur += meh(v.first, a)+w[v.second^1];
                auto tmp = dfs2(v.first, a);
                tmp.second += w[v.second];
                ord.push_back(tmp);
            }
        }
        lst = a; a = par[a];
    }
    sort(ord.begin(),ord.end(),[](pair<ll,ll>i,pair<ll,ll>j){return i.second>j.second;});
    ans[2] = tot - cur;
    for(i=0;i<ord.size();i++){
        ans[3+i]=ans[2+i]-ord[i].second;
    }
    for(i=1;i<=N;i++){if(ans[i]==-1) ans[i]=ans[i-1];}
    for(scanf("%lld",&Q);Q;Q--){
        scanf("%lld",&x);
        printf("%lld\n",ans[x]);
    }
    return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:85:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0;i<ord.size();i++){
             ~^~~~~~~~~~~
designated_cities.cpp:39:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%lld",&N),i=1;i<N;i++){
         ~~~~~~~~~~~~~~~~^~~~
designated_cities.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%lld",&Q);Q;Q--){
         ~~~~~^~~~~~~~~~~
designated_cities.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&x);
         ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 634 ms 33924 KB Output is correct
3 Correct 560 ms 39936 KB Output is correct
4 Correct 653 ms 33832 KB Output is correct
5 Correct 650 ms 34972 KB Output is correct
6 Correct 530 ms 35428 KB Output is correct
7 Correct 609 ms 35296 KB Output is correct
8 Correct 527 ms 40744 KB Output is correct
9 Correct 539 ms 38324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 633 ms 33992 KB Output is correct
3 Correct 536 ms 41540 KB Output is correct
4 Correct 632 ms 33644 KB Output is correct
5 Correct 650 ms 34916 KB Output is correct
6 Correct 541 ms 35904 KB Output is correct
7 Correct 543 ms 37588 KB Output is correct
8 Correct 512 ms 38572 KB Output is correct
9 Correct 522 ms 37828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 9 ms 9728 KB Output is correct
13 Correct 12 ms 9984 KB Output is correct
14 Correct 12 ms 9984 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 12 ms 10112 KB Output is correct
21 Correct 12 ms 9984 KB Output is correct
22 Correct 12 ms 9984 KB Output is correct
23 Correct 12 ms 10040 KB Output is correct
24 Correct 12 ms 10112 KB Output is correct
25 Correct 11 ms 10136 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 634 ms 33924 KB Output is correct
3 Correct 560 ms 39936 KB Output is correct
4 Correct 653 ms 33832 KB Output is correct
5 Correct 650 ms 34972 KB Output is correct
6 Correct 530 ms 35428 KB Output is correct
7 Correct 609 ms 35296 KB Output is correct
8 Correct 527 ms 40744 KB Output is correct
9 Correct 539 ms 38324 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 633 ms 33992 KB Output is correct
12 Correct 536 ms 41540 KB Output is correct
13 Correct 632 ms 33644 KB Output is correct
14 Correct 650 ms 34916 KB Output is correct
15 Correct 541 ms 35904 KB Output is correct
16 Correct 543 ms 37588 KB Output is correct
17 Correct 512 ms 38572 KB Output is correct
18 Correct 522 ms 37828 KB Output is correct
19 Correct 9 ms 9728 KB Output is correct
20 Correct 616 ms 33900 KB Output is correct
21 Correct 545 ms 41764 KB Output is correct
22 Correct 652 ms 33632 KB Output is correct
23 Correct 612 ms 34072 KB Output is correct
24 Correct 623 ms 33116 KB Output is correct
25 Correct 620 ms 34344 KB Output is correct
26 Correct 623 ms 33000 KB Output is correct
27 Correct 646 ms 34788 KB Output is correct
28 Correct 548 ms 35560 KB Output is correct
29 Correct 646 ms 34476 KB Output is correct
30 Correct 660 ms 33272 KB Output is correct
31 Correct 590 ms 37340 KB Output is correct
32 Correct 530 ms 39108 KB Output is correct
33 Correct 535 ms 37972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 9 ms 9728 KB Output is correct
5 Correct 9 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 9 ms 9728 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 9 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 9 ms 9728 KB Output is correct
13 Correct 634 ms 33924 KB Output is correct
14 Correct 560 ms 39936 KB Output is correct
15 Correct 653 ms 33832 KB Output is correct
16 Correct 650 ms 34972 KB Output is correct
17 Correct 530 ms 35428 KB Output is correct
18 Correct 609 ms 35296 KB Output is correct
19 Correct 527 ms 40744 KB Output is correct
20 Correct 539 ms 38324 KB Output is correct
21 Correct 9 ms 9728 KB Output is correct
22 Correct 633 ms 33992 KB Output is correct
23 Correct 536 ms 41540 KB Output is correct
24 Correct 632 ms 33644 KB Output is correct
25 Correct 650 ms 34916 KB Output is correct
26 Correct 541 ms 35904 KB Output is correct
27 Correct 543 ms 37588 KB Output is correct
28 Correct 512 ms 38572 KB Output is correct
29 Correct 522 ms 37828 KB Output is correct
30 Correct 9 ms 9728 KB Output is correct
31 Correct 12 ms 9984 KB Output is correct
32 Correct 12 ms 9984 KB Output is correct
33 Correct 12 ms 9984 KB Output is correct
34 Correct 12 ms 9984 KB Output is correct
35 Correct 12 ms 9984 KB Output is correct
36 Correct 12 ms 9984 KB Output is correct
37 Correct 12 ms 9984 KB Output is correct
38 Correct 12 ms 10112 KB Output is correct
39 Correct 12 ms 9984 KB Output is correct
40 Correct 12 ms 9984 KB Output is correct
41 Correct 12 ms 10040 KB Output is correct
42 Correct 12 ms 10112 KB Output is correct
43 Correct 11 ms 10136 KB Output is correct
44 Correct 12 ms 10112 KB Output is correct
45 Correct 9 ms 9728 KB Output is correct
46 Correct 616 ms 33900 KB Output is correct
47 Correct 545 ms 41764 KB Output is correct
48 Correct 652 ms 33632 KB Output is correct
49 Correct 612 ms 34072 KB Output is correct
50 Correct 623 ms 33116 KB Output is correct
51 Correct 620 ms 34344 KB Output is correct
52 Correct 623 ms 33000 KB Output is correct
53 Correct 646 ms 34788 KB Output is correct
54 Correct 548 ms 35560 KB Output is correct
55 Correct 646 ms 34476 KB Output is correct
56 Correct 660 ms 33272 KB Output is correct
57 Correct 590 ms 37340 KB Output is correct
58 Correct 530 ms 39108 KB Output is correct
59 Correct 535 ms 37972 KB Output is correct
60 Correct 9 ms 9728 KB Output is correct
61 Correct 682 ms 35592 KB Output is correct
62 Correct 594 ms 41728 KB Output is correct
63 Correct 700 ms 35368 KB Output is correct
64 Correct 682 ms 35840 KB Output is correct
65 Correct 665 ms 34464 KB Output is correct
66 Correct 681 ms 35804 KB Output is correct
67 Correct 682 ms 34432 KB Output is correct
68 Correct 705 ms 36680 KB Output is correct
69 Correct 589 ms 36444 KB Output is correct
70 Correct 688 ms 36044 KB Output is correct
71 Correct 670 ms 35288 KB Output is correct
72 Correct 652 ms 37592 KB Output is correct
73 Correct 580 ms 39692 KB Output is correct
74 Correct 567 ms 39244 KB Output is correct