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点走到距 ...
随机推荐
- SQL函数集合
1. PATINDEX ( '%pattern%' , expression ) 返回pattern字符串在表达式expression里第一次出现的位置,起始值从1开始算. pattern字符串在ex ...
- Oracle和plsql developer编码设置
在使用pl/sql developer时,查询出来中文字段显示乱码,因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 一.查看和修改oracle数据库字符集 selec ...
- css初始化代码方案
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-06-24) 为了消除各浏览器对css默认的设置,保持网页在各浏览器中的外观保持一致,初始化css就显得非常必要了!很多时候 ...
- CANBus Determining Network Baud Rate, Automatic bit-rate detection
http://www.canbushack.com/blog/index.php?title=determining-network-baud-rate Determining Network Bau ...
- HttpResponse类
HttpReponse是服务器接收到浏览器的请求后,处理返回结果常用的一个类. 一.属性 Buffer 获取或设置一个值,该值指示是否缓冲输出并在处理完整个响应之后发送它. BufferOutput ...
- HDU 5590 ZYB's Biology 水题
ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- .Net 垃圾回收和大对象处理
CLR垃圾回收器根据所占空间大小划分对象.大对象和小对象的处理方式有很大区别.比如内存碎片整理 —— 在内存中移动大对象的成本是昂贵的,让我们研究一下垃圾回收器是如何处理大对象的,大对象对程序性能有哪 ...
- ubuntu全盘备份与恢复
备份: 以下是我用来备份系统的完整命令: tar -jpcvf Ubuntu-12.04-20131018.tar.bz2 --exclude=/proc --exclude=/lost+found ...
- 日志记录到txt文件
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;usi ...
- defer和async的区别
先来试个一句话解释仨,当浏览器碰到 script 脚本的时候: <script src="script.js"></script> 没有 defer 或 a ...