codeforces1213G Path Queries 并查集】的更多相关文章

题意 给定n个结点的树,每条边有边权,有m个询问,每个询问给一个\(q_i\)输出树上有多少点对的简单路径上最大的边权不超过\(q_i\). 分析 用并查集维护点集,同时维护大小. 将所有边按边权排序,考虑每次从小到大加边,图中经过当前边的所有路径一定是以当前边的边权为最大值的,用并查集维护下图中每个联通块的大小,经过当前边的路径数即为\(sz[find(u)]*sz[find(v)]\).然后前缀和一下就可以\(O(1)\)询问了. Code #include<bits/stdc++.h>…
题目:https://vjudge.net/contest/323699#problem/A 题意:给你一棵树,然后有m个查询,每次查询问一条路径最大边小于给定查询的数量 思路:首先我们看到,我们其实可以计算出每个边权小于查询的所有连通块,然后sum+C(n,2),对每个连通块都加上值,然后就是答案了,但是这里注意查询数很多,我们肯定不能O(n)遍历每个查询,但是思路肯定是计算联通块里组合数的数量,怎么处理呢,我们注意到,他这个边权是的值的大小和我的连通块的有关,我们是否可以利用之前求出来的值呢…
题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的\(q_i\)排序,我们可以枚举\(q_i\),然后从last开始遍历边权,如果边权不大于\(q_i\),那么就可以用并查集将两个连通块合并且计数(因为我们是从小到大枚举的,所以将它们合并并不会对后面有影响,反而还会方便我们计数),\(cnt\)表示连通块的节点数,合并时贡献为\(res=cnt[f…
传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). 代码: #include<bits/stdc++.h> #define ri register int using namespace std; inline int read(){ int ans=0; char ch=getchar(); while(!isdigit(ch))ch=getc…
给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要好写很多. 平衡树(Treap)版: #include<bits/stdc++.h> typedef long long ll; using namespace std; ; struct E { int u,v; } e[N]; int a[N],faz[N],n,m,del[N]; int f…
[CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出现时间是区间,所以线段树分治. 既然路径最小值就是异或最小值,并且可以不是简单路径, 不难让人想到\(WC2011\)那道最大\(Xor\)路径和. 用一样的套路,我们动态维护一棵生成树,碰到一个非树边, 就把这个环的异或和丢到线性基里面去,这样子直接查就好了. 动态维护生成树直接用并查集就好了,没…
洛谷 Codeforces 分治的题目,或者说分治的思想,是非常灵活多变的. 所以对我这种智商低的选手特别不友好 脑子不好使怎么办?多做题吧-- 前置知识 线性基是你必须会的,不然这题不可做. 推荐再去看看洛谷P4151. 思路 看到异或最短路,显然线性基. 做题多一些的同学想必已经想到了"洛谷P4151 [WC2011]最大XOR和路径"了. 先考虑没有加边删边的做法: 做出原图的任意一棵生成树: 把每个非树边和树边形成的环丢进线性基里: 询问时把两点在树上的路径异或和丢进线性基里求…
Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] You are given an undirected graph with N vertexes and M edges. Every vertex in this graph has an integer value assigned to it…
之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]!=root) root = pre[root]; int i = x,j; while(i!=root){ //path compress j = pre[i]; pre[i] = root; i = j; } return root; } void join(int x,int y){ int f…
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足\(u\)到\(v\)这条路径上的最大值不超过\(k\).\(q\)次查询. [Solution] 并查集 将所有边按权值从小到大排序,查询值\(k_i\)也从小到大排序.对于每次查询的值\(k_i\),将边权小于等于\(k_i\)的边通过并查集合并在一起.对于合并后的联通块,每个联通块对答案的贡献…
Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre…
题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <string> using namespace std; /* 并查集用来判断图的联通性,有一组样例图是不连通的... */ ; ]; struct UF{ in…
[题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long using namespace std; l…
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…
CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/contest/455/problem/C C. Civilization time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Andrew p…
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…
Description standard input/output Statements Alex is known to be very clever, but Walter does not believe that. In order to test Alex, he invented a new game. He gave Alex nnodes, and a list of queries. Walter then gives Alex one query every second,…
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/C Description Andrew plays a game called "Civilization". Dima helps him. The game has n cities and m bidirectional roads. The cities are numbe…
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a game called "Civilization". Dima helps him. The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pa…
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/506/problem/D Description Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered…
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…
http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1921    Accepted Submission(s): 955 Problem Description ZLGG found a magic theory that the b…
题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS 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)…
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7783   Accepted: 2801 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usuall…
Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7871   Accepted: 2831 Case Time Limit: 1000MS 题目链接:http://poj.org/problem?id=1984 Description: Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usua…
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7136   Accepted: 2556 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usual…
这两道都用到了线段树分治和按秩合并可撤销并查集. Shortest Path Queries 给出一个连通带权无向图,边有边权,要求支持 q 个操作: x y d 在原图中加入一条 x 到 y 权值为 d 的边 x y 把图中 x 到 y 的边删掉 x y 表示询问 x 到 y 的异或最短路 保证任意操作后原图连通无重边自环且操作均合法 n,m,q≤200000 题解 与WC2011 最大XOR和路径一样,先考虑没有加边删边的做法 做出原图的任意一棵生成树 把每个非树边和树边形成的环丢进线性基里…
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问x到y的路径异或最小值 保证图在任意时刻连通 首先连通图路径异或相当于从x到y的任意一条路径再异或上若干个环得到的,只要在dfs过程中把非树边成的环丢到线性基里就好了,其他环一定可以通过这些环异或组合出来 有加边删边操作怎么做呢?线段树时间分治!注意到不能保证在线段树的任意一个节点图是连通的,需要用…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connected graph G with n nodes and m edges, with possibly repeated edges and/or loops. The stability of connectedness between node u and node v is defined by…
在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. 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…