結果

問題 No.2634 Tree Distance 3
ユーザー ゆにぽけゆにぽけ
提出日時 2024-02-24 09:26:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,491 ms / 3,000 ms
コード長 6,887 bytes
コンパイル時間 2,197 ms
コンパイル使用メモリ 149,020 KB
実行使用メモリ 53,768 KB
最終ジャッジ日時 2024-02-24 09:28:25
合計ジャッジ時間 90,926 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,396 ms
41,388 KB
testcase_01 AC 2,449 ms
41,388 KB
testcase_02 AC 2,460 ms
41,388 KB
testcase_03 AC 2,491 ms
41,388 KB
testcase_04 AC 2,412 ms
41,388 KB
testcase_05 AC 1,472 ms
38,848 KB
testcase_06 AC 1,483 ms
38,848 KB
testcase_07 AC 1,487 ms
38,848 KB
testcase_08 AC 866 ms
41,232 KB
testcase_09 AC 771 ms
41,280 KB
testcase_10 AC 772 ms
41,176 KB
testcase_11 AC 684 ms
40,884 KB
testcase_12 AC 693 ms
40,952 KB
testcase_13 AC 467 ms
38,656 KB
testcase_14 AC 464 ms
38,272 KB
testcase_15 AC 454 ms
38,508 KB
testcase_16 AC 1,984 ms
46,588 KB
testcase_17 AC 1,011 ms
29,688 KB
testcase_18 AC 1,389 ms
35,600 KB
testcase_19 AC 1,682 ms
40,464 KB
testcase_20 AC 400 ms
16,384 KB
testcase_21 AC 1,551 ms
53,768 KB
testcase_22 AC 1,606 ms
53,768 KB
testcase_23 AC 1,611 ms
53,768 KB
testcase_24 AC 2,485 ms
45,988 KB
testcase_25 AC 2,459 ms
46,072 KB
testcase_26 AC 2,485 ms
47,604 KB
testcase_27 AC 1,444 ms
43,324 KB
testcase_28 AC 1,559 ms
43,324 KB
testcase_29 AC 1,513 ms
42,220 KB
testcase_30 AC 1,358 ms
38,904 KB
testcase_31 AC 1,420 ms
38,900 KB
testcase_32 AC 1,440 ms
38,884 KB
testcase_33 AC 611 ms
29,040 KB
testcase_34 AC 88 ms
9,088 KB
testcase_35 AC 339 ms
18,944 KB
testcase_36 AC 157 ms
12,544 KB
testcase_37 AC 381 ms
21,376 KB
testcase_38 AC 5 ms
6,676 KB
testcase_39 AC 5 ms
6,676 KB
testcase_40 AC 4 ms
6,676 KB
testcase_41 AC 4 ms
6,676 KB
testcase_42 AC 4 ms
6,676 KB
testcase_43 AC 523 ms
16,328 KB
testcase_44 AC 294 ms
12,032 KB
testcase_45 AC 1,935 ms
36,220 KB
testcase_46 AC 1,100 ms
25,052 KB
testcase_47 AC 2,208 ms
39,812 KB
testcase_48 AC 2,238 ms
41,668 KB
testcase_49 AC 2,190 ms
41,660 KB
testcase_50 AC 2,461 ms
41,660 KB
testcase_51 AC 2,285 ms
41,528 KB
testcase_52 AC 2,349 ms
41,660 KB
testcase_53 AC 5 ms
6,676 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 5 ms
6,676 KB
testcase_56 AC 5 ms
6,676 KB
testcase_57 AC 5 ms
6,676 KB
testcase_58 AC 2 ms
6,676 KB
testcase_59 AC 2 ms
6,676 KB
testcase_60 AC 2,083 ms
41,116 KB
testcase_61 AC 2,006 ms
40,860 KB
testcase_62 AC 2,025 ms
41,112 KB
testcase_63 AC 512 ms
37,952 KB
testcase_64 AC 271 ms
26,352 KB
testcase_65 AC 415 ms
36,136 KB
testcase_66 AC 95 ms
13,560 KB
testcase_67 AC 77 ms
12,032 KB
testcase_68 AC 359 ms
37,128 KB
testcase_69 AC 328 ms
37,128 KB
testcase_70 AC 338 ms
37,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
struct CentroidDecomposition
{
	private:
	int N;
	vector<vector<int>> G;
	vector<int> seen,subtree;

