目录

  • 目录
  • 思路:

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

目录

题意:传送门

 原题目描述在最下面。
 有一个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_JUDGEfreopen("E://ADpan//in.in", "r", stdin);//freopen("E://ADpan//out.out", "w", stdout);
#endifint 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东北地区大学生程序设计竞赛 - 重现赛

转载于:https://www.cnblogs.com/Cwolf9/p/9433902.html

HDU5923-Prediction-有继承味道的并查集相关推荐

  1. [NC16591]关押罪犯 并查集

    题解:很明显的并查集,但因为它们带有权值,所以我们先要把他排序,我们要尽可能让危害大的罪犯在两个监狱里(这里有一点贪心的味道). 1.首先我们把它门按照之间的影响值从大到小排序. 2.假设a与b是敌人 ...

  2. c++自带的可持久化平衡树?rope大法好!(超详细解答 + 5道例题讲解,可直接替代可持久化的线段树、并查集、平衡树!)

    整理的算法模板合集: ACM模板 目录 c++自带的可持久化平衡树?rope大法好! 1. 声明 2. 支持操作 char类型的rope int类型的rope 3. 具体的细节 4. "可持 ...

  3. sdut 2129树结构练习——判断给定森林中有多少棵树(并查集)

    树结构练习--判断给定森林中有多少棵树 Time Limit: 1000MS Memory limit: 65536K 题目描述 众人皆知,在编程领域中,C++是一门非常重要的语言,不仅仅因为其强大的 ...

  4. POJ 并查集 题目汇总 ——czyuan原创(转)

    继续数据结构的复习,本次的专题是:并查集. 并查集,顾名思义,干的就是"并"和"查"两件事.很多与集合相关的操作都可以用并查集高效的解决. 两个操作代码:    ...

  5. 并查集(Union-Find)

    并查集(Union-Find) 并查集(Union-Find) 1.初始化 2.查询 3.合并 4.平衡性优化,扁平化 5.按秩合并 6.路径压缩 7.代码 常用模板 [★ 547. 省份数量](ht ...

  6. Gym - 101194G Pandaria (并查集+倍增+线段树合并)

    题意: 给定一个无向图.每个点有一种颜色.现在给定q个询问,每次询问x和w,求所有能通过边权值不超过w的边走到x的点的集合中,哪一种颜色的点出现的次数最多.次数相同时输出编号最小的那个颜色.强制在线. ...

  7. 并查集c++代码_[Leetcode 每日精选](本周主题-并查集) 547. 朋友圈

    题目难度: 中等 原题链接 今天继续来做并查集的问题, 这道题仍然比较基础, 而且也是个比较接近现实的问题了. 大家在我的公众号"每日精选算法题"中的聊天框中回复 并查集 就能看到 ...

  8. HDU1811 Rank of Tetris 拓扑排序+并查集 OR 差分约束最短路+并查集

    题目链接 题意:就是给你一堆关系,看能不能排出个确定的顺序 做法: 1. 拓扑排序+并查集 应该很容易想到的一种思路,大于小于建立单向边.对于相等的呢,就把他们缩成一个点.就用并查集缩成一个点就行了 ...

  9. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法

    tanjar算法离线求LCA的思想主要是利用并查集的思想. 求距离的话就是d[start[i]]+end[en[i]]-2*d[lca[i]]; 首先从根节点dfs,在深度遍历的回溯的过程中不断的更新 ...

最新文章

  1. 控制台打印汉字的方法
  2. mysql case设固定值_MySQL CASE语句将自定义值放置为NULL
  3. linux下安装mysql笔记
  4. 从微服务到 Serverless | 开源只是开始,终态远没有到来
  5. insert返回主键 — mybatis selectKey
  6. 音视频技术开发周刊 | 198
  7. pandas用均值填充nan_python – 如何用pandas中的滚动平均值填充nan值
  8. 【剑指offer】面试题14- I:剪绳子(Java)
  9. 三家快递公司涨派费:9月1日起每票上调0.1元
  10. scrapy爬虫架构介绍和初试
  11. 大数据之_数据采集Flume_Flume了解_学习内容介绍---Flume工作笔记002
  12. Html时间自定义控件
  13. 为了物尽其用报废的涉密计算机的硬盘,检测不到硬盘不能轻易将其定为报废
  14. Dreamweaver之简单实现网站布局、图片漂浮、区域跳转、登陆注册及图片查看器
  15. 使用uvm_report_catcher屏蔽掉特定的uvm_error/uvm_warning
  16. 最新2018.1.1深度学习平台搭建 Win10+GPU+Tensorflow+keras+CUDA --2018.1.1
  17. bzoj1127 [POI2008]KUP
  18. HyperLynx(二十九)高速串行总线仿真(一)
  19. OpenCV 外接矩形框 cv2.boundingRect、cv2.minAreaRect
  20. 【Unity3d】在Unity3d中使用百度AI人脸识别功能

热门文章

  1. 嵌入式C开发中用到的位域结构体
  2. 特征工程之特征缩放特征编码
  3. 计算机考试单招考试面试,单招计算机面试题.docx
  4. 设计一个按优先数调度算法实现处理器调度的程序_计算机中的程序都是怎么运行的,来深入了解一下吧...
  5. MyBatis:事务回滚
  6. (转载)最黑的黑客米特尼克:多次耍FBI 终被高手擒
  7. SharePoint【学习笔记】-- SharePoint 2010 技术参数整理
  8. 什么是Ext(ExtJs)【转载】
  9. 在Linux上构建ASP.NET环境-asp.net关注
  10. html中内联元素和块级元素的区别(整理版)