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;
}

时间: 2024-08-08 22:43:39

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

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

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 the

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

Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great. These two had some problems about the numbers they like, so they decided to divide the great kingdom between themselves. The

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 sum - 1 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const

任意点~并查集求联通块

链接:https://www.nowcoder.com/acm/contest/84/C来源:牛客网 任意点 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可以改变方向. 请问至少需要加多少个点,使得点对之间互相可以到达. 输入描述: 第一行一个整数n表示点数( 1 <= n <= 100).第二行n行,

C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is col

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

CSU 1601 War

1601: War Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 127  Solved: 36[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