Submission #433573

# Submission time Handle Problem Language Result Execution time Memory
433573 2021-06-20T07:45:14 Z cpp219 Election Campaign (JOI15_election_campaign) C++14
100 / 100
486 ms 34244 KB
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")

#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 1e5 + 9;
const ll mod = 1e9 + 7;
const ll Log2 = 20;
typedef pair<ll,ll> LL;
vector<ll> g[N],Q[N];
ll n,m,x,y,All[N],dp[N];

struct plan{
    ll x,y,cost;
};
plan q[N];

ll par[N][Log2],d[N],ans,child[N];
void DFS(ll u,ll p){
    child[u] = 1;
    for (ll i = 1;i < Log2;i++) if ((1 << i) <= d[u])
        par[u][i] = par[par[u][i - 1]][i - 1];
    for (auto i : g[u]){
        if (i == p) continue;
        par[i][0] = u; d[i] = d[u] + 1; DFS(i,u);
        child[u] += child[i];
    }
}

ll LCA(ll u,ll v){
    if (d[u] > d[v]) swap(u,v);
    for (ll i = Log2 - 1;i >= 0;i--) if ((1 << i) <= d[v] && d[par[v][i]] >= d[u])
        v = par[v][i];
    if (u == v) return u;
    for (ll i = Log2 - 1;i >= 0;i--) if (par[v][i] != par[u][i])
        v = par[v][i],u = par[u][i];
    return par[u][0];
}

ll st[4*N];

void upd(ll id,ll l,ll r,ll u,ll val){
    if (u < l||r < u) return;
    if (l == r){
        st[id] += val; return;
    }
    ll mid = (l + r)/2;
    upd(id*2,l,mid,u,val); upd(id*2 + 1,mid + 1,r,u,val);
    st[id] = st[id*2] + st[id*2 + 1];
}

ll Get(ll id,ll l,ll r,ll u,ll v){
    if (v < l||r < u) return 0;
    if (u <= l&&r <= v) return st[id];
    ll mid = (l + r)/2;
    return Get(id*2,l,mid,u,v) + Get(id*2 + 1,mid + 1,r,u,v);
}

ll pos[N],Real[N],chain[N],head[N],nTime = 1,cur = 1;

void DFShld(ll u,ll p){
    pos[u] = nTime; Real[nTime] = u; nTime++;
    if (!head[cur]) head[cur] = u; chain[u] = cur;
    LL s = {0,0};
    for (auto i : g[u]){
        if (i == p) continue;
        s = max(s,{child[i],i});
    }
    if (s.sc) DFShld(s.sc,u);
    for (auto i : g[u]){
        if (i == p || i == s.sc) continue;
        cur++; DFShld(i,u);
    }
}

ll hld(ll u,ll v){
    /// u is par of v
    ll res = 0;
    while(chain[u] != chain[v]){
        ll nxt = head[chain[v]];
        res += Get(1,1,n,pos[nxt],pos[v]);
        v = par[nxt][0];
    }

    res += Get(1,1,n,pos[u],pos[v]); return res;
}

void f(ll u){
    for (auto i : g[u]) if (d[i] > d[u]) f(i),All[u] += dp[i];
    upd(1,1,n,pos[u],All[u]); dp[u] = All[u];

    for (auto i : Q[u]){
        x = q[i].x; y = q[i].y; ll cost = q[i].cost;
        dp[u] = max(dp[u],hld(u,x) + hld(u,y) - All[u] + cost);
    }

    upd(1,1,n,pos[u],-dp[u]); ans = max(ans,dp[u]);

}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin>>n;
    for (ll i = 1;i < n;i++){
        cin>>x>>y;
        g[x].push_back(y); g[y].push_back(x);
    }
    DFS(1,0); DFShld(1,0);
    cin>>m;
    for (ll i = 1;i <= m;i++){
        cin>>q[i].x>>q[i].y>>q[i].cost;
        if (d[q[i].x] > d[q[i].y]) swap(q[i].x,q[i].y);
        ll now = LCA(q[i].x,q[i].y);
        Q[now].push_back(i);

    }
    f(1); cout<<ans;
}

Compilation message

election_campaign.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    1 | #pragma GCC optimization "O2"
      | 
election_campaign.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization "unroll-loop"
      | 
election_campaign.cpp: In function 'void DFShld(int, int)':
election_campaign.cpp:68:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   68 |     if (!head[cur]) head[cur] = u; chain[u] = cur;
      |     ^~
election_campaign.cpp:68:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   68 |     if (!head[cur]) head[cur] = u; chain[u] = cur;
      |                                    ^~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 4 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 181 ms 21132 KB Output is correct
