cf219d
树形dp
#include <cstdio>
#include <vector>
using namespace std; #define D(x) const int INF = 0x3f3f3f3f;
const int MAX_N = (int)(2e5) + ; int n;
vector<pair<int, int> > edge[MAX_N];
int re_num[MAX_N];
vector<int> ans_vec; int dfs(int father, int u, int add, int re_add)
{
D(printf("u=%d\n", u));
int ret = ;
for (int i = ; i < (int)edge[u].size(); i++)
{
int v = edge[u][i].first;
int w = edge[u][i].second;
if (v == father)
{
continue;
}
D(printf("u=%d, v=%d\n", u, v));
ret += dfs(u, v, add + ( - w), re_add + w) + w;
}
re_num[u] = add - re_add;
D(printf("%d %d\n", u, ret));
return ret;
} int main()
{
scanf("%d", &n);
for (int i = ; i < n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
edge[a].push_back(make_pair(b, ));
edge[b].push_back(make_pair(a, ));
D(printf("%d %d\n", a, b));
}
fill_n(re_num, n + , );
int ans = dfs(-, , , );
ans_vec.push_back();
int root_ans = ans;
D(printf("%d\n", root_ans));
for (int i = ; i <= n; i++)
{
int temp = re_num[i] + root_ans;
if (temp == ans)
{
ans_vec.push_back(i);
}
if (temp < ans)
{
ans = temp;
ans_vec.clear();
ans_vec.push_back(i);
}
}
printf("%d\n", ans);
for (int i = ; i < (int)ans_vec.size(); i++)
{
printf("%d", ans_vec[i]);
if (i != (int)ans_vec.size() - )
putchar(' ');
}
puts("");
return ;
}
cf219d的更多相关文章
- CF219D. Choosing Capital for Treeland [树形DP]
D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...
- cf219d 基础换根法
/*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...
- CF219D Choosing Capital for Treeland
嘟嘟嘟 树形dp. 首先一个很常规的想法就是如果u到v有一条边,那么建立cost(u, v) = 0, cost(v, u) = 1的两条边. 可以两遍dfs. 先任选一个点作为根节点,第一遍从下往上 ...
随机推荐
- Orchard源码分析(5):Host相关(Orchard.Environment.DefaultOrchardHost类)
概述 Host 是应用程序域级的单例,代表了Orchard应用程序.其处理应用程序生命周期中的初始化.BeginRequest事件.EndRequest事件等. 可以简单理解为HttpApplicat ...
- PHP 学习
http://www.w3school.com.cn/php/php_sessions.asp public static void chkacc() {Response.Redirect(" ...
- JavaScript 学习笔记 -- Function
JS 中 函数.继承.闭包.作用域链... 一直都是硬伤,一碰到这样的问题头就大了.但是如果我继续着说:我不会,就真的无药可救了.要勇敢地说出:我的字典里就没有不会这个词,吼吼..正好昨天在书城里看了 ...
- 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
- mongodb windows install &python
安装mongo http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ 其中修改: echo logpath=C:\mo ...
- C语言的源程序字符集与执行字符集
我们程序文件的字符集就是我们写出来的.c扩展名的文件的字符集,这里用的是系统默认的 ANSI 字符集,如下图: 上面的字符集我们不关心,我们关心的是 源程序的字符集 和程序的 执行字符集 ,源程序的字 ...
- EF接触02
Ado.net Entity Framework早期称为ObjectSpace.基于Ado.net操作数据库的一组类库. 什么是ADO.NET? 基础.net平台下的操作数据库的一组Api或组建.五大 ...
- jQuery插件之simplemodal
1.simplemodal在内部定义了如下css类 simplemodal-overlay:遮罩 simplemodal-container:弹出窗口容器 simplemodal-wrap simpl ...
- CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...
- Sqli-LABS通关笔录-7[文件写入函数Outfile]
该关卡最主要的就是想要我们学习到Outfile函数(文件写入函数)的使用. 通过源代码我们很容易的写出了payload.倘若我们一个个去尝试的话,说实话,不容易. http://127.0.0.1/s ...