hdu 3938 并查集
思路:这题的出题人是不是语文不行啊,题目意思说不清楚。
知道是求存在路径的点对数后,用并查集每次记录集合中点的数目,很容易就解决了。
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 200100
#define Maxm 800002
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 1000000000
#define lowbit(x) (x&(-x))
#define Mod 1000000007
using namespace std;
int fa[Maxn],cnt;
LL num[Maxn],ans[Maxn];
struct Edge{
int u,v,val;
int operator< (const Edge &temp) const
{
return val<temp.val;
}
}p[Maxm];
struct Query{
int val,id;
int operator< (const Query &temp) const{
return val<temp.val;
}
}q[Maxn];
void init()
{
for(int i=;i<Maxn;i++){
fa[i]=i;
ans[i]=;
num[i]=;
}
}
int find(int x)
{
if(x!=fa[x])
fa[x]=find(fa[x]);
return fa[x];
}
LL merg(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y) return ;
LL ans=num[x]*num[y];
num[x]+=num[y];
fa[y]=x;
++cnt;
return ans;
}
int main()
{
int n,m,t,i,j,u,v;
while(scanf("%d%d%d",&n,&m,&t)!=EOF){
init();
for(i=;i<=m;i++){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].val);
}
for(i=;i<=t;i++)
scanf("%d",&q[i].val),q[i].id=i;
sort(p+,p++m);
sort(q+,q++t);
p[m+].val=inf;
p[m+].u=,p[m+].v=;
j=;
LL sum=;
cnt=;
int pos=;
for(i=;i<=m+;i++){
while(j<=t&&q[j].val<p[i].val){
ans[q[j].id]=sum;
j++;
}
sum+=merg(p[i].u,p[i].v);
}
while(j<=t&&q[j].val<p[i].val)
{
ans[q[j].id]=sum;
j++;
}
for(i=;i<=t;i++){
printf("%I64d\n",ans[i]);
}
}
return ;
}
hdu 3938 并查集的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
随机推荐
- asp获取勾选checkbox的值
Dim str_select str_select = CStr(request.Form("c_name")) c_name是checkbox的name
- jquery 禁止页面滚动-移动端
禁止 window.ontouchmove=function(e){ e.preventDefault && e.preventDefault(); e.r ...
- CI reids 缓存
注意:在项目中的application/libraries 中自己定义的类最好不要以cache命名. 连接 Redis 服务器的配置信息必须保存到 application/config/redis.p ...
- Ember学习(8):REOPENING CLASSES AND INSTANCES
英文原址:http://emberjs.com/guides/object-model/reopening-classes-and-instances/ 你不须要一次就完毕类所有内容的定义,通过reo ...
- EasyUI-Tab 标签添加右键菜单
在网上看了很多demo 自己实现了一个效果如下 <!doctype html> <html> <head> <meta http-equiv="co ...
- poj 1941 The Sierpinski Fractal 递归
//poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...
- 从头学Android系列
从头学Android系列 http://blog.csdn.net/worker90/article/category/888358
- sql server常用知识点
--删除表 use [20130823_Recource] go drop table my_table1,my_table2,My_table3 --创建表 use [20130823_Recour ...
- Perl 引用与匿名数组
写这篇是因为工作遇到一个需要使用列表作为hash的值的问题,这在Python中是非常简单而轻松的事,如下面这段python程序. def add_to_index(index, keyword, ur ...
- 关于ASP.NET中Button的OnClientClick属性
Button有Click属性和OnClientClick属性,执行顺序上OnClientClick先执行,调用本地脚本,根据返回值确定是否执行Click. 当返回True则执行Click,当脚本错误或 ...