CodeForces 292D Connected Components(变种并查集+预处理)

秒速五厘米 2022-06-07 13:17 214阅读 0赞

We already know of the large corporation where Polycarpus works as a system administrator. The computer network there consists of n computers and m cables that connect some pairs of computers. In other words, the computer network can be represented as some non-directed graph with n nodes and m edges. Let’s index the computers with integers from 1 to n, let’s index the cables with integers from 1 to m.

Polycarpus was given an important task — check the reliability of his company’s network. For that Polycarpus decided to carry out a series of k experiments on the computer network, where the i-th experiment goes as follows:

  1. Temporarily disconnect the cables with indexes from l**i to r**i, inclusive (the other cables remain connected).
  2. Count the number of connected components in the graph that is defining the computer network at that moment.
  3. Re-connect the disconnected cables with indexes from l**i to r**i (that is, restore the initial network).

Help Polycarpus carry out all experiments and for each print the number of connected components in the graph that defines the computer network through the given experiment. Isolated vertex should be counted as single component.

Input

The first line contains two space-separated integers n, m (2 ≤ n ≤ 500; 1 ≤ m ≤ 104)— the number of computers and the number of cables, correspondingly.

The following m lines contain the cables’ description. The i-th line contains space-separated pair of integers x**i, y**i (1 ≤ x**i, y**i ≤ n; x**i ≠ y**i) — the numbers of the computers that are connected by the i-th cable. Note that a pair of computers can be connected by multiple cables.

The next line contains integer k (1 ≤ k ≤ 2·104) — the number of experiments. Next k lines contain the experiments’ descriptions. The i-th line contains space-separated integers l**i, r**i (1 ≤ l**i ≤ r**i ≤ m) — the numbers of the cables that Polycarpus disconnects during the i-th experiment.

Output

Print k numbers, the i-th number represents the number of connected components of the graph that defines the computer network during the i-th experiment.

Example

Input

  1. 6 5
  2. 1 2
  3. 5 4
  4. 2 3
  5. 3 1
  6. 3 6
  7. 6
  8. 1 3
  9. 2 5
  10. 1 5
  11. 5 5
  12. 2 4
  13. 3 3

Output

  1. 4
  2. 5
  3. 6
  4. 3
  5. 4
  6. 2

题解:

题意:

给你一张节点数为n的图,然后给m条边的情况,然后是s组询问,问你去掉[l,r]范围内的边最后可以形成多少个连通块

思路:

一开始用dfs暴力写,华丽地tle了,然后想到了并查集,然后又暴力写,又是tle,冷静下来以后发现每次都求一遍并查集很浪费时间,然后就想到了预处理一遍,因为每次都是除去[l,r]后的情况,那么就可以先预处理一遍加入了[1,l-1]的边的情况和加入了[r+1,m]的边的情况,然后合并这两个集合就可以了,然后就枚举这些情况保存下来备用

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<stdio.h>
  4. #include<math.h>
  5. #include<string>
  6. #include<stdio.h>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<deque>
  12. #include<algorithm>
  13. #define ll long long
  14. #define INF 1008611111
  15. #define M (t[k].l+t[k].r)/2
  16. #define lson k*2
  17. #define rson k*2+1
  18. using namespace std;
  19. struct edge
  20. {
  21. int f,t;
  22. }a[10005];
  23. int prel[10005][505];
  24. int prer[10005][505];
  25. int ppre[505];
  26. int find(int pre[],int x)
  27. {
  28. if(pre[x]!=x)
  29. pre[x]=find(pre,pre[x]);
  30. return pre[x];
  31. }
  32. int l,r;
  33. int main()
  34. {
  35. int i,j,n,m,x,y,s,d1,d2;
  36. scanf("%d%d",&n,&m);
  37. for(i=1;i<=m;i++)
  38. {
  39. scanf("%d%d",&x,&y);
  40. a[i].f=x;
  41. a[i].t=y;
  42. }
  43. for(j=1;j<=n;j++)
  44. {
  45. prel[0][j]=j;
  46. }
  47. for(i=1;i<=m;i++)
  48. {
  49. for(j=1;j<=n;j++)
  50. {
  51. prel[i][j]=prel[i-1][j];
  52. }
  53. d1=find(prel[i],a[i].f);
  54. d2=find(prel[i],a[i].t);
  55. if(d1!=d2)
  56. {
  57. prel[i][d2]=d1;
  58. }
  59. }
  60. for(j=1;j<=n;j++)
  61. {
  62. prer[m+1][j]=j;
  63. }
  64. for(i=m;i>=1;i--)
  65. {
  66. for(j=1;j<=n;j++)
  67. {
  68. prer[i][j]=prer[i+1][j];
  69. }
  70. d1=find(prer[i],a[i].f);
  71. d2=find(prer[i],a[i].t);
  72. if(d1!=d2)
  73. {
  74. prer[i][d2]=d1;
  75. }
  76. }
  77. scanf("%d",&s);
  78. while(s--)
  79. {
  80. scanf("%d%d",&l,&r);
  81. int ans=0;
  82. if(l>r)
  83. swap(l,r);
  84. for(i=1;i<=n;i++)
  85. {
  86. ppre[i]=prel[l-1][i];
  87. //printf("prel[%d]=%d!!\n",i,ppre[i]);
  88. }
  89. for(i=1;i<=n;i++)
  90. {
  91. d1=find(ppre,i);
  92. d2=find(prer[r+1],i);
  93. d2=find(ppre,d2);
  94. //printf("prer[%d]=%d!!\n",i,d2);
  95. if(d1!=d2)
  96. {
  97. ppre[d2]=d1;
  98. }
  99. }
  100. for(i=1;i<=n;i++)
  101. {
  102. if(ppre[i]==i)
  103. ans++;
  104. }
  105. printf("%d\n",ans);
  106. }
  107. return 0;
  108. }

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces 731C Socks

    1 //题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色, 2 //问要让每天的袜子是相同颜色的,要重新