Avito Cool Challenge 2018:D. Maximum Distance
D. Maximum Distance
题目链接:https://codeforces.com/contest/1081/problem/D
题意:
给出一个连通图以及一些特殊点,现在定义cost(u,v)为一条从u到v的路径上面边权的最大值,然后定义dis(u,v)为从u到v所有路径上面cost的最小值。
最后求所有特殊点到其它特殊点的最大距离...
题解:
这个题意似乎有点绕...
我们考虑一下最小生成树,那么点与点之间的距离就为最小生成树路径上面边权的最大值。
我们来证明一下:假设在最小生成树上面的路径cost为w1,另外在原图中还有一条路径从u到v,其cost为w2,那么必然有w2>w1的。那么我们最后的dis一定是w1。
那么我们现在的目标就是求特殊点到特殊点之间的最大距离。注意一下这里是从一个特殊点到其它所有特殊点的最大距离。
我们知道在Kruskal加边时,后加的边权一定时大于前面的边权的,既然要求最大权值,那么我们可以想加的最后一条边是否可以作为答案。
我们假设现在有两个集合,现在将其连接起来,当满足两个集合里面都有特殊点时我们就可以更新答案了,否则就不行。
所以我们合并的时候顺带维护一下集合里面特殊点的信息就可以了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N =1e5+;
struct Edge{
int u,v,w;
bool operator < (const Edge&A)const{
return w<A.w;
}
}e[N];
int n,m,k;
int a[N],f[N],val[N];
int find(int x){
if(x==f[x]) return f[x];
f[x]=find(f[x]);
return f[x];
}
int main(){
cin>>n>>m>>k;
int tot=k;
for(int i=,t;i<=k;i++){
cin>>t;
a[t]=;
}
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[i]=Edge{u,v,w};
}
for(int i=;i<=n;i++) f[i]=i;
sort(e+,e+m+);
int ans;
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v,w=e[i].w;
int fx=find(u),fy=find(v);
if(fx==fy) continue;
f[fx]=fy;
if(a[u]) val[fx]++;if(a[v]) val[fy]++;
if(val[fx]&&val[fy]) ans=w;
val[fy]+=val[fx];
}
for(int i=;i<=k;i++) cout<<ans<<" ";
return ;
}
Avito Cool Challenge 2018:D. Maximum Distance的更多相关文章
- Avito Cool Challenge 2018:D. Maximum Distance (最小生成树)
题目链接 题意 : 给出一个联通图和一些特殊的点,现在定义cost(u,v)为一条从u到v的路径上面边权的最大值 , 定义dis(u,v) 为从u到v 路径上面cost 的最小值 然后求所有特殊点到其 ...
- Avito Cool Challenge 2018:C. Colorful Bricks
C. Colorful Bricks 题目链接:https://codeforces.com/contest/1081/problem/C 题意: 有n个横向方块,一共有m种颜色,然后有k个方块的颜色 ...
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Avito Cool Challenge 2018(div1+2)
A. Definite Game: 题意:输入N,输出最小的结果N-x,其中x不少N的因子. 思路:N=2时,输出2:其他情况输出1:因为N>2时,N-1不会是N的因子. #include< ...
- Avito Cool Challenge 2018 Solution
A. Definite Game 签. #include <bits/stdc++.h> using namespace std; int main() { int a; while (s ...
- Avito Cool Challenge 2018
考挂了.. A - Definite Game 直接看代码吧. #include<cstdio> #include<cstring> #include<algorithm ...
- Avito Code Challenge 2018
第一次打CF,很菜,A了三道水题,第四题好像是是数位DP,直接放弃了.rateing从初始的1500变成了1499,还是绿名,这就很尴尬.之后觉得后面的题目也没有想象的那么难(看通过人数)过两天吧剩下 ...
- Avito Cool Challenge 2018 自闭记
A:n==2?2:1. #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...
- Avito Cool Challenge 2018 E. Missing Numbers 【枚举】
传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...
随机推荐
- dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
dubbo-2.8.4需用jdk版本为1.8,dubbo-2.5.3可以使用1.7版本的jdk.
- cmake的一个编译报错
在一台新搭建的服务器上执行cmake的时候,报了如下错误: $ cmake ./ -- The C compiler identification is unknown -- The CXX comp ...
- ElasticSearch入门一
ElasticSearch入门一 1 安装ElasticSearch,配置环境变量,并且存在Java环境,而且是Java环境: 下图是安装的目录: 进入bin目录之后,请看bin目录: 启动elast ...
- solr4.8中集成mmseg4j1.9.1
要想在Solr中整合mmseg4j其实很容易,只需要如下几个步骤 1.下载(https://code.google.com/p/mmseg4j/downloads/list)并解压mmseg4j-1. ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)
一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...
- Linux 控制台/终端/tty/shell
一.简介 使用linux已经有一段时间,却一直弄不明白这几个概念之间的区别.这些概念本身有着非常浓厚的历史气息,随着时代的发展,他们的含义也在发生改变,它们有些已经失去了最初的含义,但是它们的名字却被 ...
- scala中的self type
scala目前的书籍有两<快学scala>和<scala编程>.资料确实不多,对这个语法使用只能结合使用进行理解. 先看源码: private[spark] trait Act ...
- SDKD 2017 Summer Single Training #03
今天的题目有 6 个. 第一题: CodeForces - 400D Dima and Bacteria 这个题实际是不难的,难的可能在题意的理解上还有题干有点长,这个题很考察题意上面,知识点很熟悉 ...
- python连接数据库--查询数据
#!/usr/bin/python # -*- coding: utf-8 -*- import pymysql def fileDB(): # 打开数据库连接(ip/数据库用户名/登录密码/数据库名 ...
- [译]Javascript中的函数
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...