Portal

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

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
 
Source
 
题目意思:
            给定一张无向图,要你求出满足小于或等于给定边长的最多边长的种类数目。当然关键是有n次询问。
     思路:
               对于一颗有n条路径的无向图,可以知道,当与另一个m条路径的无向图合并为一个全新的无向图时: 此时的路径为 n*m;
               当然这题的重点不在这里,此题关键是有q次询问,对于每一次询问,若我们都去重新求解一次,将会很花时间,无疑TLE倒哭。%>_<%
               于是采用离线处理(这里是开了解题报告才想起来,这么巧妙的地方,果然是too yong 哇! ):
      离线的思路为:   先将询问的权值从小到大排序,由于大的权值包含了小的权值,于是整个过程,居然只需要一次就搞定。  俺是乡下人,第一次见识到这点,吓尿了!╮(╯▽╰)╭
  代码:

 #define LOCAL
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int maxn=;
/*for point*/
struct node{
int a,b,c;
bool operator <(const node &bn)const {
return c<bn.c;
}
}sac[maxn*];
/*for query*/
struct query{
int id,val;
bool operator <(const query &bn)const {
return val<bn.val;
}
}qq[maxn]; int father[maxn],rank[maxn];
int key[maxn]; void init(int n){
for(int i=;i<=n;i++){
father[i]=i;
rank[i]=;
}
} int fin(int x){
while(x!=father[x])
x=father[x];
return x;
} int unin(int x,int y)
{
x=fin(x);
y=fin(y);
int ans=;
if(x!=y){
ans=rank[x]*rank[y];
if(rank[x]<rank[y]){
rank[y]+=rank[x];
father[x]=y;
}
else{
rank[x]+=rank[y];
father[y]=x;
}
}
return ans;
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
freopen("test1.out","w",stdout);
#endif
int n,m,q;
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
init(n);
for(int i=;i<m;i++){
scanf("%d%d%d",&sac[i].a,&sac[i].b,&sac[i].c);
} for(int i=;i<q;i++){
scanf("%d",&qq[i].val);
qq[i].id=i;
}
sort(sac,sac+m);
sort(qq,qq+q);
int i,j,res=;
for(j=,i=;i<q;i++){
while(j<m&&sac[j].c<=qq[i].val){
res+=unin(sac[j].a,sac[j].b);
++j;
}
key[qq[i].id]=res;
}
for(i=;i<q;i++){
printf("%d\n",key[i]);
}
}
// system("comp"); return ;
}
 

hdu 3948 Portal (kusral+离线)的更多相关文章

  1. HDU 3938 Portal (离线并查集,此题思路很强!!!,得到所谓的距离很巧妙)

    Portal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  2. hdu 3948 后缀数组

    The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  3. hdu Portal(离线,并查集)

    题意:在一张无向图上,已知边权,做q组询问,问小于L的点对共有几组.点对间的距离取=min(两点之间每一条通路上的最大值). 分析:这里取最大值的最小值,常用到二分.而这里利用离线算法,先对边从小到大 ...

  4. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...

  5. hdu 3938 Portal (prim+离线)

    Problem - 3938 题意是要求出给定权值下,满足要求的点对的数目.所谓的要求是,给出两点,之间会有很多路径,这个点对的最小距离是众多路径中,最短的一条路径的长度,路径长度是路径上最长边的长度 ...

  6. HDU - 3948 后缀数组+Manacher

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...

  7. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  8. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  9. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

随机推荐

  1. 设置三思LED的IP地址跟端口号

    出厂设置是:202.11.11.01 初始端口号是:2929 设置虚拟机的ip跟LED的ip在一个网段上,在虚拟机上telnet命令,登陆到LED上面. 在/etc/init.d/rcS文件中, #! ...

  2. Object-C : Block的实现方式

    摘自:http://www.cnblogs.com/GarveyCalvin/p/4204167.html> Date : 2015-12-4 前言:我们可以把Block当作一个闭包函数,它可以 ...

  3. 深入理解 C# 协变和逆变

    msdn 解释如下: “协变”是指能够使用与原始指定的派生类型相比,派生程度更大的类型. “逆变”则是指能够使用派生程度更小的类型. 解释的很正确,大致就是这样,不过不够直白. 直白的理解: “协变” ...

  4. week7团队项目体会

    经过这几个星期以来的软件工程的学习,还有自己在个人项目.结对编程.团队项目的感受,总结了一些感受和理解. 总结收获 首先,一个月的软件工程团队项目的进行让我对软件开发有了比较实际的认识,之前的概念仅限 ...

  5. ubuntu的dns设置

    ubuntu的dns设置为: dns-nameservers 8.8.8.8 注意不要少s

  6. 【Todo】秒杀系统材料

    秒杀系统:Link <一个经验证可落地的秒杀系统实践思路> 主要依赖于Redis进行处理. http://geek.csdn.net/news/detail/59847   淘宝大秒系统设 ...

  7. C 语言预处理

    在嵌入式系统编程中,不管是内核的驱动程序还是应用程序的编写,都涉及到大量的预处理与条件编译,这样做的好处主要体现在代码的移植性强以及代码的修改方便等特性,因此引入了预处理与条件编译的概念.在C语言的程 ...

  8. android 主线程和子线程之间的消息传递

    从主线程发送消息到子线程(准确地说应该是非UI线程)  package com.zhuozhuo; import android.app.Activity; import android.os.Bun ...

  9. iOS开发之 在release版本禁止输出NSLog内容

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...

  10. android坐标

    说来说去都不如 画图示意 简单易懂啊!!!真是的! 来吧~~先上张图~~! (一)首先明确一下 android 中的坐标系统 :      屏幕的左上角是坐标系统原点(0,0)      原点向右延伸 ...