HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)
题目链接 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,并查集)的更多相关文章
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 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 ...
- 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 ...
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929
链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...
- 2016CCPC东北地区大学生程序设计竞赛 1005 HDU5926
链接http://acm.hdu.edu.cn/showproblem.php?pid=5926 题意:给我们一个矩阵,问你根据连连看的玩法可以消去其中的元素 解法:连连看怎么玩,就怎么写,别忘记边界 ...
随机推荐
- 用@property声明的NSString(或NSArray,NSDictionary)经常使用copy关键字,为什么?如果改用strong关键字,可能造成什么问题?
因为父类指针可以指向子类对象,使用 copy 的目的是为了让本对象的属性不受外界影响,使用 copy 无论给我传入是一个可变对象还是不可对象,我本身持有的就是一个不可变的副本. 如果我们使用是 str ...
- 玩转Node.js(三)
玩转Node.js(三) 上一节对于Nodejs的HTTP服务进行了较为详细的解析,而且也学会了将代码进行模块化,模块化以后每个功能都在单独的文件中,有利于代码的维护.接下来,我们要想想如何处理不同的 ...
- 自动化测试(三)如何用python写个双色球
写一个程序,输入N就产生N条双色球号码 红球 6 01-33 蓝球 1 01-16 产生的双色球号码不能重复,写到一个文件里面,每一行是一条 红球: 01 03 05 07 08 ...
- App自动化测试前期准备---android SDK配置
说明:就是配置android SDK 一.sdk下载 Windows(X64):立即下载 Linux(X64):立即下载 二.Windows配置 1.解压文件 直接解压到指定目录(演示目录:D:/) ...
- [18/12/3]蓝桥杯 练习系统 入门级别 Fibonacci数列求模问题 题解思路
前言略. 看到这个题目本来应该很高兴的,因为什么,因为太TM的基础了啊! 可是当你用常规方法尝试提交OJ时你会发现..hhh...运行超时..(开心地摇起了呆毛 //Fibonacci数列递归一般问题 ...
- Leetcode 668.乘法表中第k小的数
乘法表中第k小的数 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要返回表中第k 小的数字. 例 1: 输入 ...
- 软工实践 - 第十六次作业 Alpha 冲刺 (7/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10013959.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...
- android 使用LruCache缓存网络图片
加载图片,图片如果达到一定的上限,如果没有一种合理的机制对图片进行释放必然会引起程序的崩溃. 为了避免这种情况,我们可以使用Android中LruCache来缓存下载的图片,防止程序出现OOM. ...
- abp ef codefirst 设置默认值
public partial class tableIsWaringfiled : DbMigration { public override void Up() { //设置默认值为true Add ...
- ubuntu16.04 搭建nexus+maven 学习
/opt/nexus-2.10.0-02/bin vim nexus 关键配置: RUN_AS_USER=root JAVA_HOME=/usr/lib/java/jre export NEXUS_H ...