Submission #298488

# Submission time Handle Problem Language Result Execution time Memory
298488 2020-09-13T02:15:38 Z XmtosX Election Campaign (JOI15_election_campaign) C++17
100 / 100
709 ms 52556 KB
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+3;
int n,q,sprs[N][20],lvl[N],dpsons[N],dp[N],par[N],sz[N],l,r;
vector <long long> v[N],pfx[N][2];
vector <pair<long long,long long> > chn[N];
bool yes,yes1;
struct node
{
    int a,b,upa,upb,ans;
};
vector <node> V[N];
int findpar(int x)
{
    if (x==par[x])
        return x;
    return par[x]=findpar(par[x]);
}
void unun (int n1,int n2)
{
    int x1=findpar(n1);
    int x2=findpar(n2);
    par[x2]=x1;
}
void dfs1 (int x,int p)
{
    sz[x]=1;
    par[x]=x;
    sprs[x][0]=p;
    lvl[x]=lvl[p]+1;
    pair<int,long long> big={0,0};
    for (int i=1;i<20;i++)
        sprs[x][i]=sprs[sprs[x][i-1]][i-1];
    for (int i=0;i<v[x].size();i++)
    {
        if (v[x][i]!=p)
        {
            dfs1(v[x][i],x);
            sz[x]+=sz[v[x][i]];
            big=max(big,make_pair(sz[v[x][i]],v[x][i]));
        }
    }
    if (big.first)
        unun(x,big.second);
}
int query (int x,int idx)
{
    int ans=0;
    int low=0,high=chn[x].size()-1,mid,idx1,idx2;
    while (low<=high)
    {
        mid= (low+high)/2;
        if (chn[x][mid].first>l)
            low=mid+1;
        else
        {
            idx1=mid;
            high=mid-1;
        }
    }
    low=0,high=chn[x].size()-1;
    while (low<=high)
    {
        mid= (low+high)/2;
        if (chn[x][mid].first<r)
            high=mid-1;
        else
        {
            idx2=mid;
            low=mid+1;
        }
    }
    ans=pfx[x][idx][idx2];
    idx1--;
    if (idx1>=0)
        ans-=pfx[x][idx][idx1];
    if (chn[x].back().first>r)
    {
        l=chn[x].back().first-1;
        ans+= query(findpar(sprs[x][0]),idx);
    }
    return ans;
}
void dfs (int x,int p)
{
    for (int i=0;i<v[x].size();i++)
    {
        if (v[x][i]!=p)
        {
            dfs(v[x][i],x);
            dpsons[x]+=dp[v[x][i]];
        }
    }

    dp[x]=dpsons[x];
    int maxx=dpsons[x];
    for (int i=0;i<V[x].size();i++)
    {
        int cur=dpsons[x];
        cur-= dp[V[x][i].upa];
        cur-= dp[V[x][i].upb];
        cur+= dpsons[V[x][i].a];
        cur+= dpsons[V[x][i].b];
        int A= V[x][i].a;
        int B= V[x][i].b;
        if (x!=V[x][i].a&&sprs[V[x][i].a][0]!=x)
        {
            V[x][i].a=sprs[V[x][i].a][0];
            l= lvl[V[x][i].a];
            r= lvl[x]+1;
            int L=l,R=r;
            cur+= query(findpar(V[x][i].a),0);
            l=L,r=R;
            r++;
            cur-= dp[A];
            if (l>=r)
                cur-= (query(findpar(V[x][i].a),1));
        }
        if (x!=V[x][i].b&&sprs[V[x][i].b][0]!=x)
        {
            V[x][i].b=sprs[V[x][i].b][0];
            l= lvl[V[x][i].b];
            r= lvl[x]+1;
            int L=l,R=r;
            cur+= query(findpar(V[x][i].b),0);
            l=L,r=R;
            r++;
            cur-= dp[B];
            if (l>=r)
            {
                cur-= (query(findpar(V[x][i].b),1));
            }
        }
        cur+= V[x][i].ans;
        maxx=max(maxx,cur);
    }
    dp[x]=maxx;
    int idx=findpar(x);
    pair<int,int> P= {lvl[x],x};
    int o;
    if (!chn[idx].empty())
        o=chn[idx].back().second;
    chn[idx].push_back(P);
    if (pfx[idx][0].empty())
    {
        pfx[idx][0].push_back(dpsons[x]);
        pfx[idx][1].push_back(dp[x]);
    }
    else
    {
        pfx[idx][0].push_back(pfx[idx][0].back());
        pfx[idx][0].back()+= dpsons[x];
        pfx[idx][1].push_back(pfx[idx][1].back());
        pfx[idx][1].back()+= dp[x];
    }
}
pair<int,int> lca(int x,int y)
{
    pair<int,int> p;
    if (lvl[x]<lvl[y])
        swap(x,y);
    for (int i=19;i>=0;i--)
    {
        if (lvl[sprs[x][i]]>lvl[y])
            x=sprs[x][i];
    }
    if (sprs[x][0]==y)
    {
        p={x,y};
        return p;
    }
    if (lvl[x]!=lvl[y])
        x=sprs[x][0];
    for (int i=19;i>=0;i--)
    {
        if (sprs[x][i]!=sprs[y][i])
        {
            x=sprs[x][i];
            y=sprs[y][i];
        }
    }
    p={x,y};
    return p;
}
int main()
{
    int maxx=0;
    cin >>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin >>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    int st=1;
    dfs1(st,0);
    cin >>q;
    while(q--)
    {
        int x,y,z;
        cin >>x>>y>>z;
        pair<int,int> p=lca(x,y);
        int lc= sprs[p.first][0];
        int val=z;
        node A= {x,y,p.first,p.second,val};
        maxx=max(maxx,z);
        V[lc].push_back(A);
    }
    dfs(st,0);
    cout <<dp[st];
    return 0;
}
/*
7
3 4
6 5
2 7
1 5
7 5
4 5
5
4 3 10
5 6 5
2 6 9
7 2 2
1 3 8

8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
5
7 5 4
5 8 9
4 3 9
1 3 3
2 8 11

20
17 10
11 4
8 3
3 16
1 14
15 18
5 4
6 18
10 18
19 4
16 7
2 13
4 12
12 20
9 20
18 13
20 14
14 7
13 7
15
19 9 2341
13 8 6974
8 3 3339
15 17 6515
10 13 4370
1 7 8376
18 2 9272
6 7 4595
1 20 505
10 9 308
6 19 8937
2 15 5072
5 4 4217
2 4 4170
19 12 8204
*/

