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

Portal

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1921    Accepted Submission(s): 955

Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
 
Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
 
Output
Output the answer to each query on a separate line.
 
Sample Input
10 10 10
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6
 
Sample Output
36
13
1
13
36
1
36
2
16
13
题目大意:给一个图,然后会有Q次询问,询问路径上最大权值的边的权值小于 L的路径数。
题目分析:

【1】由于Q的范围是10^4,而如果一个一个查找的话,会TLE。而观察由于每次询问并未确定对路径的起点或者终点,所以每次询问的小L值可以累加得到询问时的大L的值,也就是每次询问的查找是有重叠的,所以就可以将Q次询问的L值进行从小到大的排序,小L慢慢累加得到大L,避免了不必要的重复查询
【解题的第一步-询问存起来等待离线】
【2】由于所连接的边的权值必须不大于L,所以也要将所有边进行排序
【解题第二步-->对边进行排序,等待查询是否能够连接】
【3】而由于是求路径数,两个图【一个顶点数为A,一个为B】连成一个图之后所能增加的路径数是A*B,所以可以利用并查集判断需要连接的两个图是否已经连接,如果没有则进行连接,并且路径数增加A*B,且根节点记录的图的顶点数增加A(或者B)
【解题最关键的一步,从小到大连接边,并更新L[i]的路径数,其中利用了两个连通图连接之后路径数增加A*B的原理】
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct edge{
int to1;
int to2;
int lrn;
}EDGE[];
struct qury{
int id;
int x;
}qwq[];
int n,m,q;
int pre[],num[];;
int find(int x)
{ int xx=x;
while(x!=pre[x])
{
x=pre[x];
}
while(pre[xx]!=x)
{
int t=pre[xx];
pre[xx]=x;
xx=t;
}
return x;
}
bool cmp1(struct edge orz1,struct edge orz2)
{
return orz1.lrn<orz2.lrn;
}
bool cmp2(struct qury orz3,struct qury orz4)
{
return orz3.x<orz4.x;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&q)==)
{
for(int i = ; i <= n ; i++)
{
pre[i]=i;
num[i]=;
}
for(int i = ; i < m ; i++)
{
scanf("%d%d%d",&EDGE[i].to1,&EDGE[i].to2,&EDGE[i].lrn);
}
sort(EDGE,EDGE+m,cmp1);
for(int i = ; i < q ; i++)
{
qwq[i].id=i;
scanf("%d",&qwq[i].x);
}
sort(qwq,qwq+q,cmp2);
int j=;
long long sum=;
long long ans[];
memset(ans,,sizeof(ans));
for(int i = ; i < q ; i++)
{
for(;j<m;j++)
{
if(EDGE[j].lrn>qwq[i].x)break;
int wqw1=find(EDGE[j].to1);
int wqw2=find(EDGE[j].to2);
if(wqw1!=wqw2)
{
pre[wqw1]=wqw2;
sum+=num[wqw1]*num[wqw2];
num[wqw2]+=num[wqw1];
}
}
ans[qwq[i].id]=sum; }
for(int i = ;i < q ; i++)
{
printf("%lld\n",ans[i]);
}
}
return ;
}

【杭电OJ3938】【离线+并查集】的更多相关文章

  1. 杭电--1162--Eddy's picture--并查集

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. [bzoj1015](JSOI2008)星球大战 starwar(离线+并查集)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武 器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...

  3. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  4. HDU5441 Travel 离线并查集

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

  5. [USACO18FEB] Snow Boots G (离线+并查集)

    题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...

  6. BZOJ4551 Tjoi2016&Heoi2016树(离线+并查集)

    似乎是弱化的qtree3.树剖什么的非常无脑.考虑离线.并查集维护每个点的最近打标记祖先,倒序处理,删除标记时将其与父亲合并即可. #include<iostream> #include& ...

  7. Artwork Gym - 101550A 离线并查集

    题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...

  8. 题解报告:zoj 3261 Connections in Galaxy War(离线并查集)

    Description In order to strengthen the defense ability, many stars in galaxy allied together and bui ...

  9. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

随机推荐

  1. linux系统用户和组管理

    用户和组管理 Linux是多用户多任务的网络操作系统,作为网络管理员,掌握用户的组的创建与管理至关重要. 学习要点: 了解用户和组的群的配置文件. 熟悉掌握Linux下用户的创建和维护管理. 熟悉掌握 ...

  2. 红黑树与AVL

     红黑树和avl树都属于自平衡二叉树: 两者查找.插入.删除的时间复杂度相同: 包含n个内部结点的红黑树的高度是o(logn); TreeMap是一个红黑树的实现,能保证插入的值保证排序       ...

  3. console.log()显示图片以及为文字加样式

    有兴趣的同学可以文章最后的代码复制贴到控制台玩玩. Go for Code 在正常模式下,一般只能向console 控制台输出简单的文字信息.但为了把信息输出得更优雅更便于阅读,除了cosole.lo ...

  4. C#通过shell32获取文件详细备注信息

    1.从系统Window/System32文件夹中Copy出 Shell32.dll Com组件 将Shell32.dll文件引用到项目中,并设置“嵌入互操作类型”为false http://blog. ...

  5. 未来Linux系统将是运维行业必备的技能之一

    关于linux,这个并不是每个人都能用或者需要用的,因为平时有很多人用电脑只是为了上上网,聊聊天,打打游戏,这个是完全不需要用linux的.关于linux,是不能用正常的大家所熟知的window来认知 ...

  6. web技术栈中不可或缺的Linux技术

    Web技术最重要的载体便是服务器,服务器运行在公共的网络环境下,为广大的用户提供网页浏览.信息通讯.消息推送等服务,从最开始的硬件服务器到虚拟主机技术,再到虚拟化技术的出现和云概念的兴起,绝大部分都是 ...

  7. hMailServer SSL 配置

    1.先安装 openssl , 调用如下命令,生成证书: openssl genrsa -des3 - openssl req -new -key alics.key -out alics.req o ...

  8. hMailServer 配置

    本例记录如何通过  [hMailServer] 在私有服务器中搭建邮件服务器 1.下载安装包 版本: hMailServer-5.6.7-B2425.exe (支持使用内置数据库) , 安装时,设置管 ...

  9. (Java学习笔记) Java Threading (Java线程)

    Java Threading (Java线程) ● Process & Thread Processes are the abstraction of running programs: A ...

  10. c算法:字符串查找-KMP算法

    /* *用KMP算法实现字符串匹配搜索方法 *该程序实现的功能是搜索本目录下的所有文件的内容是否与给定的 *字符串匹配,如果匹配,则输出文件名:包含该字符串的行 *待搜索的目标串搜索指针移动位数 = ...