結果
| 問題 |
No.2634 Tree Distance 3
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2024-02-16 22:51:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,169 ms / 3,000 ms |
| コード長 | 2,177 bytes |
| コンパイル時間 | 4,610 ms |
| コンパイル使用メモリ | 265,624 KB |
| 最終ジャッジ日時 | 2025-02-19 14:34:25 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 69 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:50:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
50 | rep(i,n)scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
main.cpp:55:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
55 | scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#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<long long,6>;
A e(){
A c;
rep(i,6)c[i] = -Inf64;
return c;
}
A op(A a,A b){
A c = e();
rep(i,6){
c[i] = max({c[i],a[i],b[i]});
}
c[3] = max(c[3],a[0]+b[1]);
c[4] = max(c[4],a[1]+b[2]);
c[5] = max({c[5],a[0]+b[4],a[3]+b[2]});
return c;
}
int main(){
int n;
cin>>n;
vector<int> a(n);
rep(i,n)scanf("%d",&a[i]);
vector<vector<int>> E(n);
rep(i,n-1){
int x,y;
scanf("%d %d",&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());
{
vector<A> tt(vs.size());
rep(i,n){
A temp = e();
temp[1] = -2 * d[i];
rep(j,pos[i].size())tt[pos[i][j]] = temp;//seg.set(pos[i][j],temp);
}
seg = segtree<A,op,e>(tt);
}
rep(i,n){
int v = t[i].second;
A temp = e();
temp[0] = d[v];
temp[1] = -2*d[v];
temp[2] = d[v];
temp[3] = -d[v];
temp[4] = -d[v];
temp[5] = 0;
rep(j,pos[v].size()){
A rr;
rr = seg.prod(0,pos[v][j]);
ans[v] = max({ans[v],rr[0]+temp[4],rr[3]+temp[2],temp[5]});
rr = seg.prod(pos[v][j]+1,vs.size());
ans[v] = max({ans[v],temp[0]+rr[4],temp[3]+rr[2],temp[5]});
}
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;
}
沙耶花