Compilation message

election_campaign.cpp: In function 'void dfs1(int, int)':
election_campaign.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for (int i=0;i<v[x].size();i++)
      |                  ~^~~~~~~~~~~~
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:86:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i=0;i<v[x].size();i++)
      |                  ~^~~~~~~~~~~~
election_campaign.cpp:97:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for (int i=0;i<V[x].size();i++)
      |                  ~^~~~~~~~~~~~
election_campaign.cpp:140:9: warning: variable 'o' set but not used [-Wunused-but-set-variable]
  140 |     int o;
      |         ^
election_campaign.cpp: In function 'int query(int, int)':
election_campaign.cpp:74:9: warning: 'idx1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   74 |     idx1--;
      |     ~~~~^~
election_campaign.cpp:73:25: warning: 'idx2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   73 |     ans=pfx[x][idx][idx2];
      |                         ^
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12288 KB Output is correct
5 Correct 261 ms 31932 KB Output is correct
6 Correct 171 ms 47724 KB Output is correct
7 Correct 250 ms 42380 KB Output is correct
8 Correct 255 ms 36088 KB Output is correct
9 Correct 254 ms 39528 KB Output is correct
10 Correct 252 ms 36088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 8 ms 12032 KB Output is correct
3 Correct 9 ms 12416 KB Output is correct
4 Correct 436 ms 49752 KB Output is correct
5 Correct 420 ms 49764 KB Output is correct
6 Correct 395 ms 49892 KB Output is correct
7 Correct 457 ms 49892 KB Output is correct
8 Correct 461 ms 49764 KB Output is correct
9 Correct 391 ms 49768 KB Output is correct
10 Correct 437 ms 49944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12032 KB Output is correct
2 Correct 8 ms 12032 KB Output is correct
3 Correct 9 ms 12416 KB Output is correct
4 Correct 436 ms 49752 KB Output is correct
5 Correct 420 ms 49764 KB Output is correct
6 Correct 395 ms 49892 KB Output is correct
7 Correct 457 ms 49892 KB Output is correct
8 Correct 461 ms 49764 KB Output is correct
9 Correct 391 ms 49768 KB Output is correct
10 Correct 437 ms 49944 KB Output is correct
11 Correct 52 ms 13308 KB Output is correct
12 Correct 444 ms 49768 KB Output is correct
13 Correct 460 ms 49892 KB Output is correct
14 Correct 402 ms 49888 KB Output is correct
15 Correct 461 ms 49892 KB Output is correct
16 Correct 412 ms 49844 KB Output is correct
17 Correct 451 ms 49896 KB Output is correct
18 Correct 456 ms 49764 KB Output is correct
19 Correct 417 ms 49768 KB Output is correct
20 Correct 458 ms 49792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 624 ms 34188 KB Output is correct
2 Correct 386 ms 52240 KB Output is correct
3 Correct 656 ms 46384 KB Output is correct
4 Correct 452 ms 39564 KB Output is correct
5 Correct 535 ms 45220 KB Output is correct
6 Correct 452 ms 39404 KB Output is correct
7 Correct 636 ms 44744 KB Output is correct
8 Correct 515 ms 37000 KB Output is correct
9 Correct 398 ms 52256 KB Output is correct
10 Correct 688 ms 42760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12288 KB Output is correct
5 Correct 261 ms 31932 KB Output is correct
6 Correct 171 ms 47724 KB Output is correct
7 Correct 250 ms 42380 KB Output is correct
8 Correct 255 ms 36088 KB Output is correct
9 Correct 254 ms 39528 KB Output is correct
10 Correct 252 ms 36088 KB Output is correct
11 Correct 12 ms 12288 KB Output is correct
12 Correct 11 ms 12448 KB Output is correct
13 Correct 12 ms 12416 KB Output is correct
14 Correct 11 ms 12416 KB Output is correct
15 Correct 12 ms 12448 KB Output is correct
16 Correct 11 ms 12416 KB Output is correct
17 Correct 11 ms 12328 KB Output is correct
18 Correct 11 ms 12544 KB Output is correct
19 Correct 12 ms 12416 KB Output is correct
20 Correct 11 ms 12416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 7 ms 12032 KB Output is correct
3 Correct 8 ms 12032 KB Output is correct
4 Correct 9 ms 12288 KB Output is correct
5 Correct 261 ms 31932 KB Output is correct
6 Correct 171 ms 47724 KB Output is correct
7 Correct 250 ms 42380 KB Output is correct
8 Correct 255 ms 36088 KB Output is correct
9 Correct 254 ms 39528 KB Output is correct
10 Correct 252 ms 36088 KB Output is correct
11 Correct 8 ms 12032 KB Output is correct
12 Correct 8 ms 12032 KB Output is correct
13 Correct 9 ms 12416 KB Output is correct
14 Correct 436 ms 49752 KB Output is correct
15 Correct 420 ms 49764 KB Output is correct
16 Correct 395 ms 49892 KB Output is correct
17 Correct 457 ms 49892 KB Output is correct
18 Correct 461 ms 49764 KB Output is correct
19 Correct 391 ms 49768 KB Output is correct
20 Correct 437 ms 49944 KB Output is correct
21 Correct 52 ms 13308 KB Output is correct
22 Correct 444 ms 49768 KB Output is correct
23 Correct 460 ms 49892 KB Output is correct
24 Correct 402 ms 49888 KB Output is correct
25 Correct 461 ms 49892 KB Output is correct
26 Correct 412 ms 49844 KB Output is correct
27 Correct 451 ms 49896 KB Output is correct
28 Correct 456 ms 49764 KB Output is correct
29 Correct 417 ms 49768 KB Output is correct
30 Correct 458 ms 49792 KB Output is correct
31 Correct 624 ms 34188 KB Output is correct
32 Correct 386 ms 52240 KB Output is correct
33 Correct 656 ms 46384 KB Output is correct
34 Correct 452 ms 39564 KB Output is correct
35 Correct 535 ms 45220 KB Output is correct
36 Correct 452 ms 39404 KB Output is correct
37 Correct 636 ms 44744 KB Output is correct
38 Correct 515 ms 37000 KB Output is correct
39 Correct 398 ms 52256 KB Output is correct
40 Correct 688 ms 42760 KB Output is correct
41 Correct 12 ms 12288 KB Output is correct
42 Correct 11 ms 12448 KB Output is correct
43 Correct 12 ms 12416 KB Output is correct
44 Correct 11 ms 12416 KB Output is correct
45 Correct 12 ms 12448 KB Output is correct
46 Correct 11 ms 12416 KB Output is correct
47 Correct 11 ms 12328 KB Output is correct
48 Correct 11 ms 12544 KB Output is correct
49 Correct 12 ms 12416 KB Output is correct
50 Correct 11 ms 12416 KB Output is correct
51 Correct 545 ms 37332 KB Output is correct
52 Correct 455 ms 52516 KB Output is correct
53 Correct 658 ms 43644 KB Output is correct
54 Correct 467 ms 39660 KB Output is correct
55 Correct 674 ms 36872 KB Output is correct
56 Correct 465 ms 52452 KB Output is correct
57 Correct 575 ms 44620 KB Output is correct
58 Correct 497 ms 39692 KB Output is correct
59 Correct 556 ms 37376 KB Output is correct
60 Correct 469 ms 52556 KB Output is correct
61 Correct 606 ms 44328 KB Output is correct
62 Correct 491 ms 39732 KB Output is correct
63 Correct 659 ms 36976 KB Output is correct
64 Correct 492 ms 52452 KB Output is correct
65 Correct 709 ms 44444 KB Output is correct
66 Correct 459 ms 39752 KB Output is correct
67 Correct 622 ms 36908 KB Output is correct
68 Correct 460 ms 52452 KB Output is correct
69 Correct 550 ms 41732 KB Output is correct
70 Correct 466 ms 39700 KB Output is correct