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. 先任选一个点作为根节点,第一遍从下往上 ...
随机推荐
- C#反射机制(转)
一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...
- R--基本统计分析方法(包及函数)
摘要:目前经典的统计学分析方法主要有回归分析,Logistic回归,决策树,支持向量机,聚类分析,关联分析,主成分分析,对应分析,因子分析等,那么对于这些经典的分析方法在R中的使用主要有那些程序包及函 ...
- Java实验1-文件IO
目标:掌握Java类的创建,Java I/O操作,Java集合类的使用等 内容: 王老师非常喜欢读书,为了便于查阅,他每次买书回家后就在笔记本上登记每本书的详细信息(书名.作者.出版社.出版日期.价 ...
- gtest
一.安装配置 1.简介 2.安装 下载地址: https://code.google.com/p/googletest/downloads/list 解压安装: unzip gtest-1.7.0.z ...
- 一起找bug
帮同学找的一个bug,错误代码如下: package dai_test; public class Test1 { public static void main(String[] args) { / ...
- ASP.NET MVC 4 的JS/CSS打包压缩功能-------过滤文件
今天在使用MVC4打包压缩功能@Scripts.Render("~/bundles/jquery") 的时候产生了一些疑惑,问什么在App_Start文件夹下BundleConfi ...
- angular $resource模块
目录(?)[-] 安装 应用resource 扩展resource 上一篇中讲到使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource, ...
- CSU 1328: 近似回文词
省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 ...
- QT点击"X"按钮,调用closeEvent()函数来实现调用特定事件(附:粗略介绍QT的信号与槽的使用方法)
背景: QT在用户关闭窗口(直接点击"X"键)时,程序一般都需要做一些善后的事情,就我现在的程序来说,既关闭USB.如何实现? 正文: 首先,在对应窗体的".h" ...
- 【PHP面向对象(OOP)编程入门教程】21.多态的应用
多态是除封装和继承之外的另一个面象对象的三大特性之一,我个人看来PHP中虽然可以实现多态,但和c++还有Java这些面向对象的语言相比,多 态性并不是那么突出,因为PHP本身就是一种弱类型的语言,不存 ...