QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#46061#2169. 'S No Problemdmga44WA 3ms6872kbC++203.9kb2022-08-25 01:15:402022-08-25 01:15:42

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 01:15:42]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:6872kb
  • [2022-08-25 01:15:40]
  • 提交

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 len2[MAXN];   
ll ma;

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

    sort(allr(xs));

    if(xs.size()>1)
        len2[u]=max(xs[0]+xs[1],(int)len2[u]);
}

void dfs(int u,int p,int d2,int d1)
{
    vector<pii> lens;
    int dd2=d2;
    for(auto x : g[u])
    {
        if(x.first==p)
            continue;
        lens.push_back(pii(len[x.first]+x.second,x.first));
        dd2=max(dd2,(int)len2[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);
    }
    if(lens.size()>=4)
    {
        ma=max(ma,lens[0].first+lens[1].first+lens[2].first+lens[3].first);
        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=dd2;
        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;
}
/*
7
1 2 3
1 3 4
3 4 5
3 5 6
3 6 7
6 7 8
*/

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 6872kb

input:

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

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 2ms
memory: 6608kb

input:

6
6 2 1
4 6 1
3 1 1
1 5 1
1 6 10

output:

15

result:

ok single line: '15'

Test #3:

score: -100
Wrong Answer
time: 3ms
memory: 6100kb

input:

6
1 3 1
3 2 1
3 4 10
5 4 1
4 6 1

output:

4

result:

wrong answer 1st lines differ - expected: '15', found: '4'