//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n;
vector<ii> al[200005];
map<ii,ii> memo;
map<ii,int> w;
multiset<int,greater<int> > s[200005];
int pa[200005];
int tot[200005];
ii dp(int i,int p){
if (memo.count(ii(i,p))) return memo[ii(i,p)];
ii res;
if (!pa[i]){
s[i].insert(-1e9);
for (auto &it:al[i]){
if (it.fi==p) continue;
auto temp=dp(it.fi,i);
ll val=max(temp.fi,temp.se+it.se);
tot[i]+=val;
s[i].insert(temp.fi+it.se-val);
}
res=ii(tot[i],tot[i]+*s[i].begin());
pa[i]=p;
}
else{
if (pa[i]!=-1){
auto temp=dp(pa[i],i);
ll val=max(temp.fi,temp.se+w[ii(i,pa[i])]);
tot[i]+=val;
s[i].insert(temp.fi+w[ii(i,pa[i])]-val);
pa[i]=-1;
}
if (p!=-1){
auto temp=dp(p,i);
ll val=max(temp.fi,temp.se+w[ii(i,p)]);
tot[i]-=val;
s[i].erase(s[i].find(temp.fi+w[ii(i,p)]-val));
}
res=ii(tot[i],tot[i]+*s[i].begin());
if (p!=-1){
auto temp=dp(p,i);
ll val=max(temp.fi,temp.se+w[ii(i,p)]);
tot[i]+=val;
s[i].insert(temp.fi+w[ii(i,p)]-val);
}
}
return memo[ii(i,p)]=res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n;
int a,b,c;
rep(x,1,n){
cin>>a>>b>>c;
al[a].push_back(ii(b,c));
al[b].push_back(ii(a,c));
w[ii(a,b)]=c;
w[ii(b,a)]=c;
}
ll ans=0;
rep(x,1,n+1) ans=max(ans,dp(x,-1).fi);
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
11 ms |
14592 KB |
Output is correct |
17 |
Correct |
11 ms |
14592 KB |
Output is correct |
18 |
Correct |
11 ms |
14592 KB |
Output is correct |
19 |
Correct |
11 ms |
14592 KB |
Output is correct |
20 |
Correct |
12 ms |
14592 KB |
Output is correct |
21 |
Correct |
12 ms |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
11 ms |
14592 KB |
Output is correct |
17 |
Correct |
11 ms |
14592 KB |
Output is correct |
18 |
Correct |
11 ms |
14592 KB |
Output is correct |
19 |
Correct |
11 ms |
14592 KB |
Output is correct |
20 |
Correct |
12 ms |
14592 KB |
Output is correct |
21 |
Correct |
12 ms |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14592 KB |
Output is correct |
23 |
Correct |
31 ms |
17280 KB |
Output is correct |
24 |
Correct |
32 ms |
17280 KB |
Output is correct |
25 |
Correct |
31 ms |
17272 KB |
Output is correct |
26 |
Correct |
64 ms |
19960 KB |
Output is correct |
27 |
Correct |
63 ms |
20088 KB |
Output is correct |
28 |
Correct |
57 ms |
19956 KB |
Output is correct |
29 |
Correct |
56 ms |
20088 KB |
Output is correct |
30 |
Correct |
61 ms |
20216 KB |
Output is correct |
31 |
Correct |
64 ms |
20604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 ms |
14464 KB |
Output is correct |
3 |
Correct |
10 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
10 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
11 ms |
14592 KB |
Output is correct |
17 |
Correct |
11 ms |
14592 KB |
Output is correct |
18 |
Correct |
11 ms |
14592 KB |
Output is correct |
19 |
Correct |
11 ms |
14592 KB |
Output is correct |
20 |
Correct |
12 ms |
14592 KB |
Output is correct |
21 |
Correct |
12 ms |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14592 KB |
Output is correct |
23 |
Correct |
31 ms |
17280 KB |
Output is correct |
24 |
Correct |
32 ms |
17280 KB |
Output is correct |
25 |
Correct |
31 ms |
17272 KB |
Output is correct |
26 |
Correct |
64 ms |
19960 KB |
Output is correct |
27 |
Correct |
63 ms |
20088 KB |
Output is correct |
28 |
Correct |
57 ms |
19956 KB |
Output is correct |
29 |
Correct |
56 ms |
20088 KB |
Output is correct |
30 |
Correct |
61 ms |
20216 KB |
Output is correct |
31 |
Correct |
64 ms |
20604 KB |
Output is correct |
32 |
Correct |
423 ms |
43256 KB |
Output is correct |
33 |
Correct |
402 ms |
43256 KB |
Output is correct |
34 |
Correct |
408 ms |
43256 KB |
Output is correct |
35 |
Execution timed out |
1053 ms |
95992 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |