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  fa[100000+100];
int del[100000+100];
int ans[100000+100];
int n,m,q;
struct Edge
{
    int f,to;
};
Edge edge[100000+100];
int fin(int a)
{
    return fa[a]==a?a:fa[a]=fin(fa[a]);
}
int kru(int x,int y)
{
    int fx=fin(x);
    int fy=fin(y);
    if(fx!=fy)
    {
       fa[fx]=fy;
       return 1;
    }
    return 0;
}
int main()
{

    int i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<=n;i++) fa[i]=i;
        memset(vis,0,sizeof(vis));

        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&edge[i].f,&edge[i].to);
        }
        scanf("%d",&q);
        for(i=0;i<q;i++)
        {
            scanf("%d",&del[i]);
            vis[del[i]]=1;
        }
        int cnt=0;
        int tmp=n;
        for(i=1;i<=m;i++)
        {
            if(vis[i]==0)
            {
                if(kru(edge[i].f,edge[i].to)==1) tmp--;
            }
        }
        ans[cnt++]=tmp;
        for(i=q-1;i>0;i--)
        {
            j=del[i];
            if(kru(edge[j].f,edge[j].to)==1) tmp--;
            ans[cnt++]=tmp;
        }
        for(i=cnt-1;i>=0;i--)
        {
            printf("%d",ans[i]);
            if(i!=0) printf(" ");
            else printf("\n");
        }
    }
    return 0;
}

  

时间: 2024-10-12 10:54:34

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

HDU 3371 Connect the Cities(并查集+Kruskal)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先联通了几个点,所以为了判断连通性 很容易想到用并查集+kruskal. 不过要注意 这题有一个坑点,就是边数很多 上限是25000,排序的话可能就超时了.而点数则比较少 上限是500,所以很多人选择用Prim做.但我个人觉得这样连通性不好判断.其实边数多没关系,我们主要去重就好啦,用邻接矩阵存下两点

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

HDU 1863 并查集+Kruskal

点击打开链接 题意:不解释了 思路:也不解释了,并查集判段连通性,Kruskal求最小生成树,大水题(/ □ \) #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long lo

POJ 1797 Heavy Transportation(二分+并查集/kruskal)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 24398   Accepted: 6472 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

UVA10158 - War(并查集)

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

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

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

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

[HDU3710] Battle Over Cities [树链剖分+线段树+并查集+kruskal+思维]

题面 一句话题意: 给定一张 N 个点, M 条边的无向连通图, 每条边上有边权 w . 求删去任意一个点后的最小生成树的边权之和. 思路 首先肯定要$kruskal$一下 考虑$MST$里面去掉一个点,得到一堆联通块,我们要做的就是用原图中剩下的边把这些联通块穿起来 考虑这个点$u$在$MST$上的位置,可以知道有两种边:一种是从$u$的任意一个儿子的子树连到$u$的子树外面的,一种是在$u$的两个儿子的子树之间连接的 第一种情况: 考虑边$(u,v)$,没有进入$MST$中,那么若它是某个节