Codeforces-292D:Connected Components(m个并查集)
2017-10-27 12:56
405 查看
D. Connected Components
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
We already know of the large corporation where Polycarpus works as a system administrator. The computer network there consists of ncomputers
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:
Temporarily disconnect the cables with indexes from li to ri,
inclusive (the other cables remain connected).
Count the number of connected components in the graph that is defining the computer network at that moment.
Re-connect the disconnected cables with indexes from li to ri (that
is, restore the initial network).
Help Polycarpus c
e620
arry 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 xi, yi (1 ≤ xi, yi ≤ n; xi ≠ yi)—
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 li, ri (1 ≤ li ≤ ri ≤ 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.
Examples
input
output
思路:pre[i]
表示[1,i]中的线连起来构成的并查集,suf[i]
表示[i,m]中的连线构成的并查集。就相当于前缀并查集和后缀并查集。每次查询只需合并一下。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+10;
int pre[MAX][501];
int suf[MAX][501];
int A[501];
int x[MAX],y[MAX];
int f(int *p,int x) //如果这个函数写成 f(int *p,int x){return p[x]==x?x:p[x]=f(p,p[x]);}就TLE。。。。
{
if(p[x]!=x)p[x]=f(p,p[x]);
return p[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&x[i],&y[i]);
for(int i=1;i<=n;i++)pre[0][i]=suf[m+1][i]=i;
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)pre[j][i]=pre[j-1][i];
int fx=f(pre[j],x[j]);
int fy=f(pre[j],y[j]);
if(fx!=fy)pre[j][fx]=fy;
}
for(int j=m;j>=1;j--)
{
for(int i=1;i<=n;i++)suf[j][i]=suf[j+1][i];
int fx=f(suf[j],x[j]);
int fy=f(suf[j],y[j]);
if(fx!=fy)suf[j][fx]=fy;
}
int T;scanf("%d",&T);
while(T--)
{
int x,y;
scanf("%d%d",&x,&y);
x--;
y++;
for(int i=1;i<=n;i++)A[i]=pre[x][i];
for(int i=1;i<=n;i++)
{
int fx=f(A,i);
int fy=f(suf[y],i);
fy=f(A,fy);
if(fx!=fy)A[fx]=fy;
}
int ans=0;
for(int i=1;i<=n;i++)if(A[i]==i)ans++;
printf("%d\n",ans);
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
We already know of the large corporation where Polycarpus works as a system administrator. The computer network there consists of ncomputers
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:
Temporarily disconnect the cables with indexes from li to ri,
inclusive (the other cables remain connected).
Count the number of connected components in the graph that is defining the computer network at that moment.
Re-connect the disconnected cables with indexes from li to ri (that
is, restore the initial network).
Help Polycarpus c
e620
arry 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 xi, yi (1 ≤ xi, yi ≤ n; xi ≠ yi)—
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 li, ri (1 ≤ li ≤ ri ≤ 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.
Examples
input
6 5 1 2 5 4 2 3 3 1 3 6 6 1 3 2 5 1 5 5 5 2 4 3 3
output
4 5 6 3 4 2
思路:pre[i]
表示[1,i]中的线连起来构成的并查集,suf[i]
表示[i,m]中的连线构成的并查集。就相当于前缀并查集和后缀并查集。每次查询只需合并一下。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4+10;
int pre[MAX][501];
int suf[MAX][501];
int A[501];
int x[MAX],y[MAX];
int f(int *p,int x) //如果这个函数写成 f(int *p,int x){return p[x]==x?x:p[x]=f(p,p[x]);}就TLE。。。。
{
if(p[x]!=x)p[x]=f(p,p[x]);
return p[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&x[i],&y[i]);
for(int i=1;i<=n;i++)pre[0][i]=suf[m+1][i]=i;
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)pre[j][i]=pre[j-1][i];
int fx=f(pre[j],x[j]);
int fy=f(pre[j],y[j]);
if(fx!=fy)pre[j][fx]=fy;
}
for(int j=m;j>=1;j--)
{
for(int i=1;i<=n;i++)suf[j][i]=suf[j+1][i];
int fx=f(suf[j],x[j]);
int fy=f(suf[j],y[j]);
if(fx!=fy)suf[j][fx]=fy;
}
int T;scanf("%d",&T);
while(T--)
{
int x,y;
scanf("%d%d",&x,&y);
x--;
y++;
for(int i=1;i<=n;i++)A[i]=pre[x][i];
for(int i=1;i<=n;i++)
{
int fx=f(A,i);
int fy=f(suf[y],i);
fy=f(A,fy);
if(fx!=fy)A[fx]=fy;
}
int ans=0;
for(int i=1;i<=n;i++)if(A[i]==i)ans++;
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 中国(北方)大学生程序设计训练赛(第二周)(Problem G: Connected Components-并查集)
- Codeforces 292 D. Connected Components (并查集)
- Codeforces 292D (并查集巧妙运用)
- Codeforces 920 E. Connected Components? (bfs)
- 【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components
- 【乱搞】Codeforces EDU37 Connected Components
- codeforces 920 E.Connected Components?
- F - Number of Connected Components----比较难的一个并查集
- Codeforces 475 B Strongly Connected City【DFS】
- Codeforces 292 div2 D
- Codeforces 698C. Fix a Tree (并查集)
- CodeForces - 156D:Clues(矩阵树定理&并查集)
- Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树
- Codeforces 776D The Door Problem 【并查集】
- CodeForces - 893C Rumor(并查集,DFS)
- Codeforces 651E Table Compression【并查集】
- Codeforces 300B Coach 【并查集】
- 【CODEFORCES】 B. Strongly Connected City
- 第十二章 ALDS1_11_D:Connected Components 连通分量
- LeetCode-Number of Connected Components in an Undirected Graph