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 NN­integers. 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)的更多相关文章

  1. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  2. 图论--最近公共祖先问题(LCA)模板

    最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...

  3. hihoCoder 1233 : Boxes(盒子)

    hihoCoder #1233 : Boxes(盒子) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 There is a strange ...

  4. 面试题6:二叉树最近公共节点(LCA)《leetcode236》

    Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...

  5. P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  6. 洛谷P3379 【模板】最近公共祖先(LCA)(dfs序+倍增)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  7. 「LuoguP3379」 【模板】最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  8. 洛谷——P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  9. luogo p3379 【模板】最近公共祖先(LCA)

    [模板]最近公共祖先(LCA) 题意 给一个树,然后多次询问(a,b)的LCA 模板(主要参考一些大佬的模板) #include<bits/stdc++.h> //自己的2点:树的邻接链表 ...

随机推荐

  1. node连接mysql数据库

    1. 创建项目,安装mysql 创建项目文件夹test, 在test文件夹下yarn add mysql --save安装mysql: 2. node使用mysql 在test文件夹下,创建test. ...

  2. flutte页面布局四

    AspectRatio 组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度是由 ...

  3. html 和 body标签的 css 设置

    个人猜测浏览器的机制:H5页面底板上有一张画布,画布高度可以被撑高.html.body等元素是固定在画布上的.浏览器中页面的滚动是跟着画布滚动的.(fixed定位是脱离这种机制的,相对浏览器窗口定位的 ...

  4. 2018-2019-2 20175223 实验四 《Android开发基础》实验报告

    目录 北京电子科技学院(BESTI)实验报告 实验名称:实验四 Android程序设计 实验内容.步骤与体会: 一.实验四 Android程序设计-1 二.实验四 Android程序设计-2 三.实验 ...

  5. dubbo漫谈二

    转:腾信视频 阿甘 https://ke.qq.com/course/216518 https://blog.csdn.net/u013142781/article/details/50396621 ...

  6. Codeforces 510C (拓扑排序)

    原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds mem ...

  7. 彻底搞定C指针-函数名与函数指针

     函数名与函数指针 一 通常的函数调用 一个通常的函数调用的例子://自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int mai ...

  8. 查看IOS-app证书到期时间

    参照: iOS企业版证书到期 https://www.jianshu.com/p/44b0dc46ef37 如果不能十分确定每一个打出来的ipa的有效期(过期时间),而又需要关注它具体什么时候需要强制 ...

  9. 20150721—HTML的定位 JS (转)

    本文转载于:http://blog.csdn.net/xuantian868/article/details/3116442   HTML:scrollLeft,scrollWidth,clientW ...

  10. 面试题:Nginx负载均衡的算法怎么实现的?为什么要做动静分离?

    面试题 Nginx负载均衡的算法怎么实现的?Nginx 有哪些负载均衡策略?Nginx为什么要做动静分离? 面试官心理剖析 主要是看应聘人员对Nginx的基本原理是否熟悉,需要应聘人员能够根据实际业务 ...