/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 25ms 532.0 KiB
#3 Wrong Answer 49ms 640.0 KiB

Code

#include<bits/stdc++.h>

using namespace std;
using pl=pair<long long,long long>;
using Graph=vector<vector<pl>>;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while(t>0){
    t--;
    long long n;
    cin >> n;
    vector<long long> a(n);
    for(auto &nx : a){cin >> nx;}
    Graph g(n);
    for(long long i=1;i<n;i++){
      long long u,v,w;
      cin >> u >> v >> w;
      u--; v--; w*=-2;
      g[u].push_back({v,w});
      g[v].push_back({u,w});
    }
    queue<long long> q;
    vector<long long> sq;
    vector<long long> par(n,-1);
    q.push(0);
    par[0]=-2;
    while(!q.empty()){
      auto od=q.front(); q.pop();
      sq.push_back(od);
      for(auto &nx : g[od]){
        if(par[nx.first]==-1){
          par[nx.first]=od;
          q.push(nx.first);
        }
      }
    }
    long long res=-8e18;
    vector<long long> dp(n,-8e18);
    reverse(sq.begin(),sq.end());
    for(auto &v : sq){
      vector<long long> mem;
      for(auto &w : g[v]){
        if(w.first==par[v]){continue;}
        mem.push_back(dp[w.first]+w.second);
      }
      sort(mem.rbegin(),mem.rend());
      if(mem.size()>=2){
        res=max(res,mem[0]+mem[1]+a[v]);
      }
      dp[v]=0;
      if(mem.size()>=1){
        res=max(res,mem[0]+a[v]);
        dp[v]=max(dp[v],mem[0]);
      }
      dp[v]+=a[v];
    }
    cout << res << "\n";
    // for(auto &nx : sq){cout << nx << " ";}cout << "\n";
    // for(auto &nx : par){cout << nx << " ";}cout << "\n";
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1205 E. Make Cycle in Tree
Contest
Educational Round 1
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 16:10:48
Judged At
2025-07-14 16:10:48
Judged By
Score
0
Total Time
49ms
Peak Memory
640.0 KiB