1601: War(并查集)

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");
    }
}
时间: 2024-10-12 19:36:54

1601: War(并查集)的相关文章

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

csu 1601 1601: War (并查集 kruskal)

题意:有n个村子 由m条路联通 其中q条路会依次被摧毁 问每次摧毁后会有多少片村庄被孤立 思路:首先算出q条路都被摧毁后被孤立的村庄数 然后再逆序把每条路修复上 每修复一条孤立的村庄就减少一片 最后再输出每次记录的结果 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[100000+100]; int f

ZOJ 3261 Connections in Galaxy War (并查集)

Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began

CSU1601: War(并查集)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 82  Solved: 24 [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

UVA10158 - War(并查集)

题目链接 题目大意:有n个国家,每个国家有友国和敌国,并且遵守着朋友的朋友是朋友,敌人的敌人是朋友的原则,给你四种操作,题目中有描述,然后让你根据操作进行,对应输出相应的结果. 解题思路:题目里面有敌对关系和朋友关系,朋友的关系可以使用并查集,但是敌对的关系是不行的.转化敌对的关系.假设0-n - 1,代表国家.n-2 * n - 1 代表0-n - 1这些国家的敌国.例如 1 和 4是敌对关系,因为敌人的敌人是朋友,那么1 + n 和 4 与 1 和4 + n就是朋友的关系,将敌对的关系转换成

UVA 10158 War 并查集

D - War(8.4.3) Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: War A war is being lead between two countries, A and B. As a loyal citizen of C, you dec

ZOJ3261 Connections in Galaxy War 并查集

分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; typedef pair<int,int>pii; const int N=1e4+5; pii p[N<<1]; int v[N],fa[N],n,m,q; bo

CSU 1601 War (并查集)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 202  Solved: 58 [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 ar

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [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 ar