4496 D-City【并查集-删边】

本文介绍了一个使用并查集算法解决城市道路被依次破坏后形成独立区域数量计算的问题。通过逆向思维,从完全隔离状态开始逐步连接各城市节点,统计每次新增连接后形成的独立区域数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

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.
 

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 5
Hint
The 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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值