URAL 1671 Anansi's Cobweb (并查集)
题意:给一个无向图。每次查询破坏一条边,每次输出查询后连通图的个数。
思路:并查集。逆向思维,删边变成加边。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#define inf -100000000
#define LL long long
#define maxn 100005
using namespace std;
struct Edge
{
int x,y;
};
int parent[maxn];
int findset(int p)
{
return (parent[p]==p)?p:(parent[p]=findset(parent[p]));
}
Edge edge[maxn];
int query[maxn];
bool build[maxn];
int ans[maxn];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(build,,sizeof(build));
; i<=m; ++i)
scanf("%d%d",&edge[i].x,&edge[i].y);
int q;
scanf("%d",&q);
; i<=q; ++i)
{
scanf("%d",&query[i]);
build[query[i]]=true;
}
; i<=n; ++i)
parent[i]=i;
;
; i<=m; ++i)
if(!build[i])
{
int fa=findset(edge[i].x),fb=findset(edge[i].y);
if(fa!=fb)
parent[fa]=fb;
}
; i<=n; ++i)
if(findset(i)==i) cnt++;
; --i)
{
ans[i]=cnt;
int fa=findset(edge[query[i]].x),fb=findset(edge[query[i]].y);
if(fa!=fb)
{
parent[fa]=fb;
cnt--;
}
}
; i<=q; ++i)
) printf("%d",ans[i]);
else printf(" %d",ans[i]);
printf("\n");
}
;
}
URAL 1671 Anansi's Cobweb (并查集)的更多相关文章
- ural 1671 Anansi's Cobweb
这道题是并差集的简单应用 #include <cstdio> #include <cstring> #include <algorithm> #define max ...
- 1671. Anansi's Cobweb(并查集)
1671 并查集 对于询问删除边之后的连通块 可以倒着加边 最后再倒序输出 #include <iostream> #include<cstdio> #include<c ...
- URAL 1320 Graph Decomposition(并查集)
1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...
- URAL 1682 Crazy Professor (并查集)
[题目链接] http://acm.timus.ru/problem.aspx?space=1&num=1682 [题目大意] 给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和 ...
- URAL(timus)1709 Penguin-Avia(并查集)
Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other An ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- ural1671 Anansi's Cobweb
Anansi's Cobweb Time limit: 1.0 secondMemory limit: 64 MB Usatiy-Polosatiy XIII decided to destroy A ...
- 51nod 1204 Parity(并查集应用)
1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- iOS响应式编程:ReactiveCocoa vs RxSwift 选谁好
转载: iOS响应式编程:ReactiveCocoa vs RxSwift 选谁好 内容来自stack overflow的一个回答:ReactiveCocoa vs RxSwift – pros an ...
- robotframework笔记18
测试执行 如何创建测试套件结构解析 执行测试数据,如何继续执行一个测试用例失败后, 以及如何优雅地停止整个测试执行. 执行流 执行测试套件和 总是在一个测试套件执行测试用例. 一个测试套件 创建从一个 ...
- [maven] 跳过单元测试
1.mvn命令 $ mvn install -Dmaven.test.skip=true 2.通过配置pom <project> [...] <build> <plugi ...
- (28)odoo中css可用颜色对照表
颜色 颜色英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 ...
- 189. Rotate Array -- 将数组前一半移到后一半
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- 移动前端开发的viewport总结整理
1.通俗讲移动设备上的viewport就是设备的屏幕上能用来显示我们的网页的那块区域,但不是浏览器可视区域.一般来讲,移动设备上的viewport都要大于浏览器的可视区域.移动设备上的浏览器会把默认的 ...
- Python环境的安装
参考官方文档 http://www.runoob.com/python/python-install.html Python已经被移植在许多平台上(经过改动使它能够工作在不同平台上). 您需要下载适用 ...
- Hadoop 添加删除数据节点(datanode)
前提条件: 添加机器安装jdk等,最好把环境都搞成一样,示例可做相应改动 实现目的: 在hadoop集群中添加一个新增数据节点. 1. 创建目录和用户 mkdir -p /app/hadoop gr ...
- Play framework logging设置
play的logger是基于Log4j,Play 2.0 uses logback as its logging engine. 一.配置 1. 在conf/application.conf中设置lo ...
- Axis2 webservice入门--开发环境搭建,概念理解
关于webservice的概念,网上有各种解释,但是不太好懂. 可以这样理解:1.一个webservice就是一个“功能”,只是这个功能是别人写好的,被放在别人的网站上. ...