根的转移变化-洛谷P3478 STA-Station & CF219D Choosing Capital for Treeland

时间:2020-10-21
本文章向大家介绍根的转移变化-洛谷P3478 STA-Station & CF219D Choosing Capital for Treeland,主要包括根的转移变化-洛谷P3478 STA-Station & CF219D Choosing Capital for Treeland使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

洛谷P3478 STA-Station 题目连接:https://www.luogu.com.cn/problem/P3478
CF219D Choosing Capital for Treeland 题目连接:https://codeforces.com/contest/219/problem/D
CSDN食用连接:https://blog.csdn.net/qq_43906000/article/details/109210673

STA-Station

题目描述

给定一个 n 个点的树,请求出一个结点,使得以这个结点为根时,所有结点的深度之和最大。

一个结点的深度之定义为该节点到根的简单路径上边的数量。

输入格式
第一行有一个整数,表示树的结点个数 n。
接下来 \((n - 1)\) 行,每行两个整数 \(u, v\),表示存在一条连接 \(u, v\) 的边。

输出格式
本题存在 Special Judge。

输出一行一个整数表示你选择的结点编号。如果有多个结点符合要求,输出任意一个即可。

输入输出

输入
8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
输出
7

说明/提示
样例 1 解释
输出 7 和 8 都是正确答案。

数据规模与约定
对于全部的测试点,保证 \(1 \leq n \leq 10^6\)\(1 \leq u, v \leq n\),给出的是一棵树。

看起来有点乱,但实际上我们只需要算一个根节点的答案就好了,其他的都可以通过规律直接推出来,我们可以手动找找规律,当根从一个节点a转移到另一个节点b的时候它的所有节点深度之和会发生什么变化? 我们很容易知道,所有b的儿子包括它自己的距离都会减少1,而所有不是b这个子树的节点的深度都会增加1,也就是:
\(sum[v]=sum[u]+(n-son[v]-1)-son[v]-1\)
于是答案也就出来了。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

const int mac=1e6+10;
typedef long long ll;

struct Edge{
	int to,next;
}eg[mac<<1];
int head[mac],num=0;
int son[mac],deep[mac];
ll sum[mac];
ll tot=0;
int id=1,n;

void add(int u,int v)
{
	eg[num++]=Edge{v,head[u]};
	head[u]=num-1;
}

void dfs(int fa,int u)
{
	for (int i=head[u]; i!=-1; i=eg[i].next){
		int v=eg[i].to;
		if (v==fa) continue;
		deep[v]=deep[u]+1;
		tot+=deep[v];
		dfs(u,v);
		son[u]+=son[v]+1;
	}
}

void move(int fa,int u)
{
	for (int i=head[u]; i!=-1; i=eg[i].next){
		int v=eg[i].to;
		if (v==fa) continue;
		sum[v]=sum[u]+(n-son[v]-1)-son[v]-1;
		move(u,v);
	}
}

int main(int argc, char const *argv[])
{
	scanf ("%d",&n);	
	memset(head,-1,sizeof head);
	for (int i=1; i<n; i++){
		int u,v;
		scanf ("%d%d",&u,&v);
		add(u,v); add(v,u);
	}
	dfs(-1,1);
	sum[1]=tot;
	move(-1,1);
	ll dis=0;
	for (int i=1; i<=n; i++) {
		if (sum[i]>dis) {dis=sum[i];  id=i;}
	}
	printf ("%d\n",id);
	return 0;
}

Choosing Capital for Treeland

The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.

The council of the elders has recently decided to choose the capital of Treeland. Of course it should be a city of this country. The council is supposed to meet in the capital and regularly move from the capital to other cities (at this stage nobody is thinking about getting back to the capital from these cities). For that reason if city a is chosen a capital, then all roads must be oriented so that if we move along them, we can get from city a to any other city. For that some roads may have to be inversed.

Help the elders to choose the capital so that they have to inverse the minimum number of roads in the country.

Input

The first input line contains integer \(n (2 ≤ n ≤ 2\times10^5)\) — the number of cities in Treeland. Next n - 1 lines contain the descriptions of the roads, one road per line. A road is described by a pair of integers \(s_i, t_i (1 ≤ s_i, t_i ≤ n; s_i ≠ t_i)\) — the numbers of cities, connected by that road. The i-th road is oriented from city si to city ti. You can consider cities in Treeland indexed from 1 to n.

Output

In the first line print the minimum number of roads to be inversed if the capital is chosen optimally. In the second line print all possible ways to choose the capital — a sequence of indexes of cities in the increasing order.

Examples

input
3
2 1
2 3
output
0
2
input
4
1 4
2 4
3 4
output
2
1 2 3

题目大意:有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都,那么为了使首都能到达任意一个城市,不得不将一些道路翻转方向,记翻转道路的条数为k。你的任务是找到所有满足k最小的首都。

实际上这题也是只需要计算一个节点作为根节点时候的答案就行了,其他的情况我们同样可以推出来,加上现在节点u需要翻转的边数为x,那么如果转移到u的儿子v,我们可以知道,答案为x+1,因为我们需要从儿子节点v到父节点u,这一步是逆行的,如果转移的不是儿子节点,那么也就是说他转移到了它的父亲节点(这里的父亲有多个),那么我们可以知道的是,答案为x-1,因为从父节点到儿子节点是顺的。

那么则有转移公式:\(if (son[u]==v) nb[v]=nb[u]-1;else\ nb[v]=nb[u]+1;\)

那么答案也就出来了。
以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mke make_pair 
typedef pair<int,int> pill;
const int inf=1e9+10;
const int mac=2e5+10;
vector<int>con[mac];
map<pill,int>mp;
int nb[mac],ans=inf;

void dfs(int fa,int u)
{
	for (auto v:con[u]){
		if (v==fa) continue;
		pill s={0,0};
		s.fi=u; s.se=v;
		if (!mp[s]) nb[1]++;
		dfs(u,v);
	}
}

void dfs2(int fa,int u)
{
	for (auto v:con[u]){
		if (v==fa) continue;
		pill s={0,0};
		s.fi=u; s.se=v;
		if (!mp[s]) nb[v]=nb[u]-1;
		else nb[v]=nb[u]+1;
		ans=min(ans,nb[v]);
		dfs2(u,v);
	}
}

int main(int argc, char const *argv[])
{
	int n;
	scanf ("%d",&n);
	for (int i=1; i<n; i++){
		int u,v;
		scanf ("%d%d",&u,&v);
		mp[mke(u,v)]=1;
		con[u].push_back(v); con[v].push_back(u);
	}
	dfs(-1,1);
	ans=nb[1];
	dfs2(-1,1);
	printf("%d\n",ans);
	for (int i=1; i<=n; i++)
		if (nb[i]==ans) printf("%d ",i);
	return 0;
}

原文地址:https://www.cnblogs.com/lonely-wind-/p/13855144.html