QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#46060 | #2169. 'S No Problem | dmga44# | WA | 3ms | 6704kb | C++20 | 3.6kb | 2022-08-25 00:37:05 | 2022-08-25 00:37:06 |
Judging History
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);
}
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=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: 100
Accepted
time: 3ms
memory: 6648kb
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: 0ms
memory: 5884kb
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: 0
Accepted
time: 0ms
memory: 6292kb
input:
6 1 3 1 3 2 1 3 4 10 5 4 1 4 6 1
output:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 3ms
memory: 6548kb
input:
6 1 3 1 2 3 1 3 5 1 4 5 1 5 6 1
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 3ms
memory: 6704kb
input:
6 1 2 5 3 2 3 2 4 1 4 5 2 4 6 4
output:
16
result:
ok single line: '16'
Test #6:
score: 0
Accepted
time: 3ms
memory: 6016kb
input:
6 1 6 8 2 6 2 3 6 3 4 6 5 5 6 1
output:
20
result:
ok single line: '20'
Test #7:
score: 0
Accepted
time: 3ms
memory: 6308kb
input:
7 6 4 60 4 2 463 6 7 165 6 3 81 6 1 30 4 5 214
output:
1103
result:
ok single line: '1103'
Test #8:
score: 0
Accepted
time: 3ms
memory: 6164kb
input:
7 1 3 463 1 5 214 7 2 165 7 4 81 7 1 60 7 6 30
output:
1103
result:
ok single line: '1103'
Test #9:
score: 0
Accepted
time: 3ms
memory: 6012kb
input:
8 8 7 10 1 3 1 3 2 1 6 5 2 5 4 1 3 7 4 7 5 3
output:
24
result:
ok single line: '24'
Test #10:
score: -100
Wrong Answer
time: 3ms
memory: 6020kb
input:
8 1 2 1 2 3 1 3 4 10 3 5 10 2 6 1 6 7 10 6 8 10
output:
54
result:
wrong answer 1st lines differ - expected: '46', found: '54'