Victor and Proposition

Time Limit: 6000ms
Memory Limit: 524288KB

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的更多相关文章

  1. ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

    HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & ...

  2. HDU 5417 Victor and Machine

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5417 Problem Description Victor has a machine. When t ...

  3. HDU 5418 Victor and World 允许多次经过的TSP

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...

  4. HDU 5418 Victor and World(状压DP+Floyed预处理)

    Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Other ...

  5. HDU 5421 Victor and String

    Victor and String Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on HDU. Orig ...

  6. 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,求物 ...

  7. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

  8. HDU 5419——Victor and Toys——————【线段树|差分前缀和】

    Victor and Toys Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others ...

  9. HDU 5418——Victor and World——————【状态压缩+floyd】

    Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Other ...

随机推荐

  1. LoadRunner12学习之路(6-8)

    六.创建负载测试场景 如何启动控制器? 要开始开发场景,请打开LoadRunner Controller. 打开HPE LoadRunner Controller. 在LoadRunner机器上,单击 ...

  2. 牛客小白月赛5-I-区间(差分求前缀和+一次暴力统计)

    题目描述 Apojacsleam喜欢数组. 他现在有一个n个元素的数组a,而他要对a[L]-a[R]进行M次操作: 操作一:将a[L]-a[R]内的元素都加上P 操作二:将a[L]-a[R]内的元素都 ...

  3. 转】upstart封装mongodb应用为系统服务

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! upstart封装mongodb应用为系统服务 ...

  4. 调用wsdl接口,参数是xml格式

    1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...

  5. 输入域名网站访问不了,ping与ftp都正常,这情况有可能域名被墙

    被墙的风险 1.首先域名没有备案,而且服务器是国外的服务器, 2.域名解析到国外服务器 总结:以上两点有很大几率被墙的风险 被墙的解决方案: 1.换新域名并备案(不换新域名走第二步,域名一定要备案) ...

  6. 重构26-Remove Double Negative(去掉双重否定)

    尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的.这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致bug.如下例所示: public ...

  7. H.264学习笔记1——相关概念

    此处记录学习AVC过程中的一些基本概念,不定时更新. frame:帧,相当于一幅图像,包含一个亮度矩阵和两个色度矩阵. field:场,一帧图像,通过隔行扫描得到奇偶两场,分别称为顶场和底场或奇场和偶 ...

  8. ajax 请求spring之post

    # 背景 现在使用spring boot开发一个web应用是非常普遍的了,ajax请求更是标配:那么你在ajax请求时,是否遇到过在controller中获取不到参数的情况呢?特别是post请求: # ...

  9. 6-Java-C(小题答案)

    1.15 2.36 3.0.58198 4.return v.size()-v.indexOf(n) 5."%"+(width-s.length()-2)/2+"s%s% ...

  10. 了解Java密码扩展的基础

      了解Java密码扩展的基础     Java密码扩展(The Java Cryptography Extension),是JDK1.4的一个重要部分,基本上,他是由一些包构成的,这些包形成了一个框 ...