답안 #197715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
197715 2020-01-22T13:33:31 Z moonrabbit2 구슬과 끈 (APIO14_beads) C++17
0 / 100
7 ms 5140 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;
typedef tuple<int,int,int> tii;
typedef vector<vector<ll>> mat;
const ll mod=1e9+7;
const int N=2e5+5;
int n,ans,dp1[N],dp2[N],dp3[N];
vector<pii> g[N];
void dfs(int x,int p){
    vector<int> v;
    for(auto &it : g[x]) if(it.fi!=p){
        int u=it.fi,c=it.se;
        dfs(u,x);
        v.emplace_back(max(dp1[u],dp2[u])+c-max({dp1[u],dp2[u],dp3[u]+c}));
        dp1[x]+=max({dp1[u],dp2[u],dp3[u]+c});
    }
    sort(v.begin(),v.end()); reverse(v.begin(),v.end());
    if(v.size()>=2){
        dp2[x]=dp1[x]+v[0]+v[1];
    } else dp2[x]=-1e9;
    if(v.size()>=1&&p){
        dp3[x]=dp1[x]+v[0];
    } else dp3[x]=-1e9;
    //cout<<x<<" : "<<dp1[x]<<" "<<dp2[x]<<" "<<dp3[x]<<"\n";
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0);
	cin>>n;
    for(int u,v,c,i=1;i<n;i++){
        cin>>u>>v>>c;
        g[u].emplace_back(v,c);
        g[v].emplace_back(u,v);
    }
    dfs(1,0);
    cout<<max({dp1[1],dp2[1],dp3[1]});
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 5 ms 5140 KB Output is correct
3 Incorrect 7 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 5 ms 5140 KB Output is correct
3 Incorrect 7 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 5 ms 5140 KB Output is correct
3 Incorrect 7 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 5 ms 5140 KB Output is correct
3 Incorrect 7 ms 4984 KB Output isn't correct
4 Halted 0 ms 0 KB -