HDU 5441 Travel(2015 ACM/ICPC Asia Regional Changchun Online)
2016-10-20 15:57
225 查看
题目分析
这道理第一感觉就是暴力,但是数据量实在太大,但是我们可以有策略的暴力,这道题我的方法就是先排序再暴力,首先我们可以对于距离进行排序,然后我们离线处理q次询问,同样的方法对于q此询问的距离进行排序(注意存标号)。然后用并查集处理即可,即可方法见代码。#include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1e5+100; struct Edge{ int from, to, dist; bool operator < (const Edge temp)const{ return dist < temp.dist; } }e[maxn]; int fa[20005], sum[20005], ans[5005]; int Find(int x){ if(fa[x] == x) return x; fa[x] = Find(fa[x]); return fa[x]; } struct Node{ int id, d; bool operator < (const Node temp)const{ return d < temp.d; } }node[5005]; void init(){ for(int i = 0; i < 20005; i++) fa[i] = i, sum[i] = 1; } int main(){ int T, n, m, q; scanf("%d", &T); while(T--){ init(); scanf("%d%d%d", &n, &m, &q); for(int i = 0; i < m; i++) scanf("%d%d%d", &e[i].from, &e[i].to, &e[i].dist); sort(e, e+m); for(int i = 0; i < q; i++){ node[i].id = i; scanf("%d", &node[i].d); } sort(node, node+q); int j = 0; for(int i = 0; i < q; i++){ for(; e[j].dist <= node[i].d && j < m; j++){ int xx = Find(e[j].from); int yy = Find(e[j].to); if(xx != yy){ fa[xx] = yy; sum[yy] += sum[xx]; sum[xx] = 0; } } int cnt = 0; for(int k = 1; k <= n; k++){ if(fa[k] == k) cnt += (sum[k]*(sum[k]-1)); } ans[node[i].id] = cnt; } for(int i = 0; i < q; i++) printf("%d\n", ans[i]); } return 0; }
相关文章推荐
- HDU 5441 Travel(离线 + 带权并查集)——2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5437 2015 ACM/ICPC Asia Regional Changchun Online 1002 优先队列
- HDU 5437 Alisha’s Party (Priority_queue)2015 ACM/ICPC Asia Regional Changchun Online
- 2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel (Krsukal变形)
- HDU 5437 Alisha’s Party(优先队列+模拟)——2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5443 The Water Problem 水题 2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5444 Elven Postman 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5443 The Water Problem 2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5444 Elven Postman 树的先序遍历 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5437 Alisha’s Party 优先队列 2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5438 Ponds 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5446 Unknown Treasure(lucas定理+中国剩余定理)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5443 The Water Problem(水题 找区间最大值)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
- hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5441 Travel 并查集 2015 ACM/ICPC Asia Regional Changchun Online
- hdu 5438 Ponds 拓扑排序+并查集 2015 ACM/ICPC Asia Regional Changchun Online
- HDU 5444 Elven Postman (2015 ACM/ICPC Asia Regional Changchun Online)