http://acm.hdu.edu.cn/showproblem.php?pid=5441

无向图,n个点,m条带权边,q次询问,给出数值d,点a可以到点b当且仅当存在一条路线其中最大的权值不超过d。

用sum记录连通块节点个数,两个连通块合并,结果增加( sum[a] + sum[b] ) * ( sum[a] + sum[b] - 1 ) - sum[a] * ( sum[a] - 1 ) - sum[b] * ( sum[b] -1 )

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct node
{
int a;
int b;
int c;
};
int father[N];
node p[N];
int num[N];
ll sum[N];//表示该点所在连通块的节点个数
int r[N];
ll ans[N];
int finds(int x)
{
if(father[x]!=x)
father[x]=finds(father[x]);
return father[x];
}
bool cmp(node x,node y)
{
return x.c<y.c;
}
void connect(int a,int b)
{
if(r[a]>r[b])
father[b]=a;
else if(r[a]<r[b])
father[a]=b;
else
{
father[a]=b;
r[b]++;
}
sum[a]=sum[b]=sum[a]+sum[b];
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)
{
sum[i]=1;
father[i]=i;
scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c);
}
sort(p+1,p+m+1,cmp);//首先按权值进行排序
for(int i=1;i<=m;i++)
{
int a=finds(p[i].a);
int b=finds(p[i].b);
if(a!=b)
{
ans[i]=ans[i-1]+(sum[a]+sum[b])*(sum[a]+sum[b]-1)-sum[a]*(sum[a]-1)-sum[b]*(sum[b]-1);//连通块合并结果增加
connect(a,b);
}
else
ans[i]=ans[i-1];
num[i]=p[i].c;
}
while(q--)
{
int x;
scanf("%d",&x);
int t=upper_bound(num+1,num+m+1,x)-num-1;
printf("%lld\n",ans[t]);
}
}
return 0;
}

HDU5441 Travel 并查集的更多相关文章

  1. Travel 并查集

    题意:给一个图,若干询问,每次询问只经过边权<=w的边,x能到达的点数 并查集啊,对询问和边排序,直接合并,维护size,查询 #include<cstdio> #include&l ...

  2. bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  3. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  4. HDU5441 Travel 离线并查集

    Travel Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, ...

  5. 【BZOJ-1576】安全路径Travel Dijkstra + 并查集

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1044  Solved: 363[Sub ...

  6. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  7. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  8. Travel(HDU 5441 2015长春区域赛 带权并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  9. 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集

    [BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...

随机推荐

  1. 在汇编代码中调用C函数

    对于ARM体系来说,不同语言撰写的函数之间相互调用(mix calls)遵循的是 ATPCS(ARM-Thumb Procedure Call Standard),ATPCS主要是定义了函数呼叫时参数 ...

  2. iOS视频直播初窥:高仿<喵播APP>

    视频直播初窥 视频直播,可以分为 采集,前处理,编码,传输, 服务器处理,解码,渲染 采集: iOS系统因为软硬件种类不多, 硬件适配性比较好, 所以比较简单. 而Android端市面上机型众多, 要 ...

  3. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  4. 直接取HANA数据库数据,动态QUERY

    "COPY别人的TYPE-POOLS:ADBC. DATA LV_SQL TYPE STRING. DATA LV_FROM TYPE STRING. DATA LV_WHERE TYPE ...

  5. 如何删除href=""中的链接?

    答案:在dw中操作,删除 HTML文件的href的链接地址\href="[^"]*"href="" 同理可以在title="[^" ...

  6. 用js创建XMLHttpRequest对象池[转]

    //使用literal语法定义一个对象:XMLHttp var XMLHttp = { //定义第一个属性,该属性用于缓存XMLHttpRequest对象的数组 XMLHttpRequestPool: ...

  7. if语句使用

    package yuan; public class Yuan { public static void main(String[] args) { int a = 1; int b = 8; int ...

  8. ROS程序编辑器

    我找到的比较好用的ROS代码编辑器,对于emacs和vim等神级编辑器不能自动补全,对于我这种新手编译出错都是字母打错了, 因此果断回避,找到了一款叫做code blocks的编辑器,在软件中心就能下 ...

  9. CSS引入外部字体

    @font-face {    font-family: '综艺体';    font-style: normal;    font-weight: normal;    src: url(../cs ...

  10. Unity游戏数据用Json保存

    (一)关于路径 unity有几个关键的路径 (1).Application.dataPath 只读路径,就是工作目录的Assets路径 (2).Application.streamingAssetsP ...