D-City
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2598 Accepted Submission(s): 906
Problem Description
Luxer is a really bad guy. He destroys everything he met.
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
Input
First line of the input contains two integers N and M.
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.
Constraints:
0 < N <= 10000
0 < M <= 100000
0 <= u, v < N.
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.
Constraints:
0 < N <= 10000
0 < M <= 100000
0 <= u, v < N.
Output
Output M lines, the ith line is the answer after deleting the first i edges in the input.
Sample Input
5 10 0 1 1 2 1 3 1 4 0 2 2 3 0 4 0 3 3 4 2 4
Sample Output
1 1 1 2 2 2 2 3 4 5HintThe graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there's only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.
题意:
n 个城市由多条路(m)相连成一个整体,现在按次序破坏掉所有的路,让你分别输出破坏掉每条路之后,一共分割成了几个独立的区域(相互之间不能到达)
题解:
看到的时候想到用并查集,写着写着发现不对了,删除边要怎么删除?没见过...然后上网查资料,额....原来反向思考就可以了....
就做啊做,辛辛苦苦ac了....也是醉了,错了好几次,都不清楚错在了哪,还是换个做法才过的...........
其实不必要像我写这么复杂,只要能判断是否加入新元素就行(查找判断),严格按并查集的思路写,只是为了思路清晰。.
做法:
需要先输入保存,然后倒着过来加边,每加一条边,就记录一下当前的区域个数,最后输出。
#include<stdio.h>
int per[100005],n,m;
int a[100005],b[100005];
void init()//初始化
{
for(int i=0;i<n;++i)
{
per[i]=i;
}
}
int find(int x)//查找
{
int r=x;
while(r!=per[r])
{
r=per[r];
}
int i=x,j;
while(i!=r)
{
j=per[i];per[i]=r;i=j;
}
return r;
}
int join(int x,int y)//合并
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
return 1;
}
return 0;
}
void slove()
{
init();
int x[100005],cnt=n,i;
for(i=m-1;i>=0;--i)
{
x[i]=cnt;//统计区域数目
if(join(a[i],b[i]))//只有合并之后
{
--cnt;//不连通的区域才会少了一块
}
}
for(i=0;i<m;++i)//输出记录的数量
{
printf("%d\n",x[i]);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<m;++i)
{
scanf("%d%d",a+i,b+i);//输入
}
slove();
}
return 0;
}