【POJ2832 】【Tyvj1460】【 7.22Test 】询问

几种版本的题面

Description

You are given an undirected graph G with N vertices and M edges. Each edge has a length. Below are two definitions.

  1. Define max_len(p) as the length of the edge with the maximum length of p where p is an arbitrary non-empty path in G.
  2. Define min_pair(uv) as min{max_len(p) | p is a path connecting the vertices u and v.}. If there is no paths connecting u and vmin_pair(uv) is defined as infinity.

Your task is to count the number of (unordered) pairs of vertices u and v satisfying the condition that min_pair(uv) is not greater than a given integer A.

Input

The first line of input contains three integer NM and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of vertices, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers ab, and c (1 ≤ ab ≤ N, 0 ≤ c < 108) describing an edge connecting the vertices a and b with length c. Each of the following Q lines gives a query consisting of a single integer A (0 ≤ A < 108).

Output

Output the answer to each query on a separate line.

Sample Input

4 5 4
1 2 1
2 3 2
2 3 5
3 4 3
4 1 4
0
1
3
2

Sample Output

0
1
6
3

Source

描述

A国有n座城市,每座城市都十分美,这使得A国的民众们非常喜欢旅行。然而,A国的交通十分落后,这里只有m条双向的道路,并且这些道路都十分崎岖,有的甚至还是山路,只能靠步行。通过每条道路的长度、泥泞程度等因素,我们给每条道路评估一个“崎岖度”,表示通过这条道路的不舒适程度。
从X城市经过若干条道路到达Y城市,我们称这次旅行的“代价”为所经过道路“崎岖度”的最大值。当然,如果从X城市到Y城市有多条路线,民众们会自觉选择“代价”最小的路线进行旅行。但是,A国的民众也是有脾气的,如果旅行的“代价”超过了他们的“忍耐度”,他们就不选择这个旅行了,甚至宁愿在家里宅着。
现在A国的国王想进行若干次询问:给定民众的“忍耐度”,问还有多少对城市(X,Y)会存在旅行?请你对国王的每次询问分别给出回答。

输入格式

第1行三个正整数n、m、Q,分别表示城市数量、道路数量和询问次数。
第2行到第m+1行每行三个正整数x、y、w,表示x号城市和y号城市之间有一条“崎岖度”为w的双向道路。
第m+2行至第m+Q+1行,每行一个正整数k,表示询问中给定的“忍耐度”为k。

输出格式

共Q行,对于每次询问做出回答。

测试样例1

输入

5 5 2 
1 2 1 
2 3 2 
3 4 1 
4 5 4 
5 1 1 

2

输出


10

备注

【样例说明】
第一个询问:(1,2)、(1,5)、(2,5)、(3,4)。其中(2,5)的具体走法为:2-1-5
第二个询问:(1,2)、(1,3)、(1,4)、(1,5)、(2,3)、(2,4)、(2,5)、(3,4)、(3,5)、(4,5)。其中(4,5)的具体走法为:4-3-2-1-5
【数据规模】
对于20%的数据满足n<=20,m<=40,Q<=40;
对于40%的数据满足n<=1000,m<=2000,Q<=1000;
对于60%的数据满足n<=3000,m<=6000,Q<=200000;
对于100%的数据满足n<=100000,m<=200000,Q<=200000。其他数不超过10^9。
【细节提示】
1 给出的n个城市不一定全部互相连通,且两个城市之间可能存在多条道路,也可能存在某条边是从某城市出发回到他自己。
2 对于询问的结果可能很大,请注意使用适当的类型存储。

询问
问题描述
背景:
在一个美丽的国度里,有n座城市,在每个城市中都住着一个球老板,这些球老板们非常喜
欢互相串门。。。现在YPZ的侄子XPZ的突然对这个妙不可言的国度产生了兴趣,他对你
提出了Q个问题,希望得到你的回答。。。

