csu 1601: War(并查集求每次去掉一条边后连通分量的个数)

1601: War

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 85   Solved: 25
[ Submit][ Status][ Web Board]

Description

AME decided to destroy CH’s country. In CH’ country, There are N villages, which are numbered from 1 to N. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. To defend the country from the attack of AME, CH has decided to build some roads between some villages. Let us say that two villages belong to the same garrison area if they are connected.
Now AME has already worked out the overall plan including which road and in which order would be attacked and destroyed. CH wants to know the number of garrison areas in his country after each of AME’s attack.

Input

The first line contains two integers N and M — the number of villages and roads, (2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines contains two different integers u, v (1<=u, v<=N)—which means there is a road between u and v. The next line contains an integer Q which denotes the quantity of roads AME wants to destroy (1 ≤ Q ≤ M). The last line contains a series of numbers each of which denoting a road as its order of appearance — different integers separated by spaces.

Output

Output Q integers — the number of garrison areas in CH’s country after each of AME's attack. Each pair of numbers are separated by a single space.

Sample Input

3 1
1 2
1
1
4 4
1 2
2 3
1 3
3 4
3
2 4 3

Sample Output

3
1 2 3

1.由于有10万个点,那么可以确定的是不能有嵌套的循环


代码:
#include<cstdio>
#include<cstring>
#define N 100005
using namespace std;

int father[N];//father[i]代表第i个点的父亲结点是father[i]
int vis[N];
int x[N],y[N];
int n,m;//代表有n个点,m条边
int cnt;//记录连通分量的个数
int ans[N];
int xx[N];

int Find(int a)//查找a结点的父亲是谁,带路径压缩
{
    int r=a;
    while(father[a]!=a)
    {
        a=father[a];
    }
    father[r]=a;
    return a;
}

void Merge(int a,int b)
{
    a=Find(a);
    b=Find(b);
    if(a!=b)
    {
        father[b]=a;
        cnt--;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=1;i<=n;i++)
            father[i]=i;
        cnt=n;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
        }
        memset(vis,0,sizeof(vis));
        int q;
        scanf("%d",&q);
        for(int i=1;i<=q;i++)
        {
            scanf("%d",&xx[i]);
            vis[xx[i]]=1;
        }
        for(int i=1;i<=m;i++)
        {
            if(vis[i]==0)
            {
                Merge(x[i],y[i]);
            }
        }
        for(int i=q;i>=1;i--)
        {
            ans[i]=cnt;
            Merge(x[xx[i]],y[xx[i]]);
        }
        for(int i=1;i<=q;i++)
        {
            if(i==q)
                printf("%d\n",ans[i]);
            else
                printf("%d ",ans[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值