Travel HDU
来源:互联网 发布:福建网络安全教育 编辑:程序博客网 时间:2024/06/08 19:15
Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are
Input
The first line contains one integer
For each test case, the first line consists of three integers
Each of the following mm lines consists of three integers
Then qq lines follow. Each of them is a query consisting of an integer xx where
Output
You should print q lines for each test case. Each of them contains one integer as the number of pair of cities
Note that
Sample Input
1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000
Sample Output
2
6
12
这题我没读懂题意,一直认为在所有一个点到另一个点的用时必须在规定的询问的时间之内,所有一直看不懂样例。
那么在给定时间,则只有小于这个时间的边才是有效边,然后建图,找连通分量的大小即
#include<cstdio>#include<algorithm>#include<cstring>#define N 2005#define INF 0x3f3f3f3ftypedef long long ll;using namespace std;int n,m,q;struct node{ int x1,x2; int w;}edge[100005];int cmp1(node p1,node p2){ return p1.w<p2.w;}int num[20005];int pre[20005];struct node2{ int val; int index;}query[5005];int cmp2(node2 p1,node2 p2){ return p1.val<p2.val;}long long ans[5005];void init(){ for(int i=1;i<=n;i++) { pre[i]=i; num[i]=1; }}int findd(int x){ return pre[x]==x?x:pre[x]=findd(pre[x]);}long long get(int x){ return x*x-x;}int main(){ int t; int x,y,w; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&q); init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&w); edge[i].x1=x; edge[i].x2=y; edge[i].w=w; } sort(edge,edge+m,cmp1); for(int i=0;i<q;i++) { scanf("%d",&w); query[i].val=w; query[i].index=i; } sort(query,query+q,cmp2); long long temp=0; for(int i=0,j=0;i<q;i++) { while(j<m&&(edge[j].w<=query[i].val)) { int xx=findd(edge[j].x1); int yy=findd(edge[j].x2); if(xx==yy) { j++; continue; } if(num[xx]==1) { if(num[yy]==1) { pre[xx]=yy; num[yy]++; temp+=get(2); } else { temp-=get(num[yy]); pre[xx]=yy; num[yy]++; temp+=get(num[yy]); } } else { if(num[yy]==1) { temp-=get(num[xx]); pre[yy]=xx; num[xx]++; temp+=get(num[xx]); } else { temp-=get(num[xx]); temp-=get(num[yy]); pre[xx]=yy; num[yy]+=num[xx]; temp+=get(num[yy]); } } j++; } ans[query[i].index]=temp; } for(int i=0;i<q;i++) printf("%lld\n",ans[i]); } return 0;}
- hdu Travel
- hdu-travel
- Travel HDU
- Travel HDU
- Travel HDU
- hdu 1208 pascal travel
- HDU 3053 Group Travel
- hdu 4284 Travel
- HDU 4284 Travel
- hdu 2433 TRAVEL
- hdu 4118 Time travel
- HDU 4284 Travel
- hdu 4418 Time travel
- HDU 4284 Travel
- HDU 4418 Time travel
- HDU-5441 Travel
- hdu 5441 Travel
- HDU 5441 Travel
- SQL中几个快速定位语句
- hdu1198 Farm Irrigation
- Excel 时间戳和时间格式的互相转换
- pandas小记:pandas计算工具-汇总统计
- BZOJ 1131 [POI 2008] 解题报告
- Travel HDU
- SQL取字符串中的数字OR字母
- 霍夫变换(直线检测)
- 用shell控制hql执行,如何控制多个阶段之间串行,阶段内部并行
- 文章标题
- ptyhon学习手册 第四版学习笔记(留坑)
- Kotlin 中的泛型
- Laravel 数据库加密及数据库表前缀配置
- 【牛客 题库】 指针移动 || 类创建对象的个数|| 求字符串数组中长度最短的字符串所在的行下标