	void calc_subtree(int u,int p) 
	{
		subtree[u] = 1;
		for(int v : G[u])
		{
			if(v == p || seen[v])
			{
				continue;
			}
			calc_subtree(v,u);
			subtree[u] += subtree[v];
		}
	}

	int find_centroid(int u,int p,const int total_size)
	{
		for(int v : G[u])
		{
			if(v == p || seen[v])
			{
				continue;
			}
			if(subtree[v] * 2 > total_size)
			{
				return find_centroid(v,u,total_size);
			}
		}
		return u;
	}

	public:

	CentroidDecomposition(int N) : N(N),G(N),seen(N),subtree(N) {}

	void add_edge(int u,int v)
	{
		assert(0 <= u && u < N && 0 <= v && v < N);
		G[u].push_back(v);
		G[v].push_back(u);
	}

	int build(vector<vector<int>> &H,int u)
	{
		static bool first_call = true;
		if(first_call)
		{
			first_call = false;
			H.resize(N);
		}
		calc_subtree(u,-1);
		int total_size = subtree[u];
		int centroid = find_centroid(u,-1,total_size);
		seen[centroid] = 1;
		for(int v : G[centroid])
		{
			if(seen[v])
			{
				continue;
			}
			int next_centroid = build(H,v);
			H[centroid].push_back(next_centroid);
		}
		return centroid;
	}
};
template<class T,T (*op)(T,T),T (*e)()> struct segtree
{
	private:
	int size,N;
	vector<T> node;

	public:
	segtree(int N = 0) : segtree(vector<T>(N,e())) {}

	segtree(const vector<T> &V)
	{
		N = (int)V.size();
		size = 1;
		while(size < N)
		{
			size <<= 1;
		}
		node.assign(2 * size,e());
		for(int i = 0;i < N;i++)
		{
			node[i + size] = V[i];
		}
		for(int i = size - 1;i >= 1;i--)
		{
			node[i] = op(node[2 * i],node[2 * i + 1]);
		}
	}

	void replace(int pos,T x)
	{
		func_update(pos,x,[](T u,T v){return u = v;});
	}

	void add(int pos,T x)
	{
		func_update(pos,x,[](T u,T v){return u + v;});
	}

	void func_update(int pos,T x)
	{
		func_update(pos,x,op);
	}

	void func_update(int pos,T x,T (*func)(T,T))
	{
		assert(0 <= pos && pos < N);
		pos += size;
		node[pos] = func(node[pos],x);
		while(pos >> 1)
		{
			pos >>= 1;
			node[pos] = op(node[2 * pos],node[2 * pos + 1]);
		}
	}

	T operator[](int pos)
	{
		assert(0 <= pos && pos < N);
		return node[pos + size];
	}

	int get_log()
	{
		int res = 0;
		while(1 << res < size)
		{
			res++;
		}
		assert((1 << res) == size);
		return res;
	}

	vector<vector<T>> tree_list()
	{
		vector<vector<T>> res;
		int log = get_log();
		res.reserve(log + 1);
		for(int i = 0;i <= log;i++)
		{
			int L = 1 << log,R = L << 1;
			vector<T> cur(node.begin() + L,node.begin() + R);
			res.push_back(cur);
		}
		return res;
	}

	//suppose that class T has ostream &operator<<
	void print(int k)
	{
		assert(0 <= k && k <= get_log());
		int L = 1 << k,R = L << 1;
		for(int i = L;i < R;i++)
		{
			cerr << node[i] << (i + 1 == R ? "\n":" ");
		}
	}

	void print()
	{
		int log = get_log();
		for(int k = 0;k <= log;k++)
		{
			int L = 1 << k,R = L << 1;
			const string unit((1 << (log - k)) - 1,' ');
			for(int i = L;i < R;i++)
			{
				if(i == L)
				{
					cerr << unit;
				}
				cerr << node[i];
				if(i + 1 < R)
				{
					cerr << unit << unit << ' ';
				}
				else
				{
					cerr << unit;
				}
			}
			cerr << "\n";
		}
	}

	T prod(int l,int r)
	{
		assert(0 <= l && l <= r && r <= N);
		T L = e(),R = e();
		l += size,r += size;
		while(l < r)
		{
			if(l & 1)
			{
				L = op(L,node[l++]);
			}
			if(r & 1)
			{
				R = op(node[--r],R);
			}
			l >>= 1,r >>= 1;
		}
		return op(L,R);
	}
	T prod()
	{
		return node[1];
	}

	template<class F> int max_right(int l,F f)
	{
		assert(0 <= l && l <= N);
		assert(f(e()));
		if(l == N)
		{
			return N;
		}
		l += size;
		T val = e();
		do
		{
			while(!(l & 1))
			{
				l >>= 1;
			}
			if(!f(op(val,node[l])))
			{
				while(l < size)
				{
					l <<= 1;
					if(f(op(val,node[l])))
					{
						val = op(val,node[l++]);
					}
				}
				return l - size;
			}
			val = op(val,node[l++]);
		} while((l & -l) != l);
		return N;
	}

	template<class F> int min_left(int r,F f)
	{
		assert(0 <= r && r <= N);
		assert(f(e()));
		if(r == 0)
		{
			return 0;
		}
		r += size;
		T val = e();
		do
		{
			r--;
			while(r > 1 && (r & 1))
			{
				r >>= 1;
			}
			if(!f(op(node[r],val)))
			{
				while(r < size)
				{
					r <<= 1;
					r++;
					if(f(op(node[r],val)))
					{
						val = op(node[r--],val);
					}
				}
				return r + 1 - size;
			}
			val = op(node[r],val);
		} while((r & -r) != r);
		return 0;
	}
};
int op(int a,int b)
{
	return max(a,b);
}
int e()
{
	return -1;
}
void Main()
{
	int N;
	cin >> N;
	vector<int> A(N);
	for(int i = 0;i < N;i++)
	{
		cin >> A[i];
	}
	vector<int> comp = A;
	sort(comp.begin(),comp.end());
	comp.erase(unique(comp.begin(),comp.end()),comp.end());
	for(int i = 0;i < N;i++)
	{
		A[i] = lower_bound(comp.begin(),comp.end(),A[i]) - comp.begin();
	}
	CentroidDecomposition cd(N);
	vector<vector<int>> G(N);
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		u--; v--;
		cd.add_edge(u,v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	vector<vector<int>> H;
	int r = cd.build(H,0);
	vector<int> seen(N);
	segtree<int,op,e> seg(comp.size());
	vector<int> ans(N);
	vector<pair<int,int>> tmp;
	auto dfs1 = [&](auto self,int u,int p,int d) -> void
	{
		int prod = seg.prod(A[u],comp.size());
		if(prod != -1)
		{
			ans[u] = max(ans[u],prod + d);
		}
		tmp.push_back(make_pair(A[u],d));
		for(int v : G[u])
		{
			if(v == p || seen[v])
			{
				continue;
			}
			self(self,v,u,d + 1);
		}
	};
	auto dfs2 = [&](auto self,int u,int p) -> void
	{
		if(seg[A[u]] != e())
		{
			seg.replace(A[u],e());
		}
		for(int v : G[u])
		{
			if(v == p || seen[v])
			{
				continue;
			}
			self(self,v,u);
		}
	};
	auto divide = [&](auto self,int u) -> void
	{
		seg.func_update(A[u],0);
		for(int t = 0;t < 2;t++)
		{
			for(int v : G[u])
			{
				if(seen[v])
				{
					continue;
				}
				dfs1(dfs1,v,u,1);
				for(const auto &[a,d] : tmp)
				{
					seg.func_update(a,d);
				}
				tmp.clear();
			}
			if(t)
			{
				ans[u] = max(ans[u],seg.prod(A[u],comp.size()));
			}
			for(int v : G[u])
			{
				if(seen[v])
				{
					continue;
				}
				dfs2(dfs2,v,u);
			}
			reverse(G[u].begin(),G[u].end());
		}
		seen[u] = 1;
		seg.replace(A[u],e());
		for(int v : H[u])
		{
			self(self,v);
		}
	};
	divide(divide,r);
	for(int i = 0;i < N;i++)
	{
		cout << ans[i] << (i + 1 == N ? "\n":" ");
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}

0