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 ...
随机推荐
- Python学习打算
背景:本人用python写自动化脚本,基础的东西都会啦.但是呢,鉴于年轻应该好好学习,所以打算再买一本python书籍用来精读. Python 好的博客: Python快速教程(好多,一点也不快速): ...
- Windows 10 TH2
Windows 10 TH2到底更新了啥? 15年11月,微软正式向Windows 10用户推送了Threshold 2(简称TH2)更新, 也就是传说中的November Update.更新后系统版 ...
- 只有20行Javascript代码!手把手教你写一个页面模板引擎
http://www.toobug.net/article/how_to_design_front_end_template_engine.html http://barretlee.com/webs ...
- POJ2230 Watchcow【欧拉回路】
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6172Accepted: 2663 Special Judge ...
- [ES6] Object.assign (with defaults value object)
function spinner(target, options = {}){ let defaults = { message: "Please wait", spinningS ...
- 我的Android进阶之旅------>Android 设置默认语言、默认时区
1. 设置默认时区 PRODUCT_PROPERTY_OVERRIDES += \ persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist.sys.timez ...
- 多线程、Service与IntentService的比较
资料摘自网络(侵删) Service Thread IntentService AsyncTask When to use ? Task with no UI, but shouldn't b ...
- 帧动画 AnimationDrawable
Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果. 首先,在res/drawable中定义动画 < ...
- asp.net微信开发第一篇----开发者接入
在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图 public void ProcessRequest(HttpContext context) { context.R ...
- MSSQL查询所有数据库表,指定数据库的字段、索引
--查询所有数据库USE mastergoselect [name] from [sysdatabases] order by [name] --查询其中一个数据库test,就可以得到这个数据库中的所 ...