思路:这题的出题人是不是语文不行啊,题目意思说不清楚。

知道是求存在路径的点对数后,用并查集每次记录集合中点的数目,很容易就解决了。

#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 并查集的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. “非常PHP学习网”(www.veryphp.cn)一期上线

    制作“非常PHP学习网”花了国庆整个假期,其实是从电脑学习网(http://www.why100000.com,域名刚续费)改写盗版而来的. 起初主要修改界面布局和颜色花费了大量时间(好像制作网站80 ...

  2. Oracle中TO_DATE格式

    转自:http://www.cnblogs.com/ajian/archive/2009/03/25/1421063.html TO_DATE格式(以时间:2007-11-02   13:45:25为 ...

  3. Window.navigator

    定义和用法 userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. 一般来讲,它是在 navigator.appCodeName 的值之后加上斜线和 navig ...

  4. Android LIstView初次创建getview方法执行多次问题

    写listview优化的时候,发现Listview初次创建的时候会多次执行getView方法. <?xml version="1.0" encoding="utf- ...

  5. Eclipse调试Java的10个技巧

    原文地址: http://www.oschina.net/question/82993_69439 在看这篇文章前,我推荐你看一下Eclipse 快捷键手册,我的eclipse版本是4.2 Juno. ...

  6. TMsgThread, TCommThread -- 在delphi线程中实现消息循环

    http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...

  7. 让<a></a>diabled 的方法

    <a href="test_att_data.php" disabled="true" onclick="return false"& ...

  8. Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心

    A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...

  9. Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力

    C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  10. 学渣告诉你,到底神马是傅里叶级数!转自 新浪@工程师style