(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

目录

题意:传送门

 原题目描述在最下面。

 有一个n个节点m条边的无向图和一个m个节点的有根树(根为1)。树上每个节点和图中的某些边一一对应。

 每次询问给一个树的点的集合S,真实完整的点集合不仅包含集合里面的点,还包含这些点在树上的祖先。这个完整的点集合对应了图中一些边集合。输出这个图的边集合的联通块的个数。

思路:

  • 判断联通用并查集很方便咯。

  • 因为询问带有一点继承的味道。因为每个点的父节点也算在其中了。

  • 所以针对此题要用一个可继承的并查集:对树上每个节点维护一个图连通性的并查集。然后每个子节点\(v\)继承其父节点\(u\)的并查集,并在此基础上将新的一条边\(u->v\)添加进\(v\)节点的并查集。加边就是并查集的合并操作。

  • 对于每个询问,就把点集中每个点的并查集合并。意思就是把每个点的联通块合并在一起。最后数联通块的个数。

  • 代码中还有一点注释。

AC代码:

#include <bits/stdc++.h>
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
int n, m, q;
vector<int> son[10005];
int mp[10005][2], fa[10005][505], vis[505];
int Fi(int id,int x){
return fa[id][x]==x?x:fa[id][x]=Fi(id,fa[id][x]);
}
void un(int id,int a,int b){
int pa=Fi(id,a),pb=Fi(id,b);
if(pa==pb)return;
fa[id][pb]=pa;
}
void dfs(int u,int Fa){//每个节点继承其父节点的并查集
for(int i=1;i<=n;++i)fa[u][i]=fa[Fa][i];
un(u,mp[u][0],mp[u][1]);
int len=son[u].size();
for(int i=0;i<len;++i){
dfs(son[u][i],u);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
int tim,tc = 0;
scanf("%d", &tim);
while(tim--){
scanf("%d%d",&n,&m);
for(int i=0;i<=m;++i)son[i].clear();
for(int i=2,x;i<=m;++i){
scanf("%d",&x);
son[x].push_back(i);
}
for(int i=1,u,v;i<=m;++i){
scanf("%d%d",&u,&v);
mp[i][0]=u;mp[i][1]=v;
}
for(int i=0;i<=n;++i)fa[0][i]=i;
dfs(1,0);
printf("Case #%d:\n", ++tc);
scanf("%d",&q);
while(q--){
int cnt;scanf("%d",&cnt);
for(int i=1;i<=n;++i)fa[0][i]=i,vis[i]=0;
//将cnt个节点及其父节点的并查集合并
for(int i=0,x;i<cnt;++i){
scanf("%d",&x);
for(int j=1;j<=n;++j){
int tfa = Fi(x,j);
//这个处理是精髓
if(tfa!=j){//表示在此子图中这两个点联通,故合并
un(0,tfa,j);
}
}
}
int ans=0;
for(int i=1;i<=n;++i){
int tmp = Fi(0,i);
if(vis[tmp]==0){
vis[tmp]=1;
ans++;
}
}
printf("%d\n", ans);
}
}
return 0;
}

原题目描述:

Problem Description

There is a graph G=⟨VG,EG⟩ with |VG|=n and |EG|=m, and a magic tree T=⟨VT,ET⟩) rooted at 1, which contains m vertices.

Each vertex of the magic tree corresponds to an edge in the original graph G and each edge occurs in the magic tree exactly once.

Each query includes a set S(S⊆VT), and you should tell Mr. Frog the number of components in the modified graph G‘=(VG,E‘G), where E‘G is a set of edges in which every edge corresponds to a vertex v in magic tree T satisfying at least one of the following two conditions:

∙v∈S.

∙v is an ancestor of some vertices in S.

Note that the queries are independent, and namely one query will not influence another.

Input

The input contains several test cases and the first line of the input data is an integer T, denoting the number of test cases.

For each test case, the first line contains two integers n and m(1≤n≤500,1≤m≤10000), where n is the number of vertices and m is the number of edges.

The second line contains m - 1 integers describing the magic tree, i-th integer represents the parent of the (i + 1)-th vertex.

Then the following m lines describe the edges of the graph G. Each line contains two integers u and v indicating the two ends of the edge.

The next line contains only one integer q(1≤q≤50000), indicating the number of queries.

Then the following q lines represent queries, i-th line represents the i-th query, which contains an integer ki followed by ki integers representing the set Si.

It is guarenteed that ∑qi=1ki≤300000.

Output

For each case, print a line "Case #x:", where x is the case number (starting from 1).

For each query, output a single line containing only one integer representing the answer, namely the number of components.

Sample Input

1

5 4

1 1 3

1 2

2 3

3 4

4 5

3

1 2

2 2 3

2 2 4

Sample Output

Case #1:

3

2

1

Hint

magic tree and the original graph in the sample are:

In the first query, S = {2} and the modified graph G' = {{1, 2, 3, 4}, {(1, 2), (2, 3)}}, thus the number of the components in the modified graph is 3.

In the second query, S = {1, 2, 3}, where 1 is the ancestor of 2 (and 3) in the magic tree, and the modified graph G'' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3, 4)}},

