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点走到距 ...
随机推荐
- 【转】google推出的SwipeRefreshLayout下拉刷新用法
SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便. 使用如下: 1.先下载android-support ...
- 国内外从事CV相关的企业[转]
提示:本文为笔者原创,转载请注明出处:blog.csdn.net/carson2005 经常碰到朋友问我国内从事计算机视觉(CV)领域的公司的发展情况,产品情况,甚至找工作等问题,这里,我给出自己收集 ...
- CXF 与Spring整合配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...
- C# 操作数据库就的那点代码
操作数据库的那点代码,别在费劲每个数据库都写一遍SQLHelper,SQLiteHelper,OleDbHelper,了,这里都有了. 接口不发了,自己抽取定义就行了. public abstract ...
- Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心
D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...
- VMWare虚拟机网络的三种工作模式
VMWare提供了三种工作模式: 1.bridged(桥接模式) 在这种模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访问网内任何一台机器.在桥接模式下,需要手工为虚拟系 ...
- 排版系统Latex傻瓜方式使用(论文排版)
0. 什么是Latex? LaTEX(英语发音:/ˈleɪtɛk/ lay-tek或英语发音:/ˈlɑːtɛk/ lah-tek,音译"拉泰赫").文字形式写作LaTeX.是一种基 ...
- 在android market发布个人免费应用的步骤
写了一段时间的android应用了,只是在自己手机上面安装. 上周申请了android developer,需要一次性25美元的程序开发注册费用.费用需要用google checkout,所以还要先申 ...
- 【JavaScript】Javascript中的函数声明和函数表达式
Javascript有很多有趣的用法,在Google Code Search里能找到不少,举一个例子: <script> ~function() { alert("hello, ...
- 【JavaScript】【译】编写高性能JavaScript
英文链接:Writing Fast, Memory-Efficient JavaScript 很多JavaScript引擎,如Google的V8引擎(被Chrome和Node所用),是专门为需要快速执 ...