답안 #219391

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219391 2020-04-05T08:35:40 Z MKopchev Election Campaign (JOI15_election_campaign) C++14
100 / 100
252 ms 32632 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;
int n,q;
vector<int> adj[nmax];

vector<int> seen[nmax];

struct path
{
    int u,v,gain;
};

path inp[nmax];

int up[20][nmax],depth[nmax];

int in[nmax],out[nmax],t=0;
void dfs(int node,int parent)
{
    t++;
    in[node]=t;

    up[0][node]=parent;
    depth[node]=1+depth[parent];
    for(auto k:adj[node])
        if(k!=parent)dfs(k,node);

    out[node]=t;
}

int lca(int u,int v)
{
    if(depth[u]>depth[v])swap(u,v);
    for(int i=19;i>=0;i--)
        if(depth[u]<=depth[v]-(1<<i))v=up[i][v];
    if(u==v)return u;
    for(int i=19;i>=0;i--)
        if(up[i][u]!=up[i][v])u=up[i][u],v=up[i][v];
    return up[0][u];
}

long long dp[nmax];
long long sum[nmax];

long long fenwick[nmax];
void update(int pos,long long val)
{
    while(pos<=n)
    {
        fenwick[pos]+=val;
        pos=pos+(pos&(-pos));
    }
}
long long query(int pos)
{
    long long ret=0;
    while(pos)
    {
        ret=ret+fenwick[pos];
        pos=pos-(pos&(-pos));
    }
    return ret;
}
long long ask(int l,int r)
{
    if(l>r)return 0;
    return query(r)-query(l-1);
}
long long take_path(int lower,int upper)
{
    long long ret=ask(in[upper]+1,in[lower]);

    return ret;
}
long long rec(int node,int parent)
{
    long long ret=0;
    //ignore the node
    for(auto k:adj[node])
        if(k!=parent)ret=ret+rec(k,node);

    sum[node]=ret;

    for(auto which:seen[node])
    {
        long long current=inp[which].gain+take_path(inp[which].u,node)+take_path(inp[which].v,node)+sum[node];

        //cout<<"node= "<<node<<" taken_path= "<<inp[which].u<<" "<<inp[which].v<<" "<<inp[which].gain<<" -> "<<current<<" : "<<take_path(inp[which].u,node)<<" "<<take_path(inp[which].v,node)<<" "<<sum[node]<<endl;

        ret=max(ret,current);
    }

    //cout<<node<<" -> "<<ret<<" "<<sum[node]<<endl;
    dp[node]=ret;

    update(in[node],sum[node]-dp[node]);
    update(out[node]+1,-(sum[node]-dp[node]));

    return ret;
}
int main()
{
    scanf("%i",&n);
    int u,v;
    for(int i=1;i<n;i++)
    {
        scanf("%i%i",&u,&v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(1,1);
    for(int st=1;st<20;st++)
        for(int i=1;i<=n;i++)
            up[st][i]=up[st-1][up[st-1][i]];

    scanf("%i",&q);
    for(int i=1;i<=q;i++)
    {
        scanf("%i%i%i",&inp[i].u,&inp[i].v,&inp[i].gain);
        seen[lca(inp[i].u,inp[i].v)].push_back(i);
    }

    printf("%lld\n",rec(1,0));
    return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
election_campaign.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i",&u,&v);
         ~~~~~^~~~~~~~~~~~~~
election_campaign.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&q);
     ~~~~~^~~~~~~~~
election_campaign.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i%i",&inp[i].u,&inp[i].v,&inp[i].gain);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 92 ms 19704 KB Output is correct
6 Correct 58 ms 27308 KB Output is correct
7 Correct 112 ms 24696 KB Output is correct
8 Correct 68 ms 20088 KB Output is correct
9 Correct 80 ms 23032 KB Output is correct
10 Correct 69 ms 19960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5376 KB Output is correct
4 Correct 130 ms 29560 KB Output is correct
5 Correct 131 ms 31992 KB Output is correct
6 Correct 119 ms 32120 KB Output is correct
7 Correct 138 ms 31992 KB Output is correct
8 Correct 137 ms 32120 KB Output is correct
9 Correct 120 ms 32120 KB Output is correct
10 Correct 138 ms 32248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5376 KB Output is correct
4 Correct 130 ms 29560 KB Output is correct
5 Correct 131 ms 31992 KB Output is correct
6 Correct 119 ms 32120 KB Output is correct
7 Correct 138 ms 31992 KB Output is correct
8 Correct 137 ms 32120 KB Output is correct
9 Correct 120 ms 32120 KB Output is correct
10 Correct 138 ms 32248 KB Output is correct
11 Correct 19 ms 6144 KB Output is correct
12 Correct 138 ms 32376 KB Output is correct
13 Correct 142 ms 32376 KB Output is correct
14 Correct 139 ms 32632 KB Output is correct
15 Correct 136 ms 32376 KB Output is correct
16 Correct 122 ms 32356 KB Output is correct
17 Correct 137 ms 32376 KB Output is correct
18 Correct 144 ms 32504 KB Output is correct
19 Correct 123 ms 32376 KB Output is correct
20 Correct 137 ms 32376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 208 ms 21360 KB Output is correct
2 Correct 116 ms 29688 KB Output is correct
3 Correct 222 ms 29048 KB Output is correct
4 Correct 128 ms 24052 KB Output is correct
5 Correct 213 ms 28664 KB Output is correct
6 Correct 133 ms 24052 KB Output is correct
7 Correct 235 ms 28300 KB Output is correct
8 Correct 188 ms 24568 KB Output is correct
9 Correct 124 ms 32120 KB Output is correct
10 Correct 230 ms 27380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 92 ms 19704 KB Output is correct
6 Correct 58 ms 27308 KB Output is correct
7 Correct 112 ms 24696 KB Output is correct
8 Correct 68 ms 20088 KB Output is correct
9 Correct 80 ms 23032 KB Output is correct
10 Correct 69 ms 19960 KB Output is correct
11 Correct 8 ms 5248 KB Output is correct
12 Correct 8 ms 5376 KB Output is correct
13 Correct 8 ms 5376 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5376 KB Output is correct
16 Correct 8 ms 5376 KB Output is correct
17 Correct 8 ms 5248 KB Output is correct
18 Correct 8 ms 5376 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 8 ms 5376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 8 ms 5248 KB Output is correct
5 Correct 92 ms 19704 KB Output is correct
6 Correct 58 ms 27308 KB Output is correct
7 Correct 112 ms 24696 KB Output is correct
8 Correct 68 ms 20088 KB Output is correct
9 Correct 80 ms 23032 KB Output is correct
10 Correct 69 ms 19960 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 7 ms 5376 KB Output is correct
14 Correct 130 ms 29560 KB Output is correct
15 Correct 131 ms 31992 KB Output is correct
16 Correct 119 ms 32120 KB Output is correct
17 Correct 138 ms 31992 KB Output is correct
18 Correct 137 ms 32120 KB Output is correct
19 Correct 120 ms 32120 KB Output is correct
20 Correct 138 ms 32248 KB Output is correct
21 Correct 19 ms 6144 KB Output is correct
22 Correct 138 ms 32376 KB Output is correct
23 Correct 142 ms 32376 KB Output is correct
24 Correct 139 ms 32632 KB Output is correct
25 Correct 136 ms 32376 KB Output is correct
26 Correct 122 ms 32356 KB Output is correct
27 Correct 137 ms 32376 KB Output is correct
28 Correct 144 ms 32504 KB Output is correct
29 Correct 123 ms 32376 KB Output is correct
30 Correct 137 ms 32376 KB Output is correct
31 Correct 208 ms 21360 KB Output is correct
32 Correct 116 ms 29688 KB Output is correct
33 Correct 222 ms 29048 KB Output is correct
34 Correct 128 ms 24052 KB Output is correct
35 Correct 213 ms 28664 KB Output is correct
36 Correct 133 ms 24052 KB Output is correct
37 Correct 235 ms 28300 KB Output is correct
38 Correct 188 ms 24568 KB Output is correct
39 Correct 124 ms 32120 KB Output is correct
40 Correct 230 ms 27380 KB Output is correct
41 Correct 8 ms 5248 KB Output is correct
42 Correct 8 ms 5376 KB Output is correct
43 Correct 8 ms 5376 KB Output is correct
44 Correct 8 ms 5248 KB Output is correct
45 Correct 8 ms 5376 KB Output is correct
46 Correct 8 ms 5376 KB Output is correct
47 Correct 8 ms 5248 KB Output is correct
48 Correct 8 ms 5376 KB Output is correct
49 Correct 8 ms 5376 KB Output is correct
50 Correct 8 ms 5376 KB Output is correct
51 Correct 175 ms 24696 KB Output is correct
52 Correct 145 ms 32376 KB Output is correct
53 Correct 233 ms 27764 KB Output is correct
54 Correct 122 ms 24568 KB Output is correct
55 Correct 201 ms 24312 KB Output is correct
56 Correct 139 ms 32380 KB Output is correct
57 Correct 184 ms 28408 KB Output is correct
58 Correct 131 ms 24436 KB Output is correct
59 Correct 197 ms 24552 KB Output is correct
60 Correct 138 ms 32376 KB Output is correct
61 Correct 186 ms 28536 KB Output is correct
62 Correct 147 ms 24444 KB Output is correct
63 Correct 178 ms 24140 KB Output is correct
64 Correct 143 ms 32332 KB Output is correct
65 Correct 252 ms 28404 KB Output is correct
66 Correct 129 ms 24568 KB Output is correct
67 Correct 213 ms 24052 KB Output is correct
68 Correct 140 ms 32376 KB Output is correct
69 Correct 191 ms 27256 KB Output is correct
70 Correct 205 ms 24568 KB Output is correct