http://codeforces.com/problemset/problem/700/B

题意是,在一颗树中,有k个大学,要求两两匹配,他们之间的距离作为贡献,使得距离总和最大。

一开始的时候无从下手,一路在想某一个点应该和哪一个点去匹配。但是这样是错误的思路。

正解是观察边的贡献,如果某两个学校连接了,那么肯定有一条路径的,这条路径会经过很多的边,我们把经过某条边的次数统计出来。那么答案就是这棵树的边的权值和。

那怎么算一条边的贡献呢。

贪心地去想,

某一条边,把整颗树分成了两部分,那么第一部分的学校肯定是连去第二部分的学校,这样的距离比较大。

那么就是有min(partI, k - partI)的贡献。dfs下去就好。脑洞好大。。不是我的题目

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
bool is[maxn];
struct Edge {
int u, v, tonext;
}e[maxn * ];
int num, first[maxn];
void add(int u, int v) {
++num;
e[num].u = u, e[num].v = v, e[num].tonext = first[u];
first[u] = num;
}
LL ans, son[maxn];
int n, k;
void dfs(int cur, int fa) {
son[cur] = is[cur];
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (v == fa) continue;
dfs(v, cur);
ans += min(k - son[v], son[v]);
son[cur] += son[v];
}
}
void work() {
cin >> n >> k;
k <<= ;
for (int i = ; i <= k; ++i) {
int x;
cin >> x;
is[x] = true;
}
for (int i = ; i <= n - ; ++i) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
dfs(, );
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

B. Connecting Universities DFS,无向树的更多相关文章

  1. Codeforces Round #364 (Div. 2) E. Connecting Universities (DFS)

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  2. codeforces 701E E. Connecting Universities(树的重心)

    题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...

  3. Codeforces Round #364 (Div. 2) E. Connecting Universities

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  4. Codeforces 701E Connecting Universities 贪心

    链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...

  5. hdu3452 无向树去掉最小的边集使不论什么叶子与根不连通 / 最小割

    思路一下就上来了,叶子向汇点连边,inf保证不会成为割,跑根到汇点最小割就可以.注意无向树双向建边.基础题,分分钟1A: #include<iostream> #include<qu ...

  6. Connecting Universities

    Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...

  7. BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树

    题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...

  8. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  9. hdu3452 无向树去掉最小的边集使任何叶子与根不连通 / 最小割

    思路一下就上来了,叶子向汇点连边,inf保证不会成为割,跑根到汇点最小割即可.注意无向树双向建边.基础题,分分钟1A: #include<iostream> #include<que ...

随机推荐

  1. hadoop shuffle

    1 hadoop shuffle的地位 hadoop  shuffle是map reduce算法的核心,是它连接了多个map和多个reduce,它将map的输出交给reduce作为输入. 2 hado ...

  2. webservice client setTimeOut

    一:eclipse生成的client,基于axis client_sub.getOptions().setTimeOutInMilliSeconds(1000*60); client_sub表示一个客 ...

  3. LIS n^2&nlogn模板

    LIS nlogn模板 http://acm.hdu.edu.cn/showproblem.php?pid=1950 #include <iostream> #include <st ...

  4. SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

    图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...

  5. HDU3555 Bomb —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    M ...

  6. string interpolation in sql server

    https://sqlserver.dev129.com/2018/01/29/string-interpolation-in-t-sql/ Most programming languages ha ...

  7. virtualbox 复制虚拟机提示uuid is exists

    C:\Program Files\Oracle\VirtualBox>VBoxManage.exe internalcommands sethduuid D:毛毛草\virtual\ubuntu ...

  8. Android Studio四大组件之Service

    Service在Android运行在后台,它没有可视化界面,只是默默运行在后台.我们以一个后台定时器的例子清晰的说明Service的运行流程. 一.创建Service类 项目右键->New-&g ...

  9. Healthy Holsteins

    链接 分析:因为数据范围比较小,我们可以通过二进制枚举子集,然后找出所需饲料种数最小的并记录下来,同时记录一下路径,也就是字典序最小的 /* PROB:holstein ID:wanghan LANG ...

  10. Sharepoint中WebPart開發時註意的問題

    1. 怎麼樣在WebPart中使用Sharepoint控件? 要在webpart中使用sharepoint控件必須先引用Microsoft.SharePoint.WebControls命名空間,如你現 ...