码迷,mamicode.com
首页 > 其他好文 > 详细

1601: War(并查集)

时间:2015-05-03 20:38:31      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:csu   并查集   

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

HINT

Source


题意:给出n个点,m条边,然后Q个询问,进行删边,问每次删边之后,有几个连通块

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double PI = acos(-1.0);
#define Len 200005
#define mod 19999997
const int INF = 0x3f3f3f3f;
#define exp 1e-8
const int N = 100005;

int fath[N],a[N],b[N],k[N],vist[N],ans;

int findroot(int x)
{
    if(x!=fath[x])
        fath[x]=findroot(fath[x]);
    return fath[x];
}
void setroot(int x,int y)
{
    x=findroot(x);
    y=findroot(y);
    if(x!=y)
    {
        fath[x]=y;
        ans--;
    }
}
int main()
{
    int q,n,m;
    while(scanf("%d%d",&n,&m)>0)
    {
        for(int i=1; i<=n; i++)
            fath[i]=i;
        ans=n;
        for(int i=1; i<=m; i++)
            scanf("%d%d",&a[i],&b[i]),vist[i]=0;
        scanf("%d",&q);
        for(int i=1; i<=q; i++)
        {
            scanf("%d",&k[i]);
            vist[k[i]]=1;
        }
        for(int i=1; i<=m; i++)
            if(vist[i]==0)
                setroot(a[i],b[i]);
        for(int i=q; i>0; i--)
        {
            int tt=k[i];
            k[i]=ans;
            setroot(a[tt],b[tt]);
        }
        for(int i=1; i<=q; i++)
        {
            printf("%d",k[i]);
            if(i!=q)
                printf(" ");
        }
        printf("\n");
    }
}


1601: War(并查集)

标签:csu   并查集   

原文地址:http://blog.csdn.net/libin56842/article/details/45460903

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!