Kattis - boxes (dfn序)
Boxes
There are N
boxes, indexed by a number from 1 to N
. 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.

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 N
(1≤N≤200000
), the number of boxes.
The second line contains N
integers. The ith integer is either the index of the box which contains the ith box, or zero if the i
th box is not contained in any other box.
The third line contains an integer Q
(1≤Q≤100000), the number of queries. The following Q lines will have the following format: on each line, the first integer M (1≤M≤20) is the length of the list of boxes in this query, then M
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 | Sample Output 1 |
---|---|
5 |
5 |
【分析】给出N个盒子,其中一些盒子可能会被放进另一些盒子里,Q个询问,每次询问给出M个盒子序号,求这M个盒子包含的盒子总数。
很容易想到这题就是建树,然后求子树的size。关键是如何合并这些点,即判断父子关系。有两种方法,一种是LCA,另一种是维护DFN序,然后区间合并。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e5+;
const int M = 1e6+;
int n,m,k,tot=,tim=,ans;
int head[N],vis[N],in[N];
int bg[N],ed[N],sz[N];
vector<int>root;
struct man{
int to,next;
}edg[N*];
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
in[v]++;
}
void dfs(int u,int fa){
bg[u]=++tim;
sz[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=fa){
dfs(v,u);
sz[u]+=sz[v];
}
}
ed[u]=tim;
}
int ispre(int u,int v){
if(u==v)return ;
else {
if(bg[u]<=bg[v]&&ed[u]>=ed[v])return ;
else if(bg[u]>=bg[v]&&ed[u]<=ed[v])return -;
}
return ;
}
int main() {
met(head,-);
int u,v;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&v);
if(v)add(v,i);
}
for(int i=;i<=n;i++)if(in[i]==)root.pb(i);
for(int i=;i<root.size();i++){
int rt=root[i];
dfs(rt,);
}
scanf("%d",&m);
vector<int>vec;
while(m--){
scanf("%d",&k);
vec.clear();ans=;
scanf("%d",&u);
vec.pb(u);
for(int i=;i<k;i++){
scanf("%d",&v);
bool f=false;
for(int j=;j<vec.size();j++){
u=vec[j];
if(ispre(u,v)==){
f=true;
break;
}
else if(ispre(u,v)==-){
vec.erase(vec.begin()+j);
j--;
}
}
if(!f)vec.pb(v);
}
for(int i=;i<vec.size();i++){
u=vec[i];
//printf("u : %d sz[u] : %d\n",u,sz[u]);
ans+=sz[u];
}
printf("%d\n",ans);
}
return ;
}
Kattis - boxes (dfn序)的更多相关文章
- 洛谷P4689 [Ynoi2016]这是我自己的发明(莫队,树的dfn序,map,容斥原理)
洛谷题目传送门 具体思路看别的题解吧.这里只提两个可能对常数和代码长度有优化的处理方法. I 把一个询问拆成\(9\)个甚至\(16\)个莫队询问实在是有点珂怕. 发现询问的一边要么是一个区间,要么是 ...
- 线段树分治总结(线段树分治,线段树,并查集,树的dfn序,二分图染色)
闲话 stO猫锟学长,满脑子神仙DS 网上有不少Dalao把线段树分治也归入CDQ分治? 还是听听YCB巨佬的介绍: 狭义:只计算左边对右边的贡献. 广义:只计算外部对内部的贡献. 看来可以理解为广义 ...
- 洛谷P3703 [SDOI2017]树点涂色(LCT,dfn序,线段树,倍增LCA)
洛谷题目传送门 闲话 这是所有LCT题目中的一个异类. 之所以认为是LCT题目,是因为本题思路的瓶颈就在于如何去维护同颜色的点的集合. 只不过做着做着,感觉后来的思路(dfn序,线段树,LCA)似乎要 ...
- 树的遍历顺序 - dfs序|欧拉序|dfn序(备忘)
(仅作备忘) dfs序是dfs过程中对于某节点进入这个节点的子树和离开子树的顺序 满足每个节点都会在dfs序上出现恰好两次 任意子树的dfs序都是连续的 欧拉序是dfs过程中经过节点的顺序 每个节点至 ...
- bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论
题目大意 1.将x到当前根路径上的所有点染成一种新的颜色: 2.将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根: 3.查询以x为根的子树中所有点权值的平均值. 分析 原题codec ...
- bzoj 2819 Nim dfn序+树状数组维护区间异或值
题目大意 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略 ...
- 51nod 1576 Tree and permutation(树的重心+dfn序)
乍一看我不会. 先不考虑加点. 先考虑没有那个除\(2\). 考虑每一条边的贡献,假设某一条边把这棵树分成大小为x,y的两个部分. 那么这条边最多可以被使用\(min(x,y)*2\)次(因为有进有出 ...
- BZOJ2819 Nim 【dfn序 + lca + 博弈论】
题目 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略的. ...
- 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 in ...
随机推荐
- 周记【距gdoi:117天】
国庆被“吞”了 图论还剩下平面图.分层图.欧拉图…… 是现实太残酷还是自己兴趣不够? 努力吧.
- POJ 开关问题 解题报告
开关问题 Time Limit: 1000MS Memory Limit: 30000K Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他 ...
- phaser常用API总结
1. 游戏画布的尺寸 var width = game.width, height = game.height; 2. 中心点坐标 var game = new Phaser.Game(...); ...
- Math.abs为Integer.Min_VALUE返回错误的值
Math.abs为Integer.Min_VALUE返回错误的值 这段代码: System.out.println(Math.abs(Integer.MIN_VALUE)); 回报-2147483 ...
- 编译 openssl 0.9.8zc 出现 error C2220: warning treated as error - no 'object' file generated
remove the /WX option from CFLAG
- python最简单发送邮件
#!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...
- 解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'
mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...
- kthread_create与kernel_thread的区别【栈】
转自:http://blog.chinaunix.net/uid-25513153-id-2888903.html kthread_create与kernel_thread的区别 kernel thr ...
- Shell 脚本实现TCP/UDP协议通讯
Shell 脚本实现TCP/UDP协议通讯 http://www.cnblogs.com/occult/archive/2012/12/25/2832183.html
- [ Openstack ] Openstack-Mitaka 高可用之 Rabbitmq-server 集群部署
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...