QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#133729#4931. Comic Bingewtn135687#WA 2ms6028kbC++141.3kb2023-08-02 13:35:492023-08-02 13:35:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-02 13:35:51]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6028kb
  • [2023-08-02 13:35:49]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll

const int N = 1e5+10,mo = 1e9+7;

int a[N];

vector<pair<int,int>>to[N];
int dp[N][2];//是否余下一条链
void dfs(int u,int fa){
    
    vector<int>ve;
    
    for(auto k:to[u]){
        int v=k.first;
        if(v==fa)continue;
        dfs(v,u);
        dp[u][0]+=dp[v][0];
        ve.push_back(dp[v][1]-dp[v][0]+k.second);

    }
    sort(ve.begin(),ve.end(),greater<int>());
    if(ve.size()>=1){
        dp[u][1]=dp[u][0]+ve[0];
        for(int i=2;i<ve.size();i+=2){
            dp[u][1]=max(dp[u][1],dp[u][1]+ve[i-1]+ve[i]);
        }
    }
    for(int i=1;i<ve.size();i+=2){
        dp[u][0]=max(dp[u][0],dp[u][0]+ve[i-1]+ve[i]);
    }
    // cerr<<"u fa ="<<u<<" "<<fa<<"\n";
    // for(int i:ve)cerr<<i<<" ";cerr<<"\n";
    // cerr<<"u="<<u<<" "<<dp[u][0]<<" "<<dp[u][1]<<"\n";
}
inline void solve(){
    int n;cin>>n;
    for(int i=0;i<n-1;i++){
        int u,v,w;cin>>u>>v>>w;
        to[u].push_back({v,w});to[v].push_back({u,w});
    }
    dfs(1,0);
    cout<<max(dp[1][0],dp[1][1])<<"\n";
}



signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int WTN666=1;//cin>>WTN666;
    while(WTN666--){
        solve();
    }
}






















Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 6028kb

input:

6
3 1 1 1 1 2
1 5 3 3 7 4

output:

147

result:

wrong answer 1st lines differ - expected: '13', found: '147'