根的转移变化-洛谷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
博客园食用连接:https://www.cnblogs.com/lonely-wind-/p/13855144.html

STA-Station

题目描述

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

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

输入格式
第一行有一个整数,表示树的结点个数 n。
接下来 ( n − 1 ) (n - 1) (n1) 行,每行两个整数 u , v u, v u,v,表示存在一条连接 u , v 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 ≤ n ≤ 1 0 6 1 \leq n \leq 10^6 1n106 1 ≤ u , v ≤ n 1 \leq u, v \leq n 1u,vn,给出的是一棵树。

看起来有点乱,但实际上我们只需要算一个根节点的答案就好了,其他的都可以通过规律直接推出来,我们可以手动找找规律,当根从一个节点a转移到另一个节点b的时候它的所有节点深度之和会发生什么变化? 我们很容易知道,所有b的儿子包括它自己的距离都会减少1,而所有不是b这个子树的节点的深度都会增加1,也就是:
s u m [ v ] = s u m [ u ] + ( n − s o n [ v ] − 1 ) − s o n [ v ] − 1 sum[v]=sum[u]+(n-son[v]-1)-son[v]-1 sum[v]=sum[u]+(nson[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 × 1 0 5 ) n (2 ≤ n ≤ 2\times10^5) n(2n2×105) — 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 ) s_i, t_i (1 ≤ s_i, t_i ≤ n; s_i ≠ t_i) si,ti(1si,tin;si=ti) — 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,因为从父节点到儿子节点是顺的。

那么则有转移公式: i f ( s o n [ u ] = = v ) n b [ v ] = n b [ u ] − 1 ; e l s e   n b [ v ] = n b [ u ] + 1 ; if (son[u]==v) nb[v]=nb[u]-1;else\ nb[v]=nb[u]+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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值