题解:

(并查集处理往上跳的时候,一定要先让u,v往上跳到并查集的祖先,不然会wa掉)

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 1e5 + ;
typedef long long LL;
int f[maxn], g[maxn], p[maxn], deep[maxn];
LL W[maxn];
int ffind(int x) { return f[x] == x ? f[x] : f[x] = ffind(f[x]); }
int gfind(int x) { return g[x] == x ? g[x] : g[x] = gfind(g[x]); }
struct Line{
int u1, v1, u2, v2;
int cost;
bool operator <(const Line& B) const{
return cost < B.cost;
}
};
vector<int> G[maxn];
vector<Line> V; void dfs(int x, int fa, int d){
deep[x] = d;
p[x] = fa;
for(int i = ; i < G[x].size(); i++){
int to = G[x][i];
if(to == fa) continue;
dfs(to, x, d+);
}
} void Merge(int u, int v, LL w){
u = ffind(u); v = ffind(v);
while(ffind(u) != ffind(v)){
if(deep[u] < deep[v]) swap(u, v);
int fa = ffind(u);
u = p[fa];
f[fa] = ffind(u);
u = ffind(u);
if(gfind(fa) != gfind(u)){
W[gfind(u)] += (W[gfind(fa)] + w);
g[gfind(fa)] = gfind(u);
}
}
} int main()
{
int T, n, m, x, y;
cin>>T;
while(T--){
cin>>n>>m;
memset(W, , sizeof(W));
for(int i = ; i <= n; i++) g[i] = f[i] = i;
for(int i = ; i <= n; i++) G[i].clear();
V.clear();
V.resize(m);
for(int i = ; i < n; i++){
scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(, , );
for(int i = ; i < m; i++){
scanf("%d %d %d %d %d", &V[i].u1, &V[i].v1, &V[i].u2, &V[i].v2, &V[i].cost);
}
sort(V.begin(), V.end());
for(int i = ; i < V.size(); i++){
Line line = V[i];
int u = line.u1, v = line.v1, lca1, lca2;
Merge(u, v, line.cost);
lca1 = ffind(u);
u = line.u2, v = line.v2;
Merge(u, v, line.cost);
lca2 = ffind(u);
if(gfind(lca1) != gfind(lca2)) {
W[gfind(lca2)] += (W[gfind(lca1)] + line.cost);
g[gfind(lca1)] = gfind(lca2);
}
}
int num = ;
for(int i = ; i <= n; i++) if(gfind(i) == gfind()) num++;
cout<<num<<" "<<W[gfind()]<<endl;
}
return ;
}

2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)的更多相关文章

  1. 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  3. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. 路由器基础配置之ospf基础配置

    我们将以上面的拓扑图进行本次ospf的实验,目的是能当三台pc机互通 先配置好pc机的IP地址,注意一定要给pc机设置好网关,接下来配置路由器的IP地址 router4 enable 进入特权模式 c ...

  2. Hadoop(15)-MapReduce框架原理-FileInputFormat的实现类

    1. TextInputFormat 2.KeyValueTextInputFormat 3. NLineInputFormat

  3. 开通CSDN博客的原因

                                                                          为什么要写博客? 提供持续学习的动力 例如,我为自己设限每天 ...

  4. docker和docker compose常用操作命令

    首先区分一下docker中几个概念 Image:镜像,相当于一个root文件系统,不包含任何动态数据 Container:容器,镜像运行时的实体,实质是进程,容器进程运行于属于自己的独立的命名空间 d ...

  5. python继续函数-练习(2017-8-3)

    写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 def detection(p): intcount = 0 strcount = 0 othercount = 0 spa ...

  6. ctf题目writeup(9)

    继续刷题,找到一个 什么 蓝鲸安全的ctf平台 地址:http://whalectf.xin/challenges (话说这些ctf平台长得好像) 1. 放到converter试一下: 在用十六进制转 ...

  7. c# string.format和tostring()

    字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0:D3}", ...

  8. Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif

    if-then语句 if-then语句格式如下 if comman then command fi bash shell中的if语句可鞥会和我们接触的其他if语句的工作方式不同,bash shell的 ...

  9. 虚拟现实-VR-UE4-认识UE4

    VR的火热,让每个人都想参与一下, 公司在展会上面搞了一个VR的Demo,关注度超出预期,使得公司高层决定来个VR项目 所以 关于UE4 百度百科地址:http://baike.baidu.com/l ...

  10. Python 3基础教程32-正则

    本文介绍Python的正则,通过本文介绍和一个练习,对正则有一个基本了解就可以. # 正则表达式 ''' 正则表达式是有一些特殊字符组成,能够帮你找到一些符合一定规则的字符串 先来了解几个符号所代表的 ...