therefore the number of the components in the modified graph is 2.

In the third query, S = {1, 2, 3, 4}, where 1 is the ancestor of 2 (and 4), 3 is the ancestor of 4, and the modified graph G' = {{1, 2, 3,4}, {(1, 2), (2, 3), (3,4), (4, 5)}},

therefore the answer equals to 1.

Source

2016CCPC东北地区大学生程序设计竞赛 - 重现赛

HDU5923-Prediction-有继承味道的并查集的更多相关文章

  1. hdu5923 Prediction

    jxt的思路 先膜一发 先处理 T这棵树上每个点到祖先这条链的点所生成的并查集 每个点的并查集都得分开来存 这个dfs做就好了 最后询问的时候 将k 个点的并查集合并就是这个询问的连通图 易得答案 # ...

  2. HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)

    题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题 题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...

  3. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  4. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  5. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  6. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  7. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  8. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  9. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

随机推荐

  1. spring boot 四大组件之Auto Configuration

    SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...

  2. 一维条码Code 128 类型的 A、B、C及Auto的区别

    CODE 128条码是1981年由COMPUTER IDENTICS公司开发的.对128个字符进行标准化的多级别编码.其条码的字符与其表示的数据呈1对1的关系,特点是通过起始字符.代码集字符.转换字符 ...

  3. spring事件监听(eventListener)

    原理:观察者模式 spring的事件监听有三个部分组成,事件(ApplicationEvent).监听器(ApplicationListener)和事件发布操作. 事件 事件类需要继承Applicat ...

  4. 搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

    http://blog.chinaunix.net/uid-20639775-id-154497.html

  5. PHP FILTER_VALIDATE_BOOLEAN 过滤器

    定义和用法 FILTER_VALIDATE_BOOLEAN 过滤器把值作为布尔选项来验证. Name: "boolean" ID-number: 258 可能的返回值: 如果是 & ...

  6. 【LeetCode 4】寻找两个有序数组的中位数

    题目链接 [题解] 假设在两个有序的序列中找第k小的数字. 那么我们先定位第一个序列中的第k/2个数字(不足则取最边上的那个数字)记下标为i1 然后定位第二个序列中的第k/2个数字(同样不足则取最边上 ...

  7. phpmyadmin利用的多种方式

    关于phpmyadmin的利用方式大佬们已经总结的很好了,这里只是造轮子(便于记录学习) 确认版本 渗透测试信息搜集永远是首位(也是最重要的一步).     默认目录/doc/html/index ...

  8. NX二次开发-使用MFC对话框不能用UF_UI_select等函数解决方法

    VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...

  9. jQuery选择器 (详解)

    1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的名称选择, $(" ...

  10. tp5使用jwt生成token,做api的用户认证

    首先 composer 安装  firebase/php-jwt github:https://github.com/firebase/php-jwt composer require firebas ...