[CSP-S Simulation Test]: Endless Fantasy (DFS)

Title Description

In two teenage $ cenbo $ imagine that he ruled the $ Euphoric \ Field $. He thus began a $ Endless \ Fantasy $.
$ Euphoric \ Field $ have $ n $ city, $ m $ nationalities. Between cities are connected by $ n-1 $ city road formed a $ 1 $ rooted tree root. Each city is a national settlements, $ cenbo $ $ i $ the first nation to know a city is $ A_i $, the number is $ B_i $. In order to maintain stability, $ cenbo $ need to know within a certain area of the largest number of people. He made $ n $ th ask you, first of which $ i $ th inquiry is: to seek $ i $ is within the sub-tree root, the largest number of nations which have, how many people in this nation. If the largest number of people in the sub-tree of the nation more than the output of which the smallest number of people.


Input Format

The first line has two integers $ n, m $.
$ N-1 $ next row, each row having two integers $ u, v $, $ represents a connection and U $ $ $ V road.
N-$ $ next row, the row has two $ I $ integers $ A_i, B_i $.


Output Format

I-th row two integers $ x, y $, respectively, to $ i $ subtree is the root of the largest number of ethnic and its number.


Sample

Sample input:

8 6
1 2
1 3
2 4
4 5
3 6
5 7
1 8
2 8
2 5
1 1
3 1
6 7
5 6
1 10
4 6

Sample output:

2 13
1 10
5 6
1 10
1 10
5 6
1 10
4 6


Data range and tips

$ 30 \% $ data, $ n-\ leqslant 4,000 $;
$ 60 \% $ data, $ n-\ leqslant 40,000 $;
$ 100 \% $ data, $ n \ leqslant 400,000 $, $ m \ leqslant n $, $ 1 \ leqslant A_i \ leqslant m $, $ 0 \ leqslant B_i \ leqslant 1,000 $.
Please use the large input files to optimize read.


answer

Have to admit, this question can indeed merge with the $ A $ off tree line.

But I did not play a positive solution.

But look closely, it does not modify the operation, so we can consider another method, first find heavy son, then violence statistics answer, in theory, will be stuck.

Time complexity: $ \ Theta ($ metaphysics $) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
struct rec{int nxt,to;}e[1000000];
int head[400001],cnt;
int n,m;
int a[400001],b[400001];
bool vis[400001];
int son[400001],size[400001];
int v[400001];
long long sum[400001];
pair<int,long long> ans[400001];
void add(int x,int y)
{
	e[++cnt].nxt=head[x];
	e[cnt].to=y;
	head[x]=cnt;
}
void connect(int x)
{
	if(v[a[x]]!=sum[0])
	{
		v[a[x]]=sum[0];
		sum[a[x]]=b[x];
	}
	else sum[a[x]]+=b[x];
	if(sum[a[x]]>b[0])
	{
		a[0]=a[x];
		b[0]=sum[a[x]];
	}
	if(sum[a[x]]==b[0]&&a[x]<a[0])a[0]=a[x],b[0]=sum[a[x]];
}
void find(int x,int fa)
{
	connect(x);
	for(int i=head[x];i;i=e[i].nxt)
		if(e[i].to!=fa)find(e[i].to,x);
}
void pre_dfs(int x)
{
	vis[x]=1;
	size[x]=1;
	for(int i=head[x];i;i=e[i].nxt)
		if(!vis[e[i].to])
		{
			pre_dfs(e[i].to);
			size[x]+=size[e[i].to];
			if(size[e[i].to]>size[son[x]])son[x]=e[i].to;
		}
}
void pro_dfs(int x,int fa)
{
	if(!x)return;
	for(int i=head[x];i;i=e[i].nxt)
		if(e[i].to!=fa&&son[x]!=e[i].to)
		{
			pro_dfs(e[i].to,x);
			sum[0]++;
			b[0]=-1;
		}
	pro_dfs(son[x],x);
	for(int i=head[x];i;i=e[i].nxt)
		if(e[i].to!=fa&&son[x]!=e[i].to)
			find(e[i].to,x);
	connect(x);
	ans[x]=make_pair(a[0],b[0]);
}
int main()
{
	scanf("%d%d",&n,&m);
	sum[0]=1;b[0]=-1;
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
	}
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i],&b[i]);
	pre_dfs(1);
	pro_dfs(1,0);
	for(int i=1;i<=n;i++)printf("%d %lld\n",ans[i].first,ans[i].second);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11570278.html