6 Correct 100 ms 30244 KB Output is correct
7 Correct 182 ms 27064 KB Output is correct
8 Correct 144 ms 21372 KB Output is correct
9 Correct 161 ms 25156 KB Output is correct
10 Correct 132 ms 21364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 226 ms 33700 KB Output is correct
5 Correct 226 ms 33948 KB Output is correct
6 Correct 215 ms 33788 KB Output is correct
7 Correct 231 ms 33860 KB Output is correct
8 Correct 242 ms 33908 KB Output is correct
9 Correct 225 ms 33864 KB Output is correct
10 Correct 237 ms 33840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 226 ms 33700 KB Output is correct
5 Correct 226 ms 33948 KB Output is correct
6 Correct 215 ms 33788 KB Output is correct
7 Correct 231 ms 33860 KB Output is correct
8 Correct 242 ms 33908 KB Output is correct
9 Correct 225 ms 33864 KB Output is correct
10 Correct 237 ms 33840 KB Output is correct
11 Correct 20 ms 6080 KB Output is correct
12 Correct 238 ms 34052 KB Output is correct
13 Correct 232 ms 34080 KB Output is correct
14 Correct 206 ms 34244 KB Output is correct
15 Correct 228 ms 34116 KB Output is correct
16 Correct 218 ms 34228 KB Output is correct
17 Correct 232 ms 34176 KB Output is correct
18 Correct 227 ms 34136 KB Output is correct
19 Correct 216 ms 34116 KB Output is correct
20 Correct 246 ms 34108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 486 ms 24128 KB Output is correct
2 Correct 229 ms 33888 KB Output is correct
3 Correct 369 ms 30260 KB Output is correct
4 Correct 267 ms 24384 KB Output is correct
5 Correct 321 ms 29764 KB Output is correct
6 Correct 271 ms 24492 KB Output is correct
7 Correct 382 ms 29380 KB Output is correct
8 Correct 336 ms 24624 KB Output is correct
9 Correct 200 ms 33880 KB Output is correct
10 Correct 382 ms 28312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 4 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 181 ms 21132 KB Output is correct
6 Correct 100 ms 30244 KB Output is correct
7 Correct 182 ms 27064 KB Output is correct
8 Correct 144 ms 21372 KB Output is correct
9 Correct 161 ms 25156 KB Output is correct
10 Correct 132 ms 21364 KB Output is correct
11 Correct 6 ms 5164 KB Output is correct
12 Correct 5 ms 5324 KB Output is correct
13 Correct 5 ms 5260 KB Output is correct
14 Correct 5 ms 5196 KB Output is correct
15 Correct 6 ms 5196 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 5 ms 5196 KB Output is correct
18 Correct 5 ms 5196 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 5 ms 5288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 4 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 181 ms 21132 KB Output is correct
6 Correct 100 ms 30244 KB Output is correct
7 Correct 182 ms 27064 KB Output is correct
8 Correct 144 ms 21372 KB Output is correct
9 Correct 161 ms 25156 KB Output is correct
10 Correct 132 ms 21364 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 226 ms 33700 KB Output is correct
15 Correct 226 ms 33948 KB Output is correct
16 Correct 215 ms 33788 KB Output is correct
17 Correct 231 ms 33860 KB Output is correct
18 Correct 242 ms 33908 KB Output is correct
19 Correct 225 ms 33864 KB Output is correct
20 Correct 237 ms 33840 KB Output is correct
21 Correct 20 ms 6080 KB Output is correct
22 Correct 238 ms 34052 KB Output is correct
23 Correct 232 ms 34080 KB Output is correct
24 Correct 206 ms 34244 KB Output is correct
25 Correct 228 ms 34116 KB Output is correct
26 Correct 218 ms 34228 KB Output is correct
27 Correct 232 ms 34176 KB Output is correct
28 Correct 227 ms 34136 KB Output is correct
29 Correct 216 ms 34116 KB Output is correct
30 Correct 246 ms 34108 KB Output is correct
31 Correct 486 ms 24128 KB Output is correct
32 Correct 229 ms 33888 KB Output is correct
33 Correct 369 ms 30260 KB Output is correct
34 Correct 267 ms 24384 KB Output is correct
35 Correct 321 ms 29764 KB Output is correct
36 Correct 271 ms 24492 KB Output is correct
37 Correct 382 ms 29380 KB Output is correct
38 Correct 336 ms 24624 KB Output is correct
39 Correct 200 ms 33880 KB Output is correct
40 Correct 382 ms 28312 KB Output is correct
41 Correct 6 ms 5164 KB Output is correct
42 Correct 5 ms 5324 KB Output is correct
43 Correct 5 ms 5260 KB Output is correct
44 Correct 5 ms 5196 KB Output is correct
45 Correct 6 ms 5196 KB Output is correct
46 Correct 5 ms 5196 KB Output is correct
47 Correct 5 ms 5196 KB Output is correct
48 Correct 5 ms 5196 KB Output is correct
49 Correct 5 ms 5196 KB Output is correct
50 Correct 5 ms 5288 KB Output is correct
51 Correct 346 ms 24928 KB Output is correct
52 Correct 235 ms 34068 KB Output is correct
53 Correct 392 ms 28792 KB Output is correct
54 Correct 257 ms 24628 KB Output is correct
55 Correct 452 ms 24452 KB Output is correct
56 Correct 226 ms 34116 KB Output is correct
57 Correct 344 ms 29532 KB Output is correct
58 Correct 273 ms 24772 KB Output is correct
59 Correct 341 ms 24900 KB Output is correct
60 Correct 218 ms 34116 KB Output is correct
61 Correct 332 ms 29692 KB Output is correct
62 Correct 272 ms 24708 KB Output is correct
63 Correct 444 ms 24620 KB Output is correct
64 Correct 233 ms 34056 KB Output is correct
65 Correct 385 ms 29460 KB Output is correct
66 Correct 258 ms 24772 KB Output is correct
67 Correct 443 ms 24576 KB Output is correct
68 Correct 238 ms 34180 KB Output is correct
69 Correct 320 ms 28100 KB Output is correct
70 Correct 271 ms 24896 KB Output is correct