树形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的更多相关文章

  1. CF219D. Choosing Capital for Treeland [树形DP]

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  2. cf219d 基础换根法

    /*树形dp换根法*/ #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int root,n,s,t ...

  3. CF219D Choosing Capital for Treeland

    嘟嘟嘟 树形dp. 首先一个很常规的想法就是如果u到v有一条边,那么建立cost(u, v) = 0, cost(v, u) = 1的两条边. 可以两遍dfs. 先任选一个点作为根节点,第一遍从下往上 ...

随机推荐

  1. idea中的svn配置

    idea的使用之svn篇--有图超详细 http://ylq365.iteye.com/blog/1955291

  2. WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件)

    最近在做一个winform的小软件(抢票的...).登录窗体要从远程web页面获取一些数据,为了不阻塞登录窗体的显示,开了一个线程去加载数据远程的数据,会报一个错误"线程间操作无效: 从不是 ...

  3. sql server 得到数据库字典

    SELECT      表名=case   when   a.colorder=1   then   d.name   else   ''   end,    表说明=case   when   a. ...

  4. asp.net mvc ClaimsIdentity 授权研究 (还是测试版 有bug)

      安装 Microsoft.Owin.Host.SystemWeb Identity.Core Microsoft.Owin.Security.Cookies 在是startup.cs做如下修改 p ...

  5. php:Header

    转自鸟哥的博客: http://www.laruence.com/2007/12/16/308.html PHP header()the function declaration: void head ...

  6. Linux下vim查看文件名

    在vim下编辑时,有时候看不到文件名,不知道编辑的是那个文件,怎么呢,可以按照下面的方法试试. 查看文件名 在正常模式下: :f 或CTRL+G 查看文件的路径 用 :!pwd 可以看当前的详细路径. ...

  7. CF459B Pashmak and Flowers (水

    Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...

  8. Maven初级学习(二)Maven使用入门

    序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...

  9. Cocos2D-X 学习笔记

    1. 3.4final控制台创建项目后,安卓编译会失败,必须手动把cocos/平台/andorid/java/src目录里的文件复制到安卓项目的src文件夹即可 2. 安卓的文件目录与win的略有不同 ...

  10. IGV软件

    它支持各种各样的数据类型,包括基于芯片测序.二代测序数据和基因组注释数据等.整合基因组浏览器(IGV,Integrative Genomics Viewer)进行可视化浏览,它支持各种各式的数据类型, ...