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 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- js encodeURI方法认识
很早就知道js中encodeURI方法,也很早就用过,但是每次看到它总感觉有些陌生,因为不知道到底是什么原理,和普通的编码到底什么关系, 今天在查看w3c api时又遇到了她,正好有空就多看了几眼,突 ...
- Mybatis学习(壹)
一.Mybatis的引言 1.Mybatis框架概念:是数据库持久层的框架,对数据库的访问和操作.Mybatis对JDBC的封装,Mybatis替换JDBC开发,解决DAO中的通用问题. 2.JDBC ...
- css3开门
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- MySQL学习笔记_2_MySQL创建数据表(上)
MySQL创建数据表(上) 一.创建数据表的SQL语句模型[弱类型] CREATETABLE [IF NOT EXISTS] 表名称( 字段名1列的类型[属性][索引], 字段名2 列的类型[属性][ ...
- java中的异常和处理
算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:NegativeAr ...
- 父类中“this” 指向问题
“this.字段”如果出现在父类代码中,指的就是父类属性. “this.方法”不管出现在父类还是子类代码中,指的都是子类方法. “this.字段”如果出现在子类代码中,指的就是子类属性. 在程序的时候 ...
- Mybatis update In
mysql语句如下: ,,) mybatis的mapper如下: int updateStateByIDs(@Param("ids") String[] ids, @Param(& ...
- OUT函数及其熟练使用,split的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- BZOJ1737 [Usaco2005 jan]Naptime 午睡时间
断环然后裸DP就好了... $f[i][j][k]$表示1号时间段没有被算入答案,到了第$i$个时间段,一共选了$j$个时间段,$k = 0 /1$表示第i个时间段有没有被算进答案的最优值 $g[i] ...
- js控制html文字提示语的出现和隐藏
有时我们需要在点击html输入框的时候,旁边会出现提示语.在输入字符的时候,输入框下边会出现输入了多少字符的提示. 请看下面实例. <!DOCTYPE html> <html> ...