ural 1671 Anansi's Cobweb
这道题是并差集的简单应用
#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的更多相关文章
- URAL 1671 Anansi's Cobweb (并查集)
题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> ...
- 1671. Anansi's Cobweb(并查集)
1671 并查集 对于询问删除边之后的连通块 可以倒着加边 最后再倒序输出 #include <iostream> #include<cstdio> #include<c ...
- ural1671 Anansi's Cobweb
Anansi's Cobweb Time limit: 1.0 secondMemory limit: 64 MB Usatiy-Polosatiy XIII decided to destroy A ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
- 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 ...
随机推荐
- Confluence简介
前言 我们有很多的知识共享的工具,比如我们每天都用的qq共享,网盘共享,但是都不如我们这个来的方便,直接.这个是直接通过网页的形式进行共享,类似于我们的博客共享,但是它更加的具有指向性,站点管 ...
- rpm与dpkg yum与apt-get详解
由于自由软体的蓬勃发展,加上大型Unix-Like 主机的强大效能,让很多软体开发者将他们的软体使用Tarball 来释出. 后来Linux 发展起来后,由一些企业或社群将这些软体收集起来制作成为di ...
- J2EE开发中常用的缓存策略
一.什么是缓存1.Cache是高速缓冲存储器 一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问2.凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之 ...
- 第二个参数(那个 properties)确定你将如何使用这个特性值
CBCharacteristicPropertyBroadcast: 允许一个广播特性值,用于描述特性配置,不允许本地特性 CBCharacteristicPropertyRead: 允许读一个特性值 ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [转] java中注解的使用与实例
注解目前非常的流行,很多主流框架都支持注解,而且自己编写代码的时候也会尽量的去用注解,一时方便,而是代码更加简洁. 注解的语法比较简单,除了@符号的使用之外,它基本与Java固有语法一致.Java S ...
- NYOJ-129 并查集
这个题基本上是并查集稍微一变, 只是加了一些判断条件而已,就是将点合并成树, 最后遍历一下, 统计一下有多少棵树, 如果不是1的话, 肯定不是树,所以,可以根据这个来判断 #include <s ...
- java07循环结构
public class WhileTest { // while循环结构 public static void main(String[] args) { System.out.println(&q ...
- tips [终端]
pbcopy 命令:Place standard output in the clipboard. $ pbcopy < ~/.ssh/id_rsa.pub
- android获取在res文件下的图片资源
//得到该图片的id(name 是该图片的名字,"drawable" 是该图片存放的目录,getPackageName()是应用程序的包) int resID = getResou ...