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点走到距 ...
随机推荐
- Castle IOC容器内幕故事(下)
主要内容 1.ComponentModelBuilder 和 Contributors 2.Contributors分析 3.Handles分析 4.ComponentActivator分析 一.Co ...
- Javascript模块规范(CommonJS规范&&AMD规范)
Javascript模块化编程(AMD&CommonJS) 前端模块化开发的价值:https://github.com/seajs/seajs/issues/547 模块的写法 查看 AMD规 ...
- php 内置http服务器
PHP从5.4.0起,内置了一个http服务器,开发人员可以借助这个内置服务器来做一些本地测试. 启动服务器: 打开终端,进入php安装目录,然后执行 php -S localhost: 这样就可以开 ...
- How a non-windowed component can receive messages from Windows -- AllocateHWnd
http://www.delphidabbler.com/articles?article=1 Why do it? Sometimes we need a non-windowed componen ...
- 【M28】智能指针
1.什么是智能指针? 所谓智能指针就是,看起来,用起来,感觉起来都像原始指针,但是提供了更多功能. 2.使用智能指针取代原始指针,可以获得更多的控制权.如下: a.在构造和析构的时候,可以做一些事. ...
- 因为改 UOM conversion 导致库存数量和財务上的数据错误
轻易改变 UOM conversion 会导致库存数量混乱, 也会造成財务上的数据错误. 我们这里做一个 case 来详细分析一下. 1. 開始 Carton 和 Each 的比例是 1 : 1. 2 ...
- 安卓高手之路之PackageManagerservice
源码位置:frameworks/base/core/java/android/content/pm/PackageParser.java 源文件路径:android\frameworks\base\s ...
- [Angular 2] Move and Delete Angular 2 Components After Creation
After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to re ...
- 基于html5实现的愤怒的小鸟网页游戏
之前给大家分享一款基于html5 canvas和js实现的水果忍者网页版,今天给大家分享一款基于html5实现的愤怒的小鸟网页游戏.这款游戏适用浏览器:360.FireFox.Chrome.Safar ...
- initrd映像文档的作用和制作
1.http://pan.baidu.com/s/1dDrGeKL 2.http://wenku.baidu.com/link?url=qPa_jfkEZCbERnwMYWLwm9EZJ_ebMRJA ...