Kattis - boxes (LCA)
Boxes
There are NN boxes, indexed by a number from 11 to NN. Each box may (or not may not) be put into other boxes. These boxes together form a tree structure (or a forest structure, to be precise).
You have to answer a series of queries of the following form: given a list of indices of the boxes, find the total number of boxes that the list of boxes actually contain.
Consider, for example, the following five boxes.
Figure 1: Sample input
If the query is the list “1”, then the correct answer is “5”, because box 1 contains all boxes.
If the query is the list “4 5”, then the correct answer is “2”, for boxes 4 and 5 contain themselves and nothing else.
If the query is the list “3 4”, then the correct answer is “2”.
If the query is the list “2 3 4”, then the correct answer is “4”, since box 2 also contains box 5.
If the query is the list “2”, then the correct answer is “3”, because box 2 contains itself and two other boxes.
Input
The first line contains the integer NN (1≤N≤2000001≤N≤200000), the number of boxes.
The second line contains NNintegers. The iith integer is either the index of the box which contains the iith box, or zero if the iith box is not contained in any other box.
The third line contains an integer QQ (1≤Q≤1000001≤Q≤100000), the number of queries. The following QQ lines will have the following format: on each line, the first integer MM(1≤M≤201≤M≤20) is the length of the list of boxes in this query, then MM integers follow, representing the indices of the boxes.
Output
For each query, output a line which contains an integer representing the total number of boxes
Sample Input 1
5
0 1 1 2 2
5
1 1
2 4 5
2 3 4
3 2 3 4
1 2
Sample Output 1
5
2
2
4
3
题意是给你n个盒子,它们之间互相嵌套,给你m个询问,每次问你cnt个盒子中一共有几个盒子。
显然盒子的嵌套关系可以转换成树的父子关系。假设询问中有1 2 号盒子,而且1号盒子包含了2号盒子,这时2号盒子就被cover掉了,我们统计时只统计没有被cover的盒子就行了。
这就转换成了给你cnt个点,去掉其中含有所有父子关系中的儿子们,剩下的点输出他们的孩子一共有多少就可以了。
#include <bits/stdc++.h> using namespace std;
const int maxn =+;
vector <int> son[maxn];//存儿子
int fa[maxn][];//fa[i][j]表示 节点i向上走2^j个节点会到达哪个节点
int p[maxn];//模拟的队列
int sum[maxn];//每个节点的子树(包括自己)的节点数目
int dep[maxn];//每个节点深度
int inp[];//询问的点们
bool iip[];//是否被覆盖
int n,m;
void bfs(int x)
{
dep[x]=;
int w,r;//w r 分别是队首队尾的下标
w=;
p[]=x;//模拟的队首是x
sum[x]=;
r=;//队尾下标是1
while(w<=r)//队首下标小于等于队尾下标,即队列非空
{
int u=p[w];w++;//去队首,pop队首
for(unsigned int i=;i<son[u].size();i++)//遍历当前节点的每一个儿子
{
int v=son[u][i];
if(dep[v])continue;//如果当前节点的深度不是0,就是被访问过,continue
dep[v]=dep[u]+;//当前节点深度等于父节点深度+1
sum[v]=;//当前节点的子树节点数为1
p[++r]=v;//将当前节点push到队列中去
}
}
/* 由于我们是按照数组保存的队列,我们从最大的下标(此时这些节点的深度也是最大的一批)
开始向上回溯,让他们的父节点的sum加上他们的sum
*/
while(r>)
{
sum[fa[p[r]][]]+=sum[p[r]];
r--;
}
}
int findd(int x,int y)
{
if (x!=y&&dep[x]==dep[y]) return false;
if (x==y) return true;
if (dep[x]<dep[y]){int temp=x;x=y;y=temp;}//保证dep[x]>=dep[y]
while (dep[x]>dep[y]){//从x往上跳,跳的步数为1,2,4,8...
int j=;
while (dep[fa[x][j]]>dep[y]) ++j;
if (dep[fa[x][j]]==dep[y]){x=fa[x][j];break;}//如果跳到了同一深度,更新下x
x=fa[x][--j];//让x跳到深度<=y而且深度最大的节点
}
if (x==y)
return y;//如果此时x==y,则y是x的父亲,x,y的LCA显然是y
while (x!=y){
int j=;
while (fa[x][j]!=fa[y][j]) ++j;//x,y一起往上跳,直至两个跳到同一个点
if (j==)//此时x,y是亲兄弟
break;
--j;
x=fa[x][j],y=fa[y][j];
}
return fa[x][];
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d",&n)){
for (int i=;i<=n;++i){
scanf("%d",&fa[i][]);
son[fa[i][]].push_back(i);
}
for (int j=,k=;k<=n;++j,k*=){
for (int i=;i<=n;++i)
fa[i][j]=fa[fa[i][j-]][j-];
/* i向上走2^j个点等于 i向上走2^(j-1)个点后再向上走2^(j-1)个点 */
}
memset(sum,,sizeof sum);
memset(dep,,sizeof dep);
bfs();//广搜
/*printf("sum ");
for (int i=0;i<=n;++i)
printf("%d ",sum[i]);
printf("\n");*/
scanf("%d",&m);
while (m--){
int tot=;
int cnt;
memset(inp,,sizeof inp);
memset(iip,false,sizeof iip);
scanf("%d",&cnt);
for (int i=;i<=cnt;++i)
scanf("%d",&inp[i]);
for (int i=;i<=cnt-;++i){
if (iip[i]) continue;
for (int j=i+;j<=cnt;++j){
if (iip[j]) continue;
if (inp[i]==inp[j]){iip[j]=true;continue;}
int lca=findd(inp[i],inp[j]);
//printf("%d %d lca is %d\n",i,j,lca);
if (lca==) continue;
if (lca==inp[i]){iip[j]=true;continue;}
if (lca==inp[j]){iip[i]=true;continue;}
}
}
/*for (int i=1;i<=cnt;++i)
printf("!%d ",iip[i]);
printf("\n");*/
for (int i=;i<=cnt;++i)
if (!iip[i]){
//printf("cnt %d\n",inp[i]);
tot+=sum[inp[i]];
} printf("%d\n",tot);
}
}
return ;
}
Kattis - boxes (LCA)的更多相关文章
- 洛谷P3379 【模板】最近公共祖先(LCA)
P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交 讨论 题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...
- 图论--最近公共祖先问题(LCA)模板
最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...
- hihoCoder 1233 : Boxes(盒子)
hihoCoder #1233 : Boxes(盒子) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 There is a strange ...
- 面试题6:二叉树最近公共节点(LCA)《leetcode236》
Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...
- P3379 【模板】最近公共祖先(LCA)
P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...
- 洛谷P3379 【模板】最近公共祖先(LCA)(dfs序+倍增)
P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...
- 「LuoguP3379」 【模板】最近公共祖先(LCA)
题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...
- 洛谷——P3379 【模板】最近公共祖先(LCA)
P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...
- luogo p3379 【模板】最近公共祖先(LCA)
[模板]最近公共祖先(LCA) 题意 给一个树,然后多次询问(a,b)的LCA 模板(主要参考一些大佬的模板) #include<bits/stdc++.h> //自己的2点:树的邻接链表 ...
随机推荐
- Dataphin的代码自动化能力如何助力商业决策
前言 随着大数据趋势的迅速增长,数据的重要性与日俱增,企业内看数据.用数据的诉求越来越强烈,其中最常见的就是各种经营报表数据:老板每日早晨9点准时需要看到企业核心的经营数据,以便进行企业战略及方向决策 ...
- 【Linux】清理缓存buffer/cache
运行sync将dirty的内容写回硬盘 sync 通过修改proc系统的drop_caches清理free的cache echo 3 > /proc/sys/vm/drop_caches ech ...
- qt编程参考资料
https://qtguide.ustclug.org/
- selenium2-java 浏览器不同窗口切换
1,获取浏览器的单个窗口: String parentWindowid = driver.getWindowHandle(); System.out.println( ...
- jQuery:unbind方法的使用详解
一.概述: unbind方法只能解绑用jQuery的bind方法以及用jQuery方法注册的事件处理程序.比如:$(‘a’).click(function(){})可以通过unbind解绑.用原生ad ...
- kubernetes原理
kubernetes 核心组件 etcd:保存整个集群的状态 apiserver:提供资源操作的唯一入口,并提供认证.授权.访问控制.api注册和发现等机制 controller manager:负责 ...
- B. pSort
题目链接: http://codeforces.com/problemset/problem/28/B 题意: 给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可 ...
- BootStrap自定义轮播图播放速度
$('.carousel').carousel({ interval: 3000 });
- 基于名称快速定位文件和文件夹的搜索工具Everything和dll依赖查询工具Dependency Walker
在工作中有时需要定位头文件.lib库文件.dll文件等的路径,自己去一个个盘符查找实在太麻烦,最近发现使用Everything这款工具很方便,下载地址为:下载 Everything 1.4.1.935 ...
- golang的数据类型之字符串类型
基本案例: [root@node3 shangxuetang]# cat string.go package main import "fmt" func main() { //s ...