题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题

题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问,

   每次选定树中的一个点集,然后真正被选上的是这些点以及这些点的所有祖先。

   只有标号在树中真正被选上的点代表的这些原图中的边是存在的,这样就构成了一个新的图。求这个图的连通块个数。

dfs整棵树,记$f[x]$为若$x$以及$x$的所有祖先被选上,那么构成的新的图的并查集)

这个实现比较简单,搜索的时候打上标记,回来的时候撤销即可。

这样预处理的时间复杂度是$O(nm)$的。

然后对于每个询问,把$k$个询问的并查集全部合并就可以了。

时间复杂度$O(nm + ∑kn)$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 523;
const int M = 1e4 + 10; int T;
int ca = 0;
int n, m, q;
int ans;
int a[M], b[M], c[N], f[M][N];
int father[N];
vector <int> g[M]; int getfather(int x){
return father[x] == x ? x : father[x] = getfather(father[x]);
} void dfs(int x, int fa){
rep(i, 1, n) father[i] = f[fa][i];
int fx = getfather(a[x]);
int fy = getfather(b[x]);
father[fx] = fy;
rep(i, 1, n) father[i] = getfather(i);
rep(i, 1, n) f[x][i] = father[i];
for (auto u : g[x]){
dfs(u, x);
}
} int main(){ scanf("%d", &T);
while (T--){
printf("Case #%d:\n", ++ca);
scanf("%d%d", &n, &m);
rep(i, 0, m + 1) g[i].clear(); rep(i, 2, m){
int x;
scanf("%d", &x);
g[x].push_back(i);
} memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
rep(i, 1, m){
scanf("%d%d", a + i, b + i);
} rep(i, 1, n) f[0][i] = i;
dfs(1, 0); scanf("%d", &q);
while (q--){
int y;
scanf("%d", &y);
rep(i, 1, n) father[i] = i;
rep(i, 1, y){
int x;
scanf("%d", &x);
memset(c, 0, sizeof c);
rep(j, 1, n){
int now = f[x][j];
if (c[now]){
int fx = getfather(c[now]);
int fy = getfather(j);
father[fx] = fy;
}
c[now] = j;
}
} ans = 0;
memset(c, 0, sizeof c);
rep(i, 1, n){
int x = getfather(i);
if (!c[x]) ++ans;
c[x] = 1;
}
printf("%d\n", ans);
}
} return 0;
}

  

HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)的更多相关文章

  1. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929

    链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...

  9. 2016CCPC东北地区大学生程序设计竞赛 1005 HDU5926

    链接http://acm.hdu.edu.cn/showproblem.php?pid=5926 题意:给我们一个矩阵,问你根据连连看的玩法可以消去其中的元素 解法:连连看怎么玩,就怎么写,别忘记边界 ...

随机推荐

  1. Pascal编写的蠕虫病毒,凌盟提供,Chaobs转载

    { Happy Birthday (c) 1998 WoRmI don't take responsibility for any damage caused by this virus.It was ...

  2. 20、AngularJs知识点总结 part-2

    1.作用域 当你在angularJs中创建控制器时,可以将$scope对象作为一个参数进行传递: scope 是一个 JavaScript 对象,带有属性和方法,这些属性和方法可以在视图和控制器中使用 ...

  3. Oracle 学习----游标(使用带参光标cursor)

    --表参照无参光标页信息 --注意:红色就是参数 declare cursor tt(pkeycode temp.keycode%type) is select name,sal from temp ...

  4. elk-filebeat收集docker容器日志

    目录 使用docker搭建elk filebeat安装与配置 docker容器设置 参考文章 首发地址 使用docker搭建elk 1.使用docker-compose文件构建elk.文件如下: ve ...

  5. 孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库

    孤荷凌寒自学python第五十六天通过compass客户端和mongodb shell 命令来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第二 ...

  6. 团队Alpha版本冲刺(六)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  7. Nginx 战斗准备 —— 优化指南

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  8. Java获取当前服务器IP实现

    package hope.ipaddress.demo; import java.net.InetAddress; import java.net.NetworkInterface; import j ...

  9. JAVA判断一个字符串里面有没有汉字

    private static boolean checkIfExistChineseCharacter(String s) { return !(s.length() == s.getBytes(). ...

  10. BZOJ1821 [JSOI2010]Group 部落划分 Group 【最小生成树】

    题目 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成为谜团了--聪聪根 ...