Submission #732928

# Submission time Handle Problem Language Result Execution time Memory
732928 2023-04-29T14:58:31 Z onepunchac168 Election Campaign (JOI15_election_campaign) C++14
100 / 100
282 ms 47384 KB
// created by Dinh Manh Hung
// onepunchac168
// THPT CHUYEN HA TINH
// HA TINH, VIET NAM

#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
//#pragma GCC target("sse4,avx2,fma")
#define task ""
#define ldb long double
#define pb push_back
#define fi first
#define se second
#define pc pop_back()
#define all(x) begin(x),end(x)
#define uniquev(v) v.resize(unique(all(v))-v.begin())
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define cntbit(v) __builtin_popcountll(v)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((1LL*a*b)/__gcd(a,b))
#define mask(x) (1LL<<(x))
#define readbit(x,i) ((x>>i)&1)
#define ins insert

typedef long long ll;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
typedef pair <ii,ii> iiii;

ll dx[]= {1,-1,0,0,1,-1,1,-1};
ll dy[]= {0,0,-1,1,1,-1,-1,1};

const ldb PI = acos (-1);
//const ll mod=978846151;
//const ll base=29;
const int maxn=1e6+5;
const int mod=1e9+7;
const char nl = '\n';
inline int ReadInt()
{
    char co;
    for (co = getchar(); co < '0' || co > '9'; co = getchar());
    int xo = co - '0';
    for (co = getchar(); co >= '0' && co <= '9'; co = getchar())
        xo = xo * 10 + co - '0';
    return xo;
}

void WriteInt(int xo)
{
    if (xo > 9)
        WriteInt(xo / 10);
    putchar(xo % 10 + '0');
}
/* END OF TEMPLATE*/

// ================= Solution =================//
const int N=1e5+5;
ll n,m;
vector <ll> vt[N];
vector <iii> tmp[N];
ll parent[N][20];
ll sz[N];
ll dp[N];
ll in[N];
ll out[N];
ll root=0;
ll bit[N];
void update(int u,ll val)
{
    while (u<=n)
    {
        bit[u]+=val;
        u+=u&(-u);
    }
}
ll get(int u)
{
    ll suma=0;
    while (u>0)
    {
        suma+=bit[u];
        u-=u&(-u);
    }
    return suma;
}
void dfsa(int u,int vv)
{
    in[u]=++root;
    for (auto v:vt[u])
    {
        if (v==vv)
        {
            continue;
        }
        parent[v][0]=u;
        for (int i=1;i<=18;i++)
        {
            parent[v][i]=parent[parent[v][i-1]][i-1];
        }
        sz[v]=sz[u]+1;
        dfsa(v,u);
    }
    out[u]=root;
}
ll par[N];
ll findpar(int u)
{
    if (par[u]<0)
    {
        return u;
    }
    return par[u]=findpar(par[u]);
}
void dfs(int u,int vv)
{
    ll sum=0;
    for (auto v:vt[u])
    {
        if (v==vv)
        {
            continue;
        }
        dfs(v,u);
        dp[u]=max(dp[u],dp[v]);
        sum+=dp[v];
    }
    ll gmax=0;
    for (auto v:tmp[u])
    {
        ll ua=v.se.fi;
        ll va=v.se.se;
        ll wa=v.fi;
        ll indexa=get(in[ua])+get(in[va]);
        ll indexb=sum;
        ll a1=findpar(ua);
        ll a2=findpar(va);
        if (a1!=u)
        {
            indexb-=dp[a1];
        }
        if (a2!=u)
        {
            indexb-=dp[a2];
        }
        gmax=max(gmax,indexa+indexb+wa);
    }
    dp[u]=max({dp[u],gmax,sum});
    update(in[u],sum);
    update(in[u]+1,-sum);
    for (auto v:vt[u])
    {
        if (v==vv)
        {
            continue;
        }
        update(in[v],sum-dp[v]);
        update(out[v]+1,-sum+dp[v]);
        par[v]=u;
    }
}
ll lca(int u,int v)
{
    if (sz[u]>sz[v])
    {
        swap(u,v);
    }
    for (int i=18;i>=0;i--)
    {
        if (sz[v]-mask(i)>=sz[u])
        {
            v=parent[v][i];
        }
    }
    if (u==v){return u;}
    for (int i=18;i>=0;i--)
    {
        if (parent[u][i]!=parent[v][i])
        {
            u=parent[u][i];
            v=parent[v][i];
        }
    }
    if (u!=v)
    {
        u=parent[u][0];
        v=parent[v][0];
    }
    return u;
}
void optmushnpr()
{
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        par[i]=-1;
    }
    for (int i=1;i<n;i++)
    {
        ll u,v;
        cin>>u>>v;
        vt[u].pb(v);
        vt[v].pb(u);
    }
    dfsa(1,-1);
    cin>>m;
    for (int i=1;i<=m;i++)
    {
        ll a,b,c;
        cin>>a>>b>>c;
        ll pp=lca(a,b);
        tmp[pp].pb({c,{a,b}});
    }
    dfs(1,-1);
    cout<<dp[1];
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(task".inp","r")){
    freopen(task".inp","r",stdin);
    freopen(task".out","w",stdout);}
    int t;
    t=1;
    //cin>>t;
    while (t--){optmushnpr();}
}

// goodbye see ya

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:225:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  225 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:226:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  226 |     freopen(task".out","w",stdout);}
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 122 ms 30340 KB Output is correct
6 Correct 57 ms 42080 KB Output is correct
7 Correct 112 ms 38228 KB Output is correct
8 Correct 94 ms 30568 KB Output is correct
9 Correct 113 ms 35632 KB Output is correct
10 Correct 80 ms 30496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5332 KB Output is correct
4 Correct 133 ms 44556 KB Output is correct
5 Correct 137 ms 44500 KB Output is correct
6 Correct 138 ms 44576 KB Output is correct
7 Correct 131 ms 44436 KB Output is correct
8 Correct 143 ms 44532 KB Output is correct
9 Correct 156 ms 44500 KB Output is correct
10 Correct 164 ms 44472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5332 KB Output is correct
4 Correct 133 ms 44556 KB Output is correct
5 Correct 137 ms 44500 KB Output is correct
6 Correct 138 ms 44576 KB Output is correct
7 Correct 131 ms 44436 KB Output is correct
8 Correct 143 ms 44532 KB Output is correct
9 Correct 156 ms 44500 KB Output is correct
10 Correct 164 ms 44472 KB Output is correct
11 Correct 12 ms 6356 KB Output is correct
12 Correct 133 ms 44504 KB Output is correct
13 Correct 135 ms 44492 KB Output is correct
14 Correct 126 ms 44500 KB Output is correct
15 Correct 147 ms 44464 KB Output is correct
16 Correct 125 ms 44576 KB Output is correct
17 Correct 145 ms 44500 KB Output is correct
18 Correct 131 ms 44520 KB Output is correct
19 Correct 117 ms 44616 KB Output is correct
20 Correct 145 ms 44448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 171 ms 32128 KB Output is correct
2 Correct 115 ms 46984 KB Output is correct
3 Correct 276 ms 42424 KB Output is correct
4 Correct 148 ms 35024 KB Output is correct
5 Correct 200 ms 41588 KB Output is correct
6 Correct 136 ms 34760 KB Output is correct
7 Correct 257 ms 41240 KB Output is correct
8 Correct 210 ms 34704 KB Output is correct
9 Correct 130 ms 47060 KB Output is correct
10 Correct 282 ms 39560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 122 ms 30340 KB Output is correct
6 Correct 57 ms 42080 KB Output is correct
7 Correct 112 ms 38228 KB Output is correct
8 Correct 94 ms 30568 KB Output is correct
9 Correct 113 ms 35632 KB Output is correct
10 Correct 80 ms 30496 KB Output is correct
11 Correct 5 ms 5332 KB Output is correct
12 Correct 4 ms 5460 KB Output is correct
13 Correct 4 ms 5332 KB Output is correct
14 Correct 4 ms 5332 KB Output is correct
15 Correct 4 ms 5300 KB Output is correct
16 Correct 4 ms 5332 KB Output is correct
17 Correct 4 ms 5332 KB Output is correct
18 Correct 5 ms 5428 KB Output is correct
19 Correct 4 ms 5320 KB Output is correct
20 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 122 ms 30340 KB Output is correct
6 Correct 57 ms 42080 KB Output is correct
7 Correct 112 ms 38228 KB Output is correct
8 Correct 94 ms 30568 KB Output is correct
9 Correct 113 ms 35632 KB Output is correct
10 Correct 80 ms 30496 KB Output is correct
11 Correct 4 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5332 KB Output is correct
14 Correct 133 ms 44556 KB Output is correct
15 Correct 137 ms 44500 KB Output is correct
16 Correct 138 ms 44576 KB Output is correct
17 Correct 131 ms 44436 KB Output is correct
18 Correct 143 ms 44532 KB Output is correct
19 Correct 156 ms 44500 KB Output is correct
20 Correct 164 ms 44472 KB Output is correct
21 Correct 12 ms 6356 KB Output is correct
22 Correct 133 ms 44504 KB Output is correct
23 Correct 135 ms 44492 KB Output is correct
24 Correct 126 ms 44500 KB Output is correct
25 Correct 147 ms 44464 KB Output is correct
26 Correct 125 ms 44576 KB Output is correct
27 Correct 145 ms 44500 KB Output is correct
28 Correct 131 ms 44520 KB Output is correct
29 Correct 117 ms 44616 KB Output is correct
30 Correct 145 ms 44448 KB Output is correct
31 Correct 171 ms 32128 KB Output is correct
32 Correct 115 ms 46984 KB Output is correct
33 Correct 276 ms 42424 KB Output is correct
34 Correct 148 ms 35024 KB Output is correct
35 Correct 200 ms 41588 KB Output is correct
36 Correct 136 ms 34760 KB Output is correct
37 Correct 257 ms 41240 KB Output is correct
38 Correct 210 ms 34704 KB Output is correct
39 Correct 130 ms 47060 KB Output is correct
40 Correct 282 ms 39560 KB Output is correct
41 Correct 5 ms 5332 KB Output is correct
42 Correct 4 ms 5460 KB Output is correct
43 Correct 4 ms 5332 KB Output is correct
44 Correct 4 ms 5332 KB Output is correct
45 Correct 4 ms 5300 KB Output is correct
46 Correct 4 ms 5332 KB Output is correct
47 Correct 4 ms 5332 KB Output is correct
48 Correct 5 ms 5428 KB Output is correct
49 Correct 4 ms 5320 KB Output is correct
50 Correct 4 ms 5460 KB Output is correct
51 Correct 187 ms 35000 KB Output is correct
52 Correct 139 ms 47264 KB Output is correct
53 Correct 258 ms 40060 KB Output is correct
54 Correct 133 ms 35136 KB Output is correct
55 Correct 196 ms 34944 KB Output is correct
56 Correct 152 ms 47208 KB Output is correct
57 Correct 200 ms 41132 KB Output is correct
58 Correct 125 ms 35044 KB Output is correct
59 Correct 167 ms 35084 KB Output is correct
60 Correct 158 ms 47284 KB Output is correct
61 Correct 210 ms 41212 KB Output is correct
62 Correct 130 ms 34808 KB Output is correct
63 Correct 201 ms 34664 KB Output is correct
64 Correct 164 ms 47384 KB Output is correct
65 Correct 236 ms 41084 KB Output is correct
66 Correct 139 ms 34980 KB Output is correct
67 Correct 173 ms 34744 KB Output is correct
68 Correct 128 ms 47232 KB Output is correct
69 Correct 209 ms 39388 KB Output is correct
70 Correct 149 ms 35132 KB Output is correct