HDU 3938 Portal【並查集+upper_bound(第四個引數)】
Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1459 Accepted Submission(s): 745
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
AC程式碼:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int MAXN=1e4+11;
int fa[MAXN];
struct Edge{
int u,v,w;
}e[50011];
int ans[50011],s[50011];
bool cmp(Edge a,Edge b) {
return a.w<b.w;
}
int Find(int x) {
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
int main()
{
int N,M,Q;
while(~scanf("%d%d%d",&N,&M,&Q)) {
for(int i=1;i<=M;++i) {
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
}
sort(e+1,e+M+1,cmp);
for(int i=1;i<=N;++i) {
fa[i]=i; s[i]=1;
}
ans[0]=0;
for(int i=1;i<=M;++i) {
int x=Find(e[i].u);
int y=Find(e[i].v);
ans[i]=ans[i-1];
if(x!=y) {
ans[i]+=s[x]*s[y];
fa[x]=y; s[y]+=s[x];
}
}
while(Q--) {
// puts("");
Edge q; scanf("%d",&q.w);
int loc=upper_bound(e+1,e+M+1,q,cmp)-e;
if(e[loc].w==q.w) printf("%d\n",ans[loc]);
else printf("%d\n",ans[loc-1]);
}
}
return 0;
}
相關文章
- 並查集的應用:hdu 1213並查集
- HDU 1213 How Many Tables(並查集)並查集
- HDU-3172 Virtual Friends 並查集+map並查集
- hdu5222 拓撲+並查集並查集
- hdu 1811 並查集+拓撲排序並查集排序
- HDU-1272 小希的迷宮 並查集並查集
- HDU 1198Farm Irrigation(並查集)並查集
- hdu 1829 並查集(食物鏈的弱化版)並查集
- HDU 5200 Tree (離線並查集)並查集
- HDU 3234 Exclusive-OR 擴充套件並查集套件並查集
- hdu4313 貪心並查集 || 樹形dp並查集
- HDU 1272小希的迷宮(簡單並查集)並查集
- HDU 3038 How Many Answers Are Wrong (帶權並查集)並查集
- HDU-3635 Dragon Balls 並查集路徑壓縮Go並查集路徑壓縮
- HDU-3461 Code Lock 並查集 + 二分求冪並查集
- 並查集到帶權並查集並查集
- 【並查集】【帶偏移的並查集】食物鏈並查集
- 2013成都站F題||hdu4786 並查集 生成樹並查集
- 2012長春站D題||hdu4424 並查集並查集
- 並查集(一)並查集的幾種實現並查集
- 3.1並查集並查集
- 並查集(小白)並查集
- PostgreSQL並行查詢相關配置引數SQL並行
- 並行相關的幾個引數並行
- 給一個介面傳遞引數,並接收返回的引數
- HDU5441 Travel (2015年長春網路賽,並查集)並查集
- 轉載:一個python並查集類Python並查集
- 並查集(Union Find)並查集
- 並查集應用並查集
- The Door Problem 並查集並查集
- 並查集練習並查集
- 並查集的使用並查集
- 並查集—應用並查集
- 寫模板, 並查集。並查集
- 並查集跳躍並查集
- 各種並查集並查集
- 食物鏈(並查集)並查集
- 並查集(二)並查集的演算法應用案例上並查集演算法