这道题是并差集的简单应用

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std; int x[maxn],y[maxn],flag[maxn],f[maxn],t[maxn];
bool vis[maxn]; inline int find(int x)
{
if(x==f[x]) return x;
return f[x]=find(f[x]);
}
int main()
{
int n,m,q;
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
scanf("%d%d",&x[i],&y[i]);
}
scanf("%d",&q);
memset(vis,false,sizeof(vis));
for(int i=; i<=q; i++)
{
scanf("%d",&flag[i]);
vis[flag[i]]=true;
}
for(int i=; i<=n; i++)
f[i]=i;
for(int i=; i<=m; i++)
{
if(!vis[i])
{
int a=find(x[i]);
int b=find(y[i]);
if(a!=b)
f[a]=b;
}
}
int ans=;
for(int i=; i<=n; i++)
{
if(f[i]==i) ans++;
}
for(int i=q; i>; i--)
{
int a=find(x[flag[i]]);
int b=find(y[flag[i]]);
t[i]=ans;
if(a!=b)
{
f[a]=b;
ans--;
}
}
for(int i=; i<=q; i++)
{
if(i==q) printf("%d\n",t[i]);
else printf("%d ",t[i]);
}
return ;
}

ural 1671 Anansi's Cobweb的更多相关文章

  1. URAL 1671 Anansi's Cobweb (并查集)

    题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> ...

  2. 1671. Anansi's Cobweb(并查集)

    1671 并查集 对于询问删除边之后的连通块 可以倒着加边 最后再倒序输出 #include <iostream> #include<cstdio> #include<c ...

  3. ural1671 Anansi's Cobweb

    Anansi's Cobweb Time limit: 1.0 secondMemory limit: 64 MB Usatiy-Polosatiy XIII decided to destroy A ...

  4. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  5. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  6. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  7. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  8. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  9. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

随机推荐

  1. adb Logcat用法

    转自: http://blog.csdn.net/tiantianshangcha/article/details/6288537 个人认为有一下几个常用命令: adb logcat -b radio ...

  2. L - 还是畅通工程 - hdu 1233

    Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达 ...

  3. 安装redis,含安装步骤和安装中出现的详细错误分析

    1.wget http://download.redis.io/releases/redis-2.8.13.tar.gz 2.解压文件 tar -zxvf redis-2.8.13.tar.gz 3. ...

  4. SlidingMenu导入编译用法--Eclipse和IDEA

    非常多側滑的应用都用的是开源库SlidingMenu, 效果不错,下面是我用上的效果图,因为近期换成了IDEA(IntelliJ)编辑器,昨天上网找了全部的教程都是关于在Eclipse导入的方法,摸索 ...

  5. MySQL-视频跟随练习笔记

    在表中添加某列 alter table tbl_user add email varchar(50); 在表中删除某列 alter table tbl_user drop email; 重命名某列 a ...

  6. SpringMVC08转发和重定向

    public class User { private String name; private Integer age; public String getName() { return name; ...

  7. SpringMVC05使用注解的方式

    <body> <a href="add">新增</a> <a href="update">修改</a> ...

  8. NuGet学习笔记(1)——初识NuGet及快速安装使用(转)

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  9. Linux基础知识之 系统启动流程

    [1]Linux启动的几个主要阶段 启动流程示意图

  10. MVC项目,系统找不到指定的文件。(异常来自 HRESULT:0x80070002)

    今天在用Visual Studio新建MVC项目的时候,遇到错误 系统找不到指定的文件.(异常来自 HRESULT:0x80070002) 解决办法:工具--> 扩展和更新 -->联机(V ...