【HDU 5441】Travel
来源:互联网 发布:阿里巴巴网络推广 编辑:程序博客网 时间:2024/05/22 10:51
Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities and m bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is x minutes, how many pairs of city (a,b) are there that Jack can travel from city a to b without going berserk?
For each test case, the first line consists of three integers
Each of the following
Then
Note that
15 5 32 3 63341 5 157243 5 57054 3 123821 3 2172660001000013000
2612
离线并查集,每走过一个城市,若其并未在并查集中,便将其代表的点加入并查集,对于(u, v)
如果u和v没有连通,假设u所在连通分量的点个数是n1,v所在的连通分量的点个数是n2
对于第一个连通分量,合并后这个连通分量就不存在了,ans会减少n1*(n1-1)对点
对于第二个连通分量,合并后这个连通分量就不存在了,ans会减少n2*(n2-1)对点
合并后,多了一个合并的连通分量,ans会增加(n1+n2)*(n1+n2-1)对点
之后根据权值大小更新答案AC代码:
#include<map>#include<set>#include<cmath>#include<stack>#include<queue>#include<cstdio>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;struct N1{ int u, v, c; bool operator< (const N1 b) const{ return c < b.c; }}n1[100005];struct N2{ int x, id; bool operator< (const N2 b) const{ return x < b.x; }}n2[100005];int n, m, q, par[100005], rank_[100005];ll ans[100005];int find_(int x){ if(x == par[x]) return x; else return par[x] = find_(par[x]);}inline void init(){ for(int i = 1; i <= n; i++) { par[i] = i; rank_[i] = 1; }}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d%d%d", &n, &m, &q); init(); for(int i = 1; i <= m; i++) { scanf("%d%d%d", &n1[i].u, &n1[i].v, &n1[i].c); } for(int i = 1; i <= q; i++) { scanf("%d", &n2[i].x); n2[i].id = i; } sort(n1 + 1, n1 + 1 + m); sort(n2 + 1, n2 + 1 + q); ll s = 0; int i = 1; for(int in = 1; in <= q; in++) { while(i <= m && n1[i].c <= n2[in].x) { int f1 = find_(n1[i].u); int f2 = find_(n1[i].v); if(f1 != f2) { s = s - rank_[f1] * (rank_[f1] - 1) - rank_[f2] * (rank_[f2] - 1) + (rank_[f1] + rank_[f2]) * (rank_[f1] + rank_[f2] - 1); par[f1] = f2; rank_[f2] += rank_[f1]; } i++; } ans[n2[in].id] = s; } for(int i = 1; i <= q; i++) cout<<ans[i]<<endl; }}
阅读全文
0 0
- HDU-5441 Travel
- hdu 5441 Travel
- HDU 5441 Travel
- HDU 5441Travel
- 【HDU 5441】Travel
- hdu(5441)——Travel
- hdu Travel
- hdu-travel
- Travel HDU
- Travel HDU
- Travel HDU
- HDU 5441 Travel 并查集
- hdu 5441 Travel(离线+并查集)
- hdu 5441 Travel(生成树)
- hdu 5441 Travel(并查集应用)
- HDU 5441 Travel (并查集)
- HDU 5441 Travel(并查集)
- hdu 5441 Travel 排序 并查集
- Laravel基础-模板的引入方式
- 白话空间统计之二十五:空间权重矩阵(二)
- 湖南中医药大学2017年集训队第四场选拔赛-Problem B: Power Eggs
- 从一组字符串中取出数字
- C primer plus(编程练习)file-2.12-7
- 【HDU 5441】Travel
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- C primer plus(编程练习)file-2.12-8
- linux谷歌浏览器代理设置
- wx.navigateTo传递数组对象解决方法
- email正则表达式
- String类深拷贝写法的增删查改
- E.Okabe and El Psy Kongroo
- Python图形工具turtle绘制国际象棋棋盘