Submission #161663

#TimeUsernameProblemLanguageResultExecution timeMemory
161663arnold518Beads and wires (APIO14_beads)C++14
100 / 100
315 ms32736 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;
const ll INF = 1e15;

int N;
vector<pii> adj[MAXN+10];
ll dp1[MAXN+10], dp2[MAXN+10], dp3[MAXN+10], dp4[MAXN+10], len[MAXN+10], ans;

void dfs(int now, int bef, ll par)
{
    ll val=0;
    len[now]=par;
    for(pii nxt : adj[now])
    {
        if(nxt.first==bef) continue;
        dfs(nxt.first, now, nxt.second);
        dp2[now]+=dp1[nxt.first];
        val=max(val, par-dp1[nxt.first]+dp2[nxt.first]+nxt.second);
    }
    dp1[now]=dp2[now]+val;
}

void dfs2(int now, int bef, vector<pll> &V)
{
    int i, j;
    vector<pll> V2;
    for(pii nxt : adj[now])
    {
        if(nxt.first==bef) continue;
        V2.push_back({-dp1[nxt.first]+dp2[nxt.first]+len[nxt.first], nxt.first});
    }
    sort(V2.begin(), V2.end(), greater<pll>());

    if(now==1) dp3[now]=dp4[now]=0;
    else
    {
        dp3[now]=dp4[now]=dp2[bef]-dp1[now]+dp3[bef];

        ll val=-INF;
        for(i=0; i<2 && i<V.size(); i++)
        {
            if(V[i].second!=now)
            {
                val=max(val, V[i].first);
                break;
            }
        }
        dp3[now]=max(dp3[now], dp2[bef]-dp1[now]+len[now]+dp3[bef]+val);
        if(bef!=1) dp3[now]=max(dp3[now], dp2[bef]-dp1[now]+len[now]+len[bef]+dp4[bef]);
    }

    //printf("%d %lld %lld %lld %lld\n", now, dp1[now], dp2[now], dp3[now], dp4[now]);

    for(pii nxt : adj[now])
    {
        if(nxt.first==bef) continue;
        dfs2(nxt.first, now, V2);
    }
}

int main()
{
    int i, j;

    scanf("%d", &N);
    for(i=1; i<N; i++)
    {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }

    dfs(1, 1, -INF);

    vector<pll> T;
    dfs2(1, 1, T);

    for(i=1; i<=N; i++) ans=max(ans, dp2[i]+dp3[i]);
    printf("%lld", ans);
}

Compilation message (stderr)

beads.cpp: In function 'void dfs2(int, int, std::vector<std::pair<long long int, long long int> >&)':
beads.cpp:46:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(i=0; i<2 && i<V.size(); i++)
                         ~^~~~~~~~~
beads.cpp:31:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
beads.cpp: In function 'int main()':
beads.cpp:69:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
beads.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
beads.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &u, &v, &w);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...