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. 安装windows10和fedora23双系统的一些注意事项

    在安装双系统windows10和fedora的过程中遇到了很多的问题,博主也是在慢慢的摸索中最后莫名其妙的成功的安装双系统. 当然,幸亏博主机智的记住了中间的一些细节,所以大致上的有一些注意事项希望能 ...

  2. 【CF1210D】Konrad and Company Evaluation(vector,图论)

    题意:有i个人,m对两两之间的关系,第i个人初始的薪水为i,有q次操作,第i次操作会把v[i]号的薪水提升成n+i 如果两个人之间存在关系,薪水高的会向薪水低的炫耀 定义u,v,w为一个三元组,当u向 ...

  3. iPad如何恢复

    iPad在有网络连接的情况下,可以通过iCloud进行恢复. 没有连接WiFi的情况下,只能通过USB连接才能恢复. ①下载最新版本的iTunes:https://support.apple.com/ ...

  4. HDU4762 Cut the Cake

    HDU4762 Cut the Cake 思路:公式:n/m(n-1) //package acm; import java.awt.Container; import java.awt.geom.A ...

  5. 用 Flask 来写个轻博客 (30) — 使用 Flask-Admin 增强文章管理功能

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 实现文章管理功能 实现效果 前文列表 用 Flask 来写个 ...

  6. 第一步:卸载手机上的“WIFI万能钥匙”! 会分享自己家的wifi

    http://www.znds.com/tv-164866-1-1.html 楼主今日家中WIFI卡的不行,心想,20M的电信光纤没理由这么卡!于是就在网上查找了许多资料,接下来统一收集整理给大家!第 ...

  7. python多种推导式的实现

  8. (转)SQL注入原理

    原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html SQL Injection:就是通过把SQL命令插入到Web表单递交或 ...

  9. 1.如何在JMeter中使用JUnit

    您是否需要在测试过程中使用JUnit? 要回答这个问题,我们先来看看单元测试. 单元测试是软件测试生命周期中测试的最低分辨率. 运行单元测试时,需要在应用程序中使用最小的可测试功能,将其与其他代码隔离 ...

  10. JavaFx开发桌面软件

    JavaFx开发桌面软件 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} Jav ...