2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)
题解:

(并查集处理往上跳的时候,一定要先让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+并查集)的更多相关文章
- 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) ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- jquery把数组中年月相同的数组重新组成新的数组
//原数组var data = { results: [{ id:0, date:'2017-12-12', content:'123' },{ id:0, date:'2017-12-12', co ...
- Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found
今天在发布应用的时候,出来了一个报错:An assembly specified in the application dependencies manifest was not found 情况如下 ...
- Spark知识点
1.Spark架构 分布式spark应用中的组件 在分布式环境下,Spark集群采用的是主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器 ...
- Leecode刷题之旅-C语言/python-69x的平方根
/* * @lc app=leetcode.cn id=69 lang=c * * [69] x 的平方根 * * https://leetcode-cn.com/problems/sqrtx/des ...
- ESP32 LyraT音频开发板试玩(一):搭建开发环境
我是卓波,很高兴你来看我的博客. 系列文章: ESP32 LyraT音频开发板试玩(一):搭建开发环境 ESP32 LyraT音频开发板试玩(二):播放音乐 关于ESP32的开发环境搭建,官方有教程, ...
- mybatis报表,动态列与查询参数+行列转换
这是报表原型,在这张报表中,使用了动态的列与动态查询参数,动态列与动态查询参数全部使用map将参数传入 map参数: //拼接查询时间 for (String month : monthList) { ...
- oracle杀死锁表的进程(转发+合并+自己实践)
之一: Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作 (1)锁表查询的代码有以下的形式:select count(*) from v$locked_object;sel ...
- Koa基本使用
简介 koa 是由 Express 原班人马打造的,致力于成为一个更小.更富有表现力.更健壮的 Web 框架. 使用 koa 编写 web 应用,通过组合不同的 generator,可以免除重复繁琐的 ...
- What to do when Enterprise Manager is not able to connect to the database instance (ORA-28001)
摘自:http://dbtricks.com/?p=34 If you are trying to connect to the Oracle enterprise Manger and you ge ...
- 对工具的反思 & deadlines与致歉
人和动物最大的区别就是使用工具的水平. 有些人只凭着对工具的熟练掌握便成了牛人. 工具,到底应该以何种态度去看待? 在我小的时候,工具仅仅是指树枝.线.粉笔,可以让自己有更多游戏可玩:上学之后,便又有 ...