結果

問題 No.2634 Tree Distance 3
ユーザー 沙耶花
提出日時 2024-02-16 22:42:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,230 bytes
コンパイル時間 4,344 ms
コンパイル使用メモリ 267,772 KB
最終ジャッジ日時 2025-02-19 14:28:23
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 39 TLE * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:56:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   56 |         rep(i,n)scanf("%d",&a[i]);
      |                 ~~~~~^~~~~~~~~~~~
main.cpp:61:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   61 |                 scanf("%d %d",&x,&y);
      |                 ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#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){
		for(int j=i;j<4;j++){
			for(int k=j;k<4;k++){
				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)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] = -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][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;
}
0