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 ...
随机推荐
- js 判断两个时间相差的天数
judgeDay(sDate1, sDate2) { const sDate1 = `${new Date(sDate1).getFullYear()}-${new Date(sDate1).getM ...
- Python接受流式输入
随笔记录——Python接受终端入若干行输入 Python接受终端的若干行输入时,比较常用的input()不再好用. 1. 导入sys模块: import sys 2. for循环接受输入: for ...
- Cython中def,cdef,cpdef的区别
这是我的第一篇翻译,希望大家多多给出意见和建议. 如有转载,请注明出处. 原文来自:https://stackoverflow.com/questions/28362009/definition-of ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- pads怎么高亮网络
pads怎么高亮网络 选择完整个网络----再按CTRL+H 就高亮了. 取消高亮是,选择需要取消高亮的整个网络,按 CTRL+U 就取消了. PADS在生成Gerber时过孔盖油设置方法 PADS2 ...
- 09 mongoDB基础(进阶)
mongoDB基础 阶段一.认识mongodb 1.mongodb 组织数据的基本形式 MongoDB————>数据库————>集合————>文档 mysql:表:行和列:字段 运用 ...
- django之单表查询
一.创建表 1.创建模型: 创建名为book的app,在book下的models.py中创建模型: from django.db import models # Create your models ...
- 007---logging日志模块
logging模块 用途:服务器运行日志.运维日志... import logging from logging.handlers import RotatingFileHandler, TimedR ...
- Spring配置文件一直报错的根源所在
跳坑后的感悟总结 Spring在配置文件中经常会报XML错误,以下是几种常见的解决办法 方式一:打开eclipse-->Project-->Clean ;清除一下 方式二:查看xml配置文 ...
- Java重写构造方法
public class TestSuper { public static void main(String[] args) { new ChildClass("alex", 1 ...