【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)

题干:

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·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 si, ti (1 ≤ si, ti ≤ nsi ≠ 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-1条有向边,边的方向可以改变,问最少改变多少条边可以从某一个点到达任意一个点,同时求出这些点。

解题报告:

给一段一句话题解:

要改变的边的权值为1,不需要改变的边的权值为0,两次dfs,第一次算出以1点为根节点到所有点要改变的边数,第二次以1为根节点向下遍历节点3 算出每一个点到达所有点要改变的边数,dp[son]+=(dp[root]-dp[son])+((tree[i].val)?-1:1),某一点的值是他父节点4 的值减去他以前的值再考虑他与父节点之间的边的方向。

  其实就是两边dfs就好了,第一遍是正常的递归以cur为根的状态值,第二遍求出转移到cur的那一半的状态值。也就是第一次dfs是用儿子更新父亲,第二次dfs是用父亲处理一下之后来更新儿子。好题啊精妙的!!

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int n;
int a,b;
ll dp[MAX];//指向我的有多少条边 
ll ans[MAX];
struct Node {int to;bool f;//1==实边,0==虚边 Node(){}Node(int to,int f):to(to),f(f){}
};
vector<Node> vv[MAX];
void dfs(int cur,int root) {int up = vv[cur].size();for(int i = 0; i<up; i++) {Node v = vv[cur][i];if(v.to == root) continue;dfs(v.to,cur);dp[cur] += dp[v.to];if(v.f == 0) dp[cur]++; }
}
void DFS(int cur,int root) {int up = vv[cur].size();for(int i = 0; i<up; i++) {Node v = vv[cur][i];if(v.to == root) continue;dp[v.to] += (dp[cur]-dp[v.to]);if(v.f) dp[v.to]++;else dp[v.to]--;DFS(v.to,cur);}
}
int main()
{cin>>n;for(int i = 1; i<=n-1; i++) {scanf("%d%d",&a,&b);vv[a].pb(Node(b,1));vv[b].pb(Node(a,0));}dfs(1,-1);DFS(1,-1);ll minn = 0x3f3f3f3f3f;for(int i = 1; i<=n; i++) minn = min(minn,dp[i]);printf("%lld\n",minn);int ans = 0;for(int i = 1; i<=n; i++) {if(minn == dp[i]) printf("%d ",i);}return 0 ;}

总结: 代码中DFS中无意间调用了dfs,TLE8了,,但是没有wa,,仔细想一下确实是不会wa的,,这里的dp不会再重新算一遍,因为叶子结点的dp永远没有改变过,所以不会出现多调用一次dfs就会使每个节点dp的值成倍的增加的现象。所以其实在这里多调用一次dfs顶多算是重新求了一次dp的值,并不会使dp改变成原来的多少倍那样、、

注意DFS的更新顺序,,不然就会出错!!总之牢牢把握住,更新状态的前提是所用的值一定是所需的完成值就好了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441171.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库分页查询mysql_各种数据库的分页查询SQL语句总结

1.oracle数据库分页select * from (select a.*,rownum rc from 表名 where rownum<endrow) a where a.rc>startrow2.DB2数据库分页Select * from (select rownumber() over() as rc,a.* from (select * from 表名 order by 列名) as a) where rc between startrow and en…

【HihoCoder - 1880】地铁环线 (前缀和,水题,模拟)

题干&#xff1a; H市有一环线地铁&#xff0c;一共包含N站&#xff0c;编号1~N。正向行驶的地铁会按1 -> 2 -> 3 -> ... -> N -> 1的方向行驶&#xff0c;反向会按1 -> N -> N-1 -> ... -> 3 -> 2 -> 1的方向行驶。 给定所有相邻两站之间…

unsigned int mysql_mysql 中int类型字段unsigned和signed的探索

转自&#xff1a;http://www.0791quanquan.com/news_keji/topic_816453/探索一&#xff1a;正负数问题拿tinyint字段来举例&#xff0c;unsigned后&#xff0c;字段的取值范围是0-255&#xff0c;而signed的范围是-128 - 127。 那么如果我们在明确不需要负值存在的情况下&#…

【HihoCoder - 1881】特殊任务 (树形图,遍历)

题干&#xff1a; H公司一共有N名员工&#xff0c;编号1~N&#xff0c;其中CEO是1号员工。除了CEO之外&#xff0c;其他员工都有唯一的直接上司&#xff0c;所以N名员工上下级关系恰好形成了一棵树形结构。 我们知道每一名员工向H公司的代码库贡献了多少行代码。具体来说&a…

mysql 主从 keepalived_MySQL之双向主从加keepalived高可用

最近在做MySQL数据库的双向主从&#xff0c;了解到keepalived能够自动判断并切换到可用数据库&#xff0c;自己试了一下&#xff0c;整理出文档来。先声明一下环境iptables开启3306端口或者关掉&#xff0c;关闭selinuxMySQL-01&#xff1a;192.168.204.138MySQL-02&#xff1a…

关于vector的size()的使用问题

vector<int> vec; for (int i 0; i < vec.size() - 1; i) &#xff5b; vec[i] &#xff5d; 这里会有个隐藏问题&#xff0c;当vec的size为0时&#xff0c;因为size&#xff08;&#xff09;函数的返回值是无符号整型&#xff0c;这时vec.size() - 1是一个正数…

mysql密码命名规则_MySql命名规范

数据库环境dev&#xff1a;开发环境&#xff0c;开发可读写&#xff0c;可修改表结构。开发人员可以修改表结构&#xff0c;可以随意修改其中的数据但是需要保证不影响其他开发同事。qa&#xff1a;测试环境&#xff0c;开发可读写&#xff0c;开发人员可以通过工具修改表结构。…

【CodeVS - 3639】(树的重心模板,裸题)

题干&#xff1a; 题目描述 Description 给出一棵树&#xff0c;求出树的中心。 为了定义树的中心&#xff0c;首先给每个结点进行标号。对于一个结点K&#xff0c;如果把K从树中删除&#xff08;连同与它相连的边一起&#xff09;&#xff0c;剩下的被分成了很多块&#xf…

go-mysql查询单条数据_Golang 从 MySQL 数据库读取一条数据

刚开始在 sqlx 和 gorm 两者间纠结了半天&#xff0c;准备使用 sqlx 了&#xff0c;看了文档&#xff0c;感觉就是灾难般的文档。我按照例子硬是没有从 MySQL 读出数据。又尝试了 gorm&#xff0c;这家伙的文档更加不友好&#xff0c;概念更多。调试了半天&#xff0c;返回的结…

【HYSBZ - 1088 】扫雷Mine (简单dp)

题干&#xff1a; 相信大家都玩过扫雷的游戏。那是在一个n*m的矩阵里面有一些雷&#xff0c;要你根据一些信息找出雷来。万圣节到了 &#xff0c;“余”人国流行起了一种简单的扫雷游戏&#xff0c;这个游戏规则和扫雷一样&#xff0c;如果某个格子没有雷&#xff0c;那么它里…

docker启动mysql容器_Docker容器开机自启动

查看所有容器[vagrantlocalhost ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f142f72d7e8 redis "docker-entr…

【CodeForces - 527C】Glass Carving(线段树或者SBT或者set)

题干&#xff1a; Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm    h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understand…

mysql常驻内存_MySQL的内存和相关问题排查

我们都知道数据库是IO密集型一类应用&#xff0c;为了提高其性能大量使用内存代替文件(交换分区)的IO操作是保证数据库稳定、高效的基本原则。那么数据库是如何使用内存的&#xff0c;我们如何查看数据库内存的占用&#xff0c;如何通过通过数据库内存配置设置提高其性能&#…

【HihoCoder - 1850】字母去重 (字符串,思维)

题干&#xff1a; 给定一个字符串S&#xff0c;每次操作你可以将其中任意一个字符修改成其他任意字符。 请你计算最少需要多少次操作&#xff0c;才能使得S中不存在两个相邻的相同字符。 Input 只包含小写字母的字符串S。 1 ≤ |S| ≤ 100000 Output 一个整数代表答案…

bash mysql count()_【MySQL】性能优化之 count(*) VS count(col)

优化mysql数据库时&#xff0c;经常有开发询问 count(1)和count(primary_key) VS count(*)的性能有何差异&#xff1f;看似简单的问题&#xff0c;估计会有很多人对此存在认知误区&#xff1a;1. 认为count(1) 和 count(primary_key) 比 count(*) 的性能好。2. count(column) 和…

【HihoCoder - 1851】D级上司 (树形图,dfs)

题干&#xff1a; H公司一共有N名员工&#xff0c;编号为1~N&#xff0c;其中CEO的编号是1。除了CEO之外&#xff0c;每名员工都恰好有唯一的直接上司&#xff1b;N名员工形成了一个树形结构。 我们定义X的1级上司是他的直接上司&#xff0c;2级上司是他上司的上司&#xf…

mysql录数据总是错误_MySQL数据库出错

在主库上备份表 t (假设备份快照 GTID 为 aaaa:1-10000)&#xff1b;恢复到从库&#xff1b;启动复制。这里的问题是复制起始位点是 aaaa:101&#xff0c;从库上表 t 的数据状态是领先其他表的。aaaa:101-10000 这些事务中只要有修改表 t 数据的事务&#xff0c;就会导致复制报…

linux安装mysql后怎么进去_linux安装mysql详细步骤

最近买了个腾讯云服务器&#xff0c;搭建环境。该笔记用于系统上未装过mysql的干净系统第一次安装mysql。自己指定安装目录&#xff0c;指定数据文件目录。linux系统版本&#xff1a; CentOS 7.3 64位安装源文件版本&#xff1a;mysql-5.7.21-linux-glibc2.12-x86_64.tar.gzmys…

给各位ACMer,OIer详细介绍一下Codeforces比赛

Codeforces 简称: cf(所以谈论cf的时候经常被误会成TX的那款游戏). 网址: codeforces.com   这是一个俄国的算法竞赛网站,由来自萨拉托夫州立大学、由Mike Mirzayanov领导的一个团队创立和维护,是一个举办比赛、做题和交流的平台.举办比赛和做题就不说了,“交流”指的是自带b…

【CodeForces - 278C 】Learning Languages(并查集,思维)

题干&#xff1a; The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of language…