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. php strtolower()函数 语法

    php strtolower()函数 语法 作用:把所有字符转换为小写.大理石量具 语法:strtolower(string) 参数: 参数 描述 string 必须,规定要转换的字符串 说明:str ...

  2. delphi 使用 InputBox、InputQuery 的启发

    使用 InputBox.InputQuery 的启发 看了 InputBox.InputQuery 函数实现的源码, 有些收获与心得... ------------------------------ ...

  3. C++ string.replace的使用

    //下面是一个检查一个字符串中是否有'.'的函数,该函数将找到的'.'转化为'_'. inline void checkName(string& name) { std::; while (s ...

  4. RHEL/CentOS通用性能优化、安全配置参考

    RHEL/CentOS通用性能优化.安全配置参考 本文的配置参数是笔者在实际生产环境中反复实践总结的结果,完全适用绝大多数通用的高负载.安全性要求的网络服务器环境.故可以放心使用. 若有异议,欢迎联系 ...

  5. js中Array方法重写(二):myForEach;myEvery;mySome;myFilter;myReduce

    一.myForEach //myForeach 数组每个元素都执行一次回调函数 Array.prototype.myForEach = function(callback){ for(var i = ...

  6. python中的Nonetype

    在python中的None的类型是Nonetype, 嗯,看清楚了吧,None是值,Nonetype是类型.同理,数字1是值,int是类型.注意:在python中是没有Null的,取而代之的是None

  7. JSP 获取访问者真正的IP地址

    request.getRemoteAddr(),这种方法在大部分情况下都是有效的,但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了, 如果使用了反向代理软件,用re ...

  8. Docker 进入容器的4种方法

    在使用Docker创建了容器之后,大家比较关心的就是如何进入该容器了,其实进入Docker容器有好几多种方式,这里我们就讲一下常用的几种进入Docker容器的方法. 进入Docker容器比较常见的几种 ...

  9. css 样式小窍门

    css 样式小窍门 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  10. 在Emacs中使用plantuml画UML图

    在Emacs中使用plantuml画UML图 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #83 ...