Travel---hdu5441(并查集)】的更多相关文章

Travel Problem Description 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,…
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1044  Solved: 363[Submit][Status][Discuss] Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数:从牛棚_1到牛棚_i+1并且避免从牛棚1到牛棚i+1最…
Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1313    Accepted Submission(s): 472 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr…
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u到城市w的长度e1≤d,同时城市w到城市v的长度e2≤d). 析:一开始的时候,题意都读错了,怎么看都不对,原来是只要最大的d小于等于x就可以,过了好几天才知道是这样..... 这个题是并查集的应用,先从d小的开始遍历,然后去判断有几个连通块,在连通块中计数,用一个数组即可,运用排列组合的知识可以知…
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点对(x,y)使得在x到y的路径上没有一条边的距离大于d. 思路:只要边距离小于d,那就是可行的,直接加入并查集来维护.并查集需要维护一下树的节点个数. 将边和询问都排序离线处理. #include<iostream> #include<cstdio> #include<cstri…
Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2055    Accepted Submission(s): 709 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr…
这题计算 一张图上 能走的 点对有多少个  对于每个限制边权 , 对每条边排序,对每个查询排序 然后边做克鲁斯卡尔算法 的时候变计算就好了 #include <iostream> #include <algorithm> #include <string.h> #include <cstdio> #include <vector> #include <queue> using namespace std; ; typedef long…
对于每次询问的大的值,都是从小的值开始的,那就从小到大处理,省去很多时间,并且秩序遍历一遍m; 这题cin容易超时,scanf明显快很多很多.G++又比C++快; //这代码scanf400+,cin800+ 并且我交了快10次cin的8次超时 //scanf代码C++400+,G++250+ #include<cstring> #include<iostream> #include<algorithm> #define maxn 20010 using namespa…
Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Description 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 bidir…
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 Description 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…
Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr…
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数:从牛棚_1到牛棚_i+1并且避免从牛棚1到牛棚i+1最短路经上最后一条牛路的最少的时间.如果这样的路经不存在,输出-1. Sample Input 4 5 1 2 2 1 3 2 3 4 4 3 2 1 2 4 3 输入解释:…
有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数:从牛棚_1到牛棚_i+1并且避免从牛棚1到牛棚i+1最短路经上最后一条牛路的最少的时间.如果这样的路经不存在,输出-1. 题目分析 做法一 暴力树剖线段树 做法二 并查集[bzoj1576] [Usaco2009 Jan]安全路经Travel #include<bits/stdc++…
题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each one walks from the barn (conveniently located at pasture_1) to the other fields, with cow_i traveling to from pasture_1 to pasture_i. Each gremlin is…
题意:有一张无向图,一些点之间有有权边,某条路径的值等于路径上所有边的边权的最大值,而某个点对的值为这两点间所有路径的值的最小值,给出多个询问,每个询问有一个值,询问有多少点对满足其值小于等于询问值.点的顺序不同算作不同点对. 这题的做法很类似Kruskal算法.一开始所有的点都为一个并查集,从权值最小的边开始,当加入这条边的时候,这条边连接的两个点(并查集)之间相互到达的路径中,值最小的一个路径一定就是通过这条边的,所以这两点间的值就是这条边的权值.之后每加入一条最小边,如果可以用来合并两个并…
题意:给一个图,若干询问,每次询问只经过边权<=w的边,x能到达的点数 并查集啊,对询问和边排序,直接合并,维护size,查询 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #define N 105000 using namespace std; int fa[N],size[N],n,m,k; struc…
题目链接 BZOJ 洛谷 先求最短路树.考虑每一条非树边(u,v,len),设w=LCA(u,v),这条边会对w->v上的点x(x!=w)有dis[u]+dis[v]-dis[x]+len的距离. 每条边用dis[u]+div[v]+len更新链.树剖就做完了. 因为每个点只需取最小值,所以把边按dis[u]+div[v]+len排序后并查集更新链也行. 复杂度\(O(n\alpha(n)+mlogm)\). 树DP失败,好像没法处理子树内的ndis互相更新..唉. //12680kb 556m…
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数:从牛棚_1到牛棚_i+1并且避免从牛棚1到牛棚i+1最短路经上最后一条牛路的最少的时间. 如果这样的路经不存在,输出-1. Sample Input 4 5 1 2 2 1 3 2 3 4 4 3 2 1 2 4 3 Sample Output 3 3 6—————————————————————…
题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个贪心开心的飞起然而只多得了10分qwq 正解比较神仙. 首先把最短路树建出来,考虑一条非树边$(u, v)$什么时候能更新答案 结论是:除了他们的LCA外的子树内其他都可以更新,且新的权值为$dis[u] + dis[v] + w(u, v) - dis[x]$,$x$表示新节点 这样我们把所有的边…
传送门 蒟蒻我原本还想着跑两边spfa,发现不行,就gg了. 首先这道题卡spfa,所以需要用堆优化的dijkstra求出最短路径 因为题目中说了,保证最短路径有且只有一条,所以可以通过dfs求出最短路径树 发现,需要给这课树加边,才能有别的路径到达一个点x 那么我们连接树上两个节点u,v,边权为w 发现,u,v到两点公共祖先的路径上的所有点(除去lca)的答案都会受到影响 且ans[i] = dis[u] + dis[v] + w - dis[i] 要使得ans最小,需要dis[u] + di…
题目传送门 题意:给一张无向图,问存在多少(a, b)表示a点到b点经过的边值小于等于x ((a,b) 和 (b, a)属于不同的方案) 分析:首先将边权值和查询x值升序排序,从前往后扫描边,累加从u和v两个集合各自选取一个组成(a, b)的方案数(u,v属于不同的集合),不能从一个集合选两个,因为同一个集合的方案数已经计算过了.然后将u和v合并到一个集合,在O (m) 复杂度得到答案 收获:并查集真是个很好的数据结构 代码: /*********************************…
在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime.The farm consists of NN barns con…
A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Bein…
Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2102 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o…
Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1…
Travel Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u /*题意 给出n[节点数],m[连线数],q[查询次数]; 输入m组数据 u[起点],v[终点],cost[权值]; (u,v)和 (v,u)表示两种不同的连线 输入q组查询数据: 要求 输出在m组数据中所有cost<q的所有节点连线的连线方式个数. 注意:每个拥有n个节点的联通块有 n*(n-1)个联通分量 (连线数):…
Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of…
Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains…
Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1852    Accepted Submission(s): 641 Problem Description Jack likes to travel around the world, but he doesn't like to wait. Now, he is t…
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 100…