QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#46058#2169. 'S No Problemdmga44#WA 1ms5944kbC++203.4kb2022-08-25 00:31:022022-08-25 00:31:05

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-25 00:31:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5944kb
  • [2022-08-25 00:31:02]
  • 提交

answer

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops", "omit-frame-pointer", "inline")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native")
// #pragma GCC option("arch=native", "no-zero-upper") // Enable AVX

/// UH Top
#include <bits/stdc++.h>
#define db(x) cerr << #x << ':' << (x) << '\n';
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
// #define int ll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// typedef __int128_t int128;
typedef pair<ll, ll> pii;
typedef pair<ld, ll> pdi;
typedef pair<ld, ld> pdd;
typedef pair<ld, pdd> pdp;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<string, string> pss;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
typedef complex<ld> point;
typedef vector<point> polygon;
typedef vector<ll> vi;
typedef pair<point, int> ppi;
#define prec(n)        \
    cout.precision(n); \
    cout << fixed
const ll mod = (1e9 + 7);
const ld eps = (1e-9);
const ll oo = (ll)(1e18 + 5);
#define pi acos(-1)
#define MAXN (ll)(1e5 + 5)

vector<pii> g[MAXN];
ll len[MAXN];
ll ma;

void dfs1(int u,int p)
{
    for(auto x : g[u])
    {
        if(x.first==p)
            continue;
        dfs1(x.first,u);
        len[u]=max(len[x.first]+x.second,len[u]);
    }
}

void dfs(int u,int p,int d2,int d1)
{
    vector<pii> lens;
    for(auto x : g[u])
    {
        if(x.first==p)
            continue;
        lens.push_back(pii(len[x.first]+x.second,x.first));
    }

    sort(allr(lens));

    if(lens.size()==1)
    {
        ma=max(ma,d2+lens[0].first);
        ma=max(ma,d1+lens[0].first);
    }
    if(lens.size()==2)
    {
        ma=max(ma,d2+lens[0].first+lens[1].first);
        ma=max(ma,d1+lens[0].first+lens[1].first);
    }
    if(lens.size()>=3)
    {
        ma=max(ma,d2+lens[0].first+lens[1].first);
        ma=max(ma,d1+lens[0].first+lens[1].first+lens[2].first);
    }
    for(auto x : g[u])
    {
        if(x.first==p)
            continue;
        ll dx2=d2;
        ll dx1=d1;
        if(x.first==lens[0].second)
        {
            if(lens.size()>1)
            {
                dx2=max(dx2,d1+lens[1].first);
                dx1=max(dx1,lens[1].first);
            }
            if(lens.size()>2)
                dx2=max(dx2,lens[2].first+lens[1].first);
        }
        else
        {
            dx2=max(dx2,d1+lens[0].first);
            dx1=max(dx1,lens[0].first);
            if(lens.size()>1)
            {
                if(x.first==lens[1].second)
                {
                    if(lens.size()>2)
                        dx2=max(dx2,lens[2].first+lens[0].first);
                }
                else
                {
                    dx2=max(dx2,lens[1].first+lens[0].first);
                }
            }
        }

        dx1+=x.second;
        dfs(x.first,u,dx2,dx1);
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    ll ans=0;
    for(int i=0;i<n-1;i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ans+=2*w;
        u--,v--;
        g[u].push_back(pii(v,w));
        g[v].push_back(pii(u,w));
    }

    dfs1(0,-1);

    dfs(0,-1,0,0);

    cout << ans-ma << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5944kb

input:

5
1 2 1
1 3 2
1 4 3
1 5 4

output:

11

result:

wrong answer 1st lines differ - expected: '10', found: '11'