HDU 4677 Query on Graph 并查集+分块
来源:互联网 发布:淘宝网斜挎包包 编辑:程序博客网 时间:2024/06/11 06:24
Query on Graph
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 482 Accepted Submission(s): 190
Problem Description
Given an undirected and connected graph with n vertices and m edges, and q queries on the graph. In each query, you are given two integers l, r, and output the number of connected components of the graph only with vertices labelled from l to r (that means deleting the vertices labelled 1, 2, ..., l-1, r+1, r+2, ... , n and the corresponding edges from the graph, then output the number of connected components).
Input
The first line of the input contains an integer T (T<=10), indicating the number of test cases.
For each test case, the first line contains two integers n, m (1<=n<=30000, n-1<=m<=3*n), as described above.
For next m lines, each line contains two integers u, v (1<=u, v<=n), indicates there is an edge between vertex u and vertex v.
Next line is an integer q(1<=q<=30000), indicates the number of queries.
For next q lines, each line contains two integers l, r (1<=l<=r<=n), indicates the query.
The graph are generated randomly and there are only few big datas.
For each test case, the first line contains two integers n, m (1<=n<=30000, n-1<=m<=3*n), as described above.
For next m lines, each line contains two integers u, v (1<=u, v<=n), indicates there is an edge between vertex u and vertex v.
Next line is an integer q(1<=q<=30000), indicates the number of queries.
For next q lines, each line contains two integers l, r (1<=l<=r<=n), indicates the query.
The graph are generated randomly and there are only few big datas.
Output
For each test case, output "Case #X:" first, X is the test number. Then output q lines, each with a number, the answer to each query.
Sample Input
19 101 21 32 42 54 53 63 76 98 98 741 23 56 96 7
Sample Output
Case #1:1212
题意:给出一个n个点m条边的图,q询问,每次询问一个区间[l,r]要求出该区间内的点所构成的连通块数量。
连通块的计算可以用并查集来解决,但是q次查询不可能每次都O(n)得出,因此对q次查询离线处理。
将n个点分块,一每次查询的l所在块好为第一条件,r的位置为第二条件从小到大排序,对所有l位于同一块的查询一起处理,
将[l,r]分为左右两个部分,mid为l所在块的最后一位,因为l是递增的,所以右半部分查询的复杂度为O(maxr),二左半部分每次暴力查询,
复杂度位根号级别。因为右边的l是递增的,因此rp只需要在块的位置变化时初始化,而对于左半部分开一个标记数组,如果没被标记则lp[i]=rp[i]。这样左右部分就可以合并了。
#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>using namespace std;const int maxm = 30005;struct node{int b, l, r, id;bool operator<(const node &rr)const{if (b != rr.b) return b < rr.b;else return r < rr.r;}}a[maxm];int block, R, magic, now, cnt, n, m;int vis[maxm], ans[maxm], lp[maxm], rp[maxm];vector<int>v[maxm];int Rfind(int k){return k == rp[k] ? k : rp[k] = Rfind(rp[k]);}int Lfind(int k){if (vis[k] != now)vis[k] = now, lp[k] = rp[k];return k == lp[k] ? k : lp[k] = Lfind(lp[k]);}int work(int l, int r, int b){int i, j, k, sum = 0, mid;mid = magic*(b + 1) - 1;if (block != b){block = b, cnt = 0, R = mid;for (i = mid - magic + 1;i <= n;i++) rp[i] = i;}for (int i = max(R, mid) + 1;i <= r;i++){cnt++;for (int j = 0;j < v[i].size();j++){if (v[i][j] > r || v[i][j] <= mid) continue;int t1 = Rfind(i), t2 = Rfind(v[i][j]);if (t1 == t2) continue;rp[t1] = rp[t2], cnt--;}}for (int i = l;i <= min(r, mid);i++){sum++;for (int j = 0;j < v[i].size();j++){if (v[i][j]<l || v[i][j]>r) continue;int t1 = Lfind(i), t2 = Lfind(v[i][j]);if (t1 == t2) continue;lp[t1] = lp[t2], sum--;}}R = r;return sum + cnt;}int main(){int i, j, k, sum, t, m, x, y, cas = 0;scanf("%d", &t);while (t--){scanf("%d%d", &n, &m);magic = (int)sqrt(n + 0.5);memset(vis, -1, sizeof(vis));for (i = 1;i <= n;i++) v[i].clear();for (i = 1;i <= m;i++){scanf("%d%d", &x, &y);v[x].push_back(y), v[y].push_back(x);}scanf("%d", &m);for (i = 1;i <= m;i++){scanf("%d%d", &a[i].l, &a[i].r);a[i].b = a[i].l / magic, a[i].id = i;}sort(a + 1, a + 1 + m);block = -1;for (i = 1;i <= m;i++){j = i;while (a[j].b == a[i].b&&j <= m) j++;for (k = i;k < j;k++){now = k;ans[a[k].id] = work(a[k].l, a[k].r, a[k].b);}i = j - 1;}printf("Case #%d:\n", ++cas);for (i = 1;i <= m;i++) printf("%d\n", ans[i]);}return 0;}
阅读全文
0 0
- HDU 4677 Query on Graph 并查集+分块
- HDU 4677 Query on Graph
- hdu 4677 Query on Graph
- hihocoder1629:Graph (分块+并查集)
- 【HDU】4677 Query on Graph 动态树
- HDOJ 4677 Query on Graph
- hdu 4677 并查集+分块算法 好题 (2013多校联合)
- hdu 4677 并查集+分块算法 好题 (2013多校联合)
- HDU 3560 Graph’s Cycle Component 【并查集】
- HDU 3560 并查集 Graph’s Cycle Component
- HDU 5354 Bipartite Graph[cdq+并查集]
- 【hdu 5354】Bipartite Graph【分治 并查集】
- HDU 3276 Graph and Queries [离线+并查集+treap]
- HDU 5631 Rikka with Graph(并查集)
- HDU 5631 Rikka with Graph (并查集)
- HDU Rikka with Graph (并查集+枚举)
- 并查集-HDU-5631-Rikka with Graph
- hdu 5631 Rikka with Graph(并查集)
- hadoop37
- 【linux 磁盘管理】Linux磁盘管理常用三个命令为df、du和fdisk。
- log4详细配置
- phpcms v9 专题添加自定义字段及其调用
- 685
- HDU 4677 Query on Graph 并查集+分块
- 日常知识整理---web项目字典设计与管理
- bzoj 1965: [Ahoi2005]SHUFFLE 洗牌
- 取list中的值时候总是取到第一个值
- CSS 元素垂直居中的 6种方法
- java5的拆箱与装箱
- FreeRTOS入门学习记录2——队列管理
- Tomcat配置虚拟路径
- Nessus 漏洞扫描软件安装使用