题目描述:
现在有一个n个点m条边的无向图,每条边有一个”崎岖度”。从X城市经过若干条道路到达Y
城市,我们称这次旅行的“代价”为所经过道路“崎岖度”的最大值。当然,如果从X城市到Y城
市有多条路线,球老板们会自觉选择“代价”最小的路线进行串门。如果旅行的“代价”超过了
他们的“忍耐度”,他们就不选择这次串门了,甚至宁愿在家里宅着。
现在XPZ想进行Q次询问:给定球老板们的“忍耐度”,问还有多少对城市(X,Y)会存在串门?
请你对XPZ的每次询问分别给出回答。
输入格式
1行三个正整数n、m、Q,分别表示城市数量、道路数量和询问次数。
第2行到第m+1行每行三个正整数x、y、w,表示x号城市和y号城市之间有一条“崎岖度”为w
的双向道路。
第m+2行至第m+Q+1行,每行一个正整数k,表示询问中给定的“忍耐度”为k。
输出格式
共Q行,对于每次询问做出回答。
样例输入输出
样例输入1
5 5 2
1 2 1
2 3 2
3 4 1
4 5 4
5 1 1
1
2
样例输出1
4
10
限制与约定对于20%的数据满足n<=20,m<=40,Q<=40;
对于50%的数据满足n<=3000,m<=6000,Q<=200000;
对于100%的数据满足n<=100000,m<=200000,Q<=200000。其他数不超过10^9。
提示:
1 给出的n个城市不一定全部互相连通,且两个城市之间可能存在多条道路,也可能存在某
条边是从某城市出发回到他自己。
2 对于询问的结果可能很大,请注意使用适当的类型存储。

思路:离线,先读入询问,然后按顺序加边,对于每个询问统计一下即可。。。考试居然没做出来,中午20min切了。。。

 1 // It is made by XZZ
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 #define rep(a,b,c) for(rg int a=b;a<=c;a++)
 6 #define drep(a,b,c) for(rg int a=b;a>=c;a--)
 7 #define erep(a,b) for(rg int a=fir[b];a;a=nxt[a])
 8 #define il inline
 9 #define rg register
10 #define vd void
11 typedef long long ll;
12 il int gi(){
13     rg int x=0;rg char ch=getchar();
14     while(ch<'0'||ch>'9')ch=getchar();
15     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
16     return x;
17 }
18 const int maxn=100001,maxm=200001;
19 struct edge{int a,b,c;}E[maxm];
20 bool operator < (edge a,edge b){return a.c<b.c;}
21 int fa[maxn];ll siz[maxn],ans;
22 struct qq{int q,id;ll ans;}Q[200001];
23 il bool cmpq1(qq a,qq b){return a.q<b.q;}
24 il bool cmpq2(qq a,qq b){return a.id<b.id;}
25 il int hd(int a){
26     if(a==fa[a])return a;
27     else return fa[a]=hd(fa[a]);
28 }
29 il vd Union(int a,int b){
30     rg int A=hd(a),B=hd(b);
31     if(A^B)ans+=siz[A]*siz[B],siz[A]+=siz[B],fa[B]=A;
32 }
33 int main(){
34     rg int n=gi(),m=gi(),q=gi();
35     rep(i,1,m)E[i].a=gi(),E[i].b=gi(),E[i].c=gi();
36     sort(E+1,E+m+1);
37     rep(i,1,n)fa[i]=i,siz[i]=1;
38     rep(i,1,q)Q[i]=(qq){gi(),i};
39     sort(Q+1,Q+q+1,cmpq1);
40     rg int NOW=1;
41     rep(i,1,q){
42     while(E[NOW].c<=Q[i].q)Union(E[NOW].a,E[NOW].b),++NOW;
43     Q[i].ans=ans;
44     }
45     sort(Q+1,Q+q+1,cmpq2);
46     rep(i,1,q)printf("%lld
",Q[i].ans);
47     return 0;
48 }
View Code
原文地址:https://www.cnblogs.com/xzz_233/p/7221211.html