HDU 5420 Victor and Proposition
Victor and Proposition
This problem will be judged on HDU. Original ID: 5420
64-bit integer IO format: %I64d Java class name: Main
At the very beginning, Victor has a proposition, then this proposition procudes many propositions. Then every proposition procudes more propositions...... Finally there are n propositions. These propositions can be regarded as a tree whose root is 1.
We assume that the first proposition, whose number is 1, belongs to the 0-th generation, and those propositions produced by the x-th generation belong to the x+1-th generation. We also assume that all of the propositions in the x-th generation are in level x. Specially, Victor has discovered that the proposition whose number is i can infer the proposition whose number is xi and all of the propositions in xi's subtree, whose levels are not greater than xi's level + di.
Notice : a is b's father does not show that either a can infer b or b can infer a.
Now please determine the number of such ordered pairs (i,j), that 1≤i<j≤n, the proposition i can infer the proposition j, and the proposition j can also infer the proposition i.
Input
The first line of the input contains an integer T, denoting the number of test cases.
In every test case, there is an integer n in the first line, denoting the number of the propositions.
The second line contains n−1 integers, the i-th integer fi+1(fi<i) denotes that the proposition i+1 is produced by the proposition fi+1.
Then there are n lines, the i-th line contains two integers xi and di.
1≤T≤5.
2≤n≤100000.
0≤di<n.
Output
Your program should print T lines : the i-th of these should contain a single integer, denoting the number of such ordered pairs (i,j).
Sample Input
1
4
1 2 1
2 1
1 0
4 0
2 0
Sample Output
6
Source
BestCoder Round #52 (div.2)
解题:线段树优化建图,妙哉,内存开得好凶残,吓呆本宝宝了
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[(+)**];
int head[maxn],L[maxn],R[maxn],tot,clk;
int dep[maxn],hs[maxn],st[maxn],n;
vector<pii>order[maxn];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void init() {
tot = ;
memset(head,-,sizeof head);
}
void dfs(int u,int depth) {
hs[L[u] = ++clk] = u;
dep[u] = depth;
for(int i = head[u]; ~i; i = e[i].next) dfs(e[i].to,depth + );
R[u] = clk;
}
void build(int L,int R,int v) {
order[v].resize(R - L + );
if(L == R) {
st[v] = ++n;
order[v][] = pii(dep[hs[L]],hs[L]);
add(n,hs[L]);
return;
}
int mid = (L + R)>>;
build(L,mid,v<<);
build(mid + ,R,v<<|);
st[v] = n + ;
merge(order[v<<].begin(),order[v<<].end(),order[v<<|].begin(),order[v<<|].end(),order[v].begin());
for(int i = ; i <= R - L; ++i)
add(n + i + , n + i);
for(int i = ; i <= R - L; ++i)
add(n + i + ,order[v][i].second);
n += R - L + ;
}
void connect(int L,int R,int lt,int rt,int u,int d,int v) {
if(lt <= L && rt >= R) {
int pos = lower_bound(order[v].begin(),order[v].end(),pii(d,INF)) - order[v].begin() - ;
if(~pos) add(u,st[v] + pos);
return;
}
int mid = (L + R)>>;
if(lt <= mid) connect(L,mid,lt,rt,u,d,v<<);
if(rt > mid) connect(mid + ,R,lt,rt,u,d,v<<|);
}
int dfn[maxn],low[maxn],cnt[maxn],scc,ct;
bool instack[maxn];
stack<int>stk;
void tarjan(int u) {
dfn[u] = low[u] = ++ct;
instack[u] = true;
stk.push(u);
for(int i = head[u]; ~i; i = e[i].next) {
if(!dfn[e[i].to]) {
tarjan(e[i].to);
low[u] = min(low[u],low[e[i].to]);
} else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
}
if(low[u] == dfn[u]) {
int v;
cnt[++scc] = ;
do {
instack[v = stk.top()] = false;
stk.pop();
cnt[scc] += (v <= clk);
} while(v != u);
}
}
int main() {
int kase,u,v;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
init();
clk = scc = ct = ;
memset(dfn,,sizeof dfn);
memset(instack,false,sizeof instack);
for(int i = ; i <= n; ++i) {
scanf("%d",&u);
add(u,i);
}
dfs(,);
init();
build(,clk,);
for(int i = ; i <= clk; ++i) {
scanf("%d%d",&u,&v);
connect(,clk,L[u],R[u],i,dep[u] + v,);
}
for(int i = ; i <= n; ++i)
if(!dfn[i]) tarjan(i);
LL ret = ;
for(int i = ; i <= scc; ++i)
ret += (LL)cnt[i]*(cnt[i]-)/;
printf("%I64d\n",ret);
}
return ;
}
HDU 5420 Victor and Proposition的更多相关文章
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- HDU 5417 Victor and Machine
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5417 Problem Description Victor has a machine. When t ...
- HDU 5418 Victor and World 允许多次经过的TSP
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...
- HDU 5418 Victor and World(状压DP+Floyed预处理)
Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Other ...
- HDU 5421 Victor and String
Victor and String Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on HDU. Orig ...
- HDU - 5419 Victor and Toys(组合计数)
http://acm.hdu.edu.cn/showproblem.php?pid=5419 题意 n个物品,标号1-n,物品i有权值wi.现在有m个区间[l,r],从中任意选三个区间i,j,k,求物 ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- HDU 5419——Victor and Toys——————【线段树|差分前缀和】
Victor and Toys Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others ...
- HDU 5418——Victor and World——————【状态压缩+floyd】
Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Other ...
随机推荐
- [POI2007]对称轴osi
Description FGD小朋友--一个闻名遐迩的年轻数学家--有一个小MM,yours.FGD小朋友非常喜欢他的MM,所以他很乐意帮助他的MM做数学作业.但是,就像所有科学的容器一样,FGD的大 ...
- 异或+构造 HDOJ 5416 CRB and Tree
题目传送门 题意:给一棵树,问f (u, v) 意思是u到v的所有路径的边权值的异或和,问f (u, v) == s 的u,v有几对 异或+构造:首先计算f (1, u) 的值,那么f (u, v) ...
- node入门(二)——gulpfile.js初探
本文关于gulpfile.js怎么写,利于完成个性化需求.本文开发环境默认已安装node,详情参考<node入门(一)——安装>. 一.安装gulp npm install -g gulp ...
- input 全选 jquery封装方法
HTML代码 <table class="table table-striped"> <thead> <tr> <th><in ...
- Node.js——Buffer
介绍 JavaScript没有读取和操作二进制数据流的机制,但是 node.js 引入了Buffer 类型,可以操作TCP流或者文件流 使用Buffer可以用来对临时数据(二进制数据)进行存储,当我们 ...
- file.seek()
语法:fileObject.seek(offset,whence) offset -- 开始的偏移量,也就是代表需要移动偏移的字节数 whence:可选,默认值为 0.给offset参数一个定义,表示 ...
- Eureka 整理
服务治理:(该模块也可以使用集群) 该模块主要负责完成微服务架构中的服务治理功能. 1.构建服务注册中心. 每个服务单元需要向注册中心登记自己提供的服务. 2.服务注册与服务发现. 服务之间的调用不再 ...
- SQLSTATE=2300
在powerdesigner将表的结构运用于数据库的时候报的错. 目标: 在Hibernate中使用多表级联的插入操作. 解决办法: 将navicat中的mysql数据库表删除, 手动创建 原因: p ...
- springboot Tomcat connector configured to listen on port 8081 failed to start.
启动报 Tomcat connector configured to listen on port 8081 failed to start. The port may already be in ...
- Android全局异常捕获
PS:本文摘抄自<Android高级进阶>,仅供学习使用 Java API提供了一个全局异常捕获处理器,Android引用在Java层捕获Crash依赖的就是Thread.Uncaught ...