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
1 9 10 1 2 1 3 2 4 2 5 4 5 3 6 3 7 6 9 8 9 8 7 4 1 2 3 5 6 9 6 7
Sample Output
Case #1: 1 2 12
题意:给出一个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; }