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 ...
随机推荐
- [UOJ386]鸽子固定器
题解 堆+贪心 题意就是给你\(n\)个物品,让你最多选\(m\)个 每个物品有两个属性\(a_i,b_i\) 最大化\((\sum_{a_i})^{dv}+(max(b_i)-min(b_i))^{ ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- python general
everything in python is object assignment is binding a name to an object one object can have several ...
- Snort里如何将读取的包记录存到二进制tcpdump文件下(图文详解)
不多说,直接上干货! 如果网络速度很快,或者想使日志更加紧凑以便以后的分析,那么应该使用二进制的日志文件格式.如tcpdump格式或者pcap格式. 这里,我们不需指定本地网络了,因为所以的东西都被 ...
- @ComponentScan、@EnableFeignClients和@MapperScan注解笔记
@ComponentScan:此注解是用来管理容器中的bean,即是管理项目中类的依赖关系, 注意此注解并不创建类的实例: 默认情况下此注解扫描本工程下的所有包, ...
- Apollo源码搭建调试看一文就够
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场景. 我 ...
- IIS 安装了.net framework 4.0/4.5 却找不到相应应用程序池
通常情况下是因为没注册造成的,有些安装包会自己帮你注册上有些不会,感觉略坑. 注册方法:在计算机中点击 开始菜单–>运行 拷贝以下内容运行一下即可. C:\WINDOWS\Microsoft.N ...
- ASP.NET Eval四种绑定方式 及详解
1.1.x中的数据绑定语法 <asp:Literal id="litEval2" runat="server" Text='<%#DataBinde ...
- eclipse 升级note
参考http://www.cnblogs.com/jiqingwu/archive/2013/05/26/eclipse_plugins_import.html. 最终决定采用 启动 eclipse. ...
- JSP的有哪些内置对象,作用分别是什么?
request:表示HttpServletRequest对象,它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie,header和session数据的有用方法: response:表示Ht ...