結果
| 問題 |
No.2634 Tree Distance 3
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2024-02-16 22:39:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,087 bytes |
| コンパイル時間 | 5,499 ms |
| コンパイル使用メモリ | 267,904 KB |
| 最終ジャッジ日時 | 2025-02-19 14:21:54 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 2 |
| other | TLE * 1 -- * 68 |
ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001
vector<int> vs;
vector<vector<int>> pos;
long long d[300005];
void dfs(int cv,int pv,vector<vector<int>> &E){
if(pv==-1)d[cv] = 0;
else d[cv] = d[pv] + 1;
pos[cv].push_back(vs.size());
vs.push_back(cv);
rep(i,E[cv].size()){
int to = E[cv][i];
if(to==pv)continue;
dfs(to,cv,E);
pos[cv].push_back(vs.size());
vs.push_back(cv);
}
}
using A = array<array<long long,4>,4>;
A e(){
A c;
rep(i,4){
rep(j,4){
if(i==j)c[i][j] = 0;
else c[i][j] = -Inf64;
}
}
return c;
}
A op(A a,A b){
A c = e();
rep(i,4){
rep(j,4){
rep(k,4){
c[i][k] = max(c[i][k],a[i][j] + b[j][k]);
}
}
}
return c;
}
int main(){
int n;
cin>>n;
vector<int> a(n);
rep(i,n)cin>>a[i];
vector<vector<int>> E(n);
rep(i,n-1){
int x,y;
cin>>x>>y;
x--,y--;
E[x].push_back(y);
E[y].push_back(x);
}
pos.resize(n);
dfs(0,-1,E);
vector<long long> ans(n,-Inf64);
rep(_,2){
vector<pair<int,int>> t;
rep(i,n){
if(_)t.emplace_back(a[i],i);
else t.emplace_back(-a[i],i);
}
sort(t.rbegin(),t.rend());
if(_){
}
else{
reverse(t.begin(),t.end());
}
segtree<A,op,e> seg(vs.size());
rep(i,n){
A temp = e();
temp[1][2] = -2 * d[i];
rep(j,pos[i].size())seg.set(pos[i][j],temp);
}
rep(i,n){
int v = t[i].second;
A temp = e();
temp[0][1] = d[v];
temp[1][2] = -2*d[v];
temp[2][3] = d[v];
temp[0][2] = -d[v];
temp[1][3] = -d[v];
temp[0][3] = 0;
rep(j,pos[v].size()){
A rr;
rr = seg.prod(0,pos[v][j]);
rep(k,3){
ans[v] = max(ans[v],rr[0][k] + temp[k][3]);
}
rr = seg.prod(pos[v][j]+1,vs.size());
rep(k,3){
ans[v] = max(ans[v],temp[0][k+1] + rr[k+1][3]);
}
}
rep(j,pos[v].size()){
seg.set(pos[v][j],temp);
}
}
}
rep(i,n){
if(i!=0)cout<<' ';
cout<<ans[i];
}
cout<<endl;
return 0;
}
沙耶花