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 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- 利用php的序列化和反序列化来做简单的数据本地存储
利用php的序列化和反序列化来做简单的数据本地存储 如下程序可以做为一个工具类 /** * 利用php的序列化和反序列化来做简单的数据本地存储 */ class objectdb { private ...
- 20145218 《Java程序设计》第10周学习总结
20145218 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发送到指定的位 ...
- word-break:break-all和word-wrap:break-word的区别
了解word-break属性 /* 关键字值 */ word-break: normal; word-break: break-all; word-break: keep-all; /* 全局值 */ ...
- robotframework笔记17
执行测试用例 基本用法 机器人框架从命令行执行测试用例,和 最终的结果是,在默认情况下,一个 输出文件 以XML格式和一个HTML 报告 和 日志 . 执行后,可以组合和输出文件 否则 进行后期处理 ...
- [maven] settings 文件节点配置详解
基本结构 <settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3. ...
- python 基础学习(元组,if,for)
1.tuple对象 tuple 元组 有序的列表 tuple一旦创建不能修改 a.定义元组t=('a','b','c')空元素的tuple t=()()既表示tuple 也表示运算符的优先级 所以定义 ...
- postgresql 视图
一.创建视图 create or replace view vw_users as select * from users; 二.通过定义规则来更新视图 create rule vw_users_up ...
- hdu----(2222)Keywords Search(ac自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Simple Data
Git地址:https://github.com/markrendle/Simple.Data 来源:http://bbs.nfinal.com/read-13
- FileUpload上传与下载
后台代码: public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass"; protecte ...