[51Nod] 配对
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737
求出树的重心,跑spfa
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string> using namespace std;
const int N = 1e5 + ; #define oo 999999999999
#define yxy getchar() int n, now, Point, Min_siz;
int head[N] ,siz[N], Que[N << ];
struct Node {int v, w, nxt;} G[N << ];
bool vis[N];
long long dis[N]; inline int read(){
int x = ; char c = yxy;
while(c < '' || c > '') c = yxy;
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x;
} inline void add(int u, int v, int w){
G[now].v = v; G[now].w = w; G[now].nxt = head[u]; head[u] = now ++;
} void dfs(int u, int father){
siz[u] = ;
int Max_son = -;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != father){
dfs(v, u);
siz[u] += siz[v];
Max_son = max(Max_son, siz[v]);
}
}
Max_son = max(Max_son, n - siz[u]);
if(Max_son < Min_siz){
Min_siz = Max_son;
Point = u;
}
} void spfa(int S){
for(int i = ; i <= n; i ++) dis[i] = oo;
dis[S] = ;
int H = , T = ;
Que[H] = S;
while(H <= T){
int topp = Que[H ++];
vis[topp] = ;
for(int i = head[topp]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(dis[v] > dis[topp] + G[i].w){
dis[v] = dis[topp] + G[i].w;
if(!vis[v]) vis[v] = , Que[++ T] = v;
}
}
}
} int main()
{
n = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {
int u = read(), v = read(), w = read();
add(u, v, w); add(v, u, w);
}
Min_siz = ;
dfs(, );
spfa(Point);
long long Answer = ;
for(int i = ; i <= n; i ++) Answer += dis[i];
cout << Answer;
return ;
}
[51Nod] 配对的更多相关文章
- 51Nod 2006 飞行员配对(二分图最大匹配)
链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2006 思路: 二分匹配 注意n m的关系 代码: #include ...
- [51nod][cf468D]1558 树中的配对
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1558 不是很懂dalao们用线段树是怎么写的…… 反正找出重心以后每个子 ...
- 51Nod 2006 飞行员配对(二分图最大匹配)
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- 51Nod 1737 配对(树的重心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 题意: 思路: 树的重心. 树的重心就是其所以子树的最大的子树结点 ...
- 51nod 2006 飞行员配对
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...
- 51Nod 1558 树中的配对
题目链接 分析: 想了好久~~~还是得看题解...QwQ 首先因为是排列,所以我们猜想要把式子拆开来看, $ \sum dis(i,p[i])=\sum dep[i]+dep[p[i]]-2*dep[ ...
- 51nod 1737配对
题意:给定一个n个点的带边权树, 保证n是偶数,给这个树两两配对,使得配对后的点路径和最大,输出最大值. 其实是个很简单的题,但还是被绊了.这充分说明现在连简单题都做不来了555 单独考虑每条边.每 ...
- (二分图最大匹配)51NOD 2006 飞行员配对
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
随机推荐
- 【数学】Prime-Factor Prime
Prime-Factor Prime 题目描述 A positive integer is called a "prime-factor prime" when the numbe ...
- scratch少儿编程第一季——05、移动还可以这样动
各位小伙伴大家好: 上期我们学习了怎么控制方向和移动的程序块. 今天我们继续学习运动模块下的其他9个指令(程序块). 首先来看前面两个关于x坐标的程序块. 分别是将x坐标增加()单位,和将x坐标设定为 ...
- Redis过期命令
Redis键的过期时长的设定 ·命令名称:EXPIRE ·语法:EXPIRE key seconds ·功能:为给定key设置生存时间,当key过期时(生存时间为0),它会被自动删除 ·返回值:设置成 ...
- 部署java应用的几种方式
J2EE应用 该应用根目录下有好多文件夹和以jsp结尾的文件 部署时,需要在tomcat的conf目录下的server.xml文件中添加上<Context path="/" ...
- TeamCity安装和配置
目录 TeamCity安装和配置 前言 持续集成(CI) TeamCity 环境 安装 配置TeamCity 项目构建 效果展示 TeamCity自动构建项目及集成IDEA(待更新......) 结束 ...
- 谷歌浏览器chrome安装vue-devtools 插件
1.打开https://github.com/vuejs/vue-devtools直接下载该项目,或者cmd方式直接输入:git Clone https://github.com/vuejs/vue- ...
- dubbo框架梳理
Dubbo分层 Dubbo框架运行主要分如下九层: 配置层:config 服务代理层:proxy 注册中心层:registry 路由层:cluster 监控层:monitor 远程调用层:protoc ...
- django_redis
目录 下载 说明 补充: 内存中设置值 取值 使用 配置: settings/dev.py缓存配置 - redis存储:依赖 django-redis,要安装>>>pip insta ...
- nmap中文帮助文档
简介: Nmap(“ Network Mapper ”)是用于网络探索和安全审核的开源工具.它设计用于快速扫描大型网络,尽管它可以在单个主机上正常运行.Nmap以新颖的方式使用原始IP数据包来确定网络 ...
- 网络设备驱动程序-netdevice结构体关键部分注释
仅仅做个记录,内核4.19 struct net_device { char name[IFNAMSIZ]; //网络设备的名称 struct hlist_node name_hlist; char ...