C: City

时间限制: 1 s      内存限制: 128 MB     
 

题目描述

如果城市A和城市B互通,城市B和城市C互通,那么城市A和城市C也互通,A、B、C三个城市算一个聚集点。先已知有n个城市和m条道路,想求的是有几个聚集点?但小S觉得太简单了,由于战争原因,某些城市会被导弹销毁掉,与之相应的道路也变得不可用。之前已经被销毁的不会被复原。现给定每次销毁的城市顺序,求每次销毁后聚集点有多少个。

输入

第一行输入n

和m,表示城市数量和道路数量(1≤n≤104,1≤m≤2n)

接下来m

行,每行输入两个数ai和bi (1≤ai,bi≤n)

。表示ai和bi直接有道路

第m+2

行输入q,表示有q个城市会被销毁 (1≤q≤n)

接下来输入q

个数,每行输入一个不重复的数,表示被销毁的城市

输出

输出一行q个数,每i个数表示第i个城市销毁后聚集点的数量

样例输入

8 9
1 2
1 3
1 6
2 4
3 6
4 5
4 7
5 7
5 8
4
3 2 5 4

样例输出

1 2 3 3
 
#include<iostream>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
int a[], b[], c[], p[], ans[], vis[], r[];
int n, m, t = , cnt;
void init()//初始化集合,每个元素的根节点都是自己
{
for (int i = ; i <= n; i++)
{
p[i] = i;
}
} int find(int x)//查找元素x的根节点是谁
{
if (x == p[x])
return x;
else
return p[x] = find(p[x]);
} void join(int x, int y)//合并两个集合
{
int xRoot = find(x);
int yRoot = find(y); if (xRoot == yRoot) //根节点相同,不合并
return;
if (r[xRoot] < r[yRoot]) //r[i]是元素i所在树的高度,矮树的根节点认高树的根节点做根节点
p[xRoot] = yRoot;
else if (r[xRoot] > r[yRoot])
p[yRoot] = xRoot;
else
{
p[yRoot] = xRoot;//树高相同,做根节点的树高度要加一
r[xRoot]++;
}
}
void num()//求不同子集个数
{
for (int i = ; i <= n; i++)//有多少个p[i]==i,就有多少个子集
{
if (p[i] == i && vis[i] == )
t++;
}
}
bool sameRoot(int x, int y)//查询两个元素的老板是否相同
{
return find(x) == find(y);
}
int main()
{
scanf("%d%d", &n, &m); init();
cnt = n;//当元素各不相连的时候,有n个集合
for (int i = ; i <= m; i++)
{
scanf("%d%d", &a[i], &b[i]);
}
int k;
scanf("%d", &k);
for (int i = ; i <= k; i++)
{
scanf("%d", &c[i]);
vis[c[i]] = ;
} for (int i = ; i <= m; i++)
{
if (vis[a[i]] == && vis[b[i]] == )
join(a[i], b[i]);
}
num();
int base = t;//销毁所有c[i]元素之后可以构成几个子集
cnt = base;
ans[k + ] = base;
for (int i = k; i >= ; i--)
{
vis[c[i]] = ;
cnt = cnt + ;
ans[i] = cnt;
for (int j = ; j <= m; j++)
{
if (a[j] == c[i] && vis[b[j]] == )
{
if (sameRoot(a[j], b[j]))
ans[i] = cnt;
else
{
join(a[j], b[j]);
{
cnt = cnt - ;
ans[i] = cnt;
}
}
}
if (b[j] == c[i] && vis[a[j]] == )
{
if (sameRoot(a[j], b[j]))
ans[i] = cnt; else
{
join(a[j], b[j]);
{
cnt = cnt - ;
ans[i] = cnt;
}
}
} }
}
for (int i = ; i <= k + ; i++)
{
printf("%d", ans[i]);
if (i != k + )
printf(" ");
}
printf("\n");
//system("pause");
return ;
}
相似题目:https://www.cnblogs.com/-citywall123/p/10719924.html

C: City----逆向并查集的更多相关文章

  1. HDU - 4496 City 逆向并查集

    思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...

  2. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

  3. zoj 3261 逆向并查集+离线处理

    题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...

  4. HDU_4496_逆向并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 逆向并查集,先读取,然后从后向前join每次保存答案即可. #include<iostream> ...

  5. 逆向并查集 hrbust 1913

    #include<iostream> //由于拆除并查集的方法太难或者没有#include<cstdio> //可以先将所有没有拆的桥连接 再逆向操作 断开变成连接 反向输出# ...

  6. ZOJ - 3261 逆向并查集

    思路:很巧妙的解法.如果按照常规一边读入,一边合并并查集,删边实在没办法做. 首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查 ...

  7. Connections in Galaxy War (逆向并查集)题解

    Connections in Galaxy War In order to strengthen the defense ability, many stars in galaxy allied to ...

  8. BZOJ 1016 星球大战starwar(逆向-并查集)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1015 题意:给出一个图.每次删掉一个点,求删掉之后连通块个数. 思路:正着做不好做,我们 ...

  9. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

  10. ZOJ3261:Connections in Galaxy War(逆向并查集)

    Connections in Galaxy War Time Limit: 3 Seconds      Memory Limit: 32768 KB 题目链接:http://acm.zju.edu. ...

随机推荐

  1. pcl知识

    1.pcl/io/pcd_io.h pcl/io/ply_io.h pcl::PLYReader reader; pcl::PCDWriter pcdwriter; 读取ply pcd 2.voidl ...

  2. Anaconda 安装和配置

    Anaconda 安装和配置 1. Anaconda 安装 Anaconda说明及安装过程:Anaconda详细安装使用教程 2. Anaconda和Pip源修改 Anaconda源修改:打开Anac ...

  3. HBase表的memstore与集群memstore

    一直有一个问题,今天调查了一下源码算是明白了. ===问题=== 通过java api(如下代码所示)在创建表的时候,可以通过setMemStoreFlushSize函数来指定memstore的大小, ...

  4. understand的安装

    1.win7 64位下安装 1)下载Understand.4.0.908.x64.rar. 2)解压之,直接运行里面的Understand-4.0.908-Windows-64bit.exe. 3)选 ...

  5. windows cmd命令相关知识和经验的碎片化记录

    1.循环遍历当前文件夹下的所有*.dll文件,并打印其绝对路径和相对路径 ``` for /f "tokens=*" %%a in ('dir /s/b/a-d "*.d ...

  6. vue相关操作

    一: vue的安装 -安装node.js -vue脚手架 -vue create 项目名字 二:vue create 项目名字 用pycharm打开vue项目 -需要安装vue.js插件-settin ...

  7. SpringMVC源码解读 - RequestMapping注解实现解读 - ConsumesRequestCondition

    consumes  指定处理请求的提交内容类型(media-Type),例如application/json, text/html. 所以这边的ConsumesRequestCondition就是通过 ...

  8. C++ 动态分配二维和三维数组

    目的:熟悉c++动态内存分配 描述:使用c++程序定义动态数组类,使用new和delete操作符实现动态二维数组和三维数组的定义 //main.cpp //主程序类 #include <iost ...

  9. Android-bindService远程服务(Aidl)-初步

    之前上一篇讲解到本地服务,本地服务只能在自身APP中Activity访问Service,调用Service里面到方法等操作 如果想A应用访问B应用里面的方法,属于跨进程调用,如果Android不特供这 ...

  10. [HTTP]Nonocast.http post方法

    Nonocast.Http is a free, open source developer focused web service via http for small and medium sof ...