zoukankan      html  css  js  c++  java
  • HDU 5420 Victor and Proposition

    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)

    解题:线段树优化建图,妙哉,内存开得好凶残,吓呆本宝宝了

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 typedef long long LL;
      4 typedef pair<int,int> pii;
      5 const int INF = 0x3f3f3f3f;
      6 const int maxn = 2000010;
      7 struct arc {
      8     int to,next;
      9     arc(int x = 0,int y = -1) {
     10         to = x;
     11         next = y;
     12     }
     13 } e[(100000+1000)*20*8];
     14 int head[maxn],L[maxn],R[maxn],tot,clk;
     15 int dep[maxn],hs[maxn],st[maxn],n;
     16 vector<pii>order[maxn];
     17 void add(int u,int v) {
     18     e[tot] = arc(v,head[u]);
     19     head[u] = tot++;
     20 }
     21 void init() {
     22     tot = 0;
     23     memset(head,-1,sizeof head);
     24 }
     25 void dfs(int u,int depth) {
     26     hs[L[u] = ++clk] = u;
     27     dep[u] = depth;
     28     for(int i = head[u]; ~i; i = e[i].next) dfs(e[i].to,depth + 1);
     29     R[u] = clk;
     30 }
     31 void build(int L,int R,int v) {
     32     order[v].resize(R - L + 1);
     33     if(L == R) {
     34         st[v] = ++n;
     35         order[v][0] = pii(dep[hs[L]],hs[L]);
     36         add(n,hs[L]);
     37         return;
     38     }
     39     int mid = (L + R)>>1;
     40     build(L,mid,v<<1);
     41     build(mid + 1,R,v<<1|1);
     42     st[v] = n + 1;
     43     merge(order[v<<1].begin(),order[v<<1].end(),order[v<<1|1].begin(),order[v<<1|1].end(),order[v].begin());
     44     for(int i = 1; i <= R - L; ++i)
     45         add(n + i + 1, n + i);
     46     for(int i = 0; i <= R - L; ++i) 
     47         add(n + i + 1,order[v][i].second);
     48     n += R - L + 1;
     49 }
     50 void connect(int L,int R,int lt,int rt,int u,int d,int v) {
     51     if(lt <= L && rt >= R) {
     52         int pos = lower_bound(order[v].begin(),order[v].end(),pii(d,INF)) - order[v].begin() - 1;
     53         if(~pos) add(u,st[v] + pos);
     54         return;
     55     }
     56     int mid = (L + R)>>1;
     57     if(lt <= mid) connect(L,mid,lt,rt,u,d,v<<1);
     58     if(rt > mid) connect(mid + 1,R,lt,rt,u,d,v<<1|1);
     59 }
     60 int dfn[maxn],low[maxn],cnt[maxn],scc,ct;
     61 bool instack[maxn];
     62 stack<int>stk;
     63 void tarjan(int u) {
     64     dfn[u] = low[u] = ++ct;
     65     instack[u] = true;
     66     stk.push(u);
     67     for(int i = head[u]; ~i; i = e[i].next) {
     68         if(!dfn[e[i].to]) {
     69             tarjan(e[i].to);
     70             low[u] = min(low[u],low[e[i].to]);
     71         } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
     72     }
     73     if(low[u] == dfn[u]) {
     74         int v;
     75         cnt[++scc] = 0;
     76         do {
     77             instack[v = stk.top()] = false;
     78             stk.pop();
     79             cnt[scc] += (v <= clk);
     80         } while(v != u);
     81     }
     82 }
     83 int main() {
     84     int kase,u,v;
     85     scanf("%d",&kase);
     86     while(kase--) {
     87         scanf("%d",&n);
     88         init();
     89         clk = scc = ct = 0;
     90         memset(dfn,0,sizeof dfn);
     91         memset(instack,false,sizeof instack);
     92         for(int i = 2; i <= n; ++i) {
     93             scanf("%d",&u);
     94             add(u,i);
     95         }
     96         dfs(1,0);
     97         init();
     98         build(1,clk,1);
     99         for(int i = 1; i <= clk; ++i) {
    100             scanf("%d%d",&u,&v);
    101             connect(1,clk,L[u],R[u],i,dep[u] + v,1);
    102         }
    103         for(int i = 1; i <= n; ++i)
    104             if(!dfn[i]) tarjan(i);
    105         LL ret = 0;
    106         for(int i = 1; i <= scc; ++i)
    107             ret += (LL)cnt[i]*(cnt[i]-1)/2;
    108         printf("%I64d
    ",ret);
    109     }
    110     return 0;
    111 }
    View Code
  • 相关阅读:
    题解——[[SHOI2010]最小生成树]
    7.12周总结
    还有5个月就NOIP2019了,我干了什么
    【CQOI2018】破解D-H协议
    【SHOI2006】仙人掌
    【HNOI/AHOI2018】道路
    2019.11纪中集训 宋新波老师和曹天佑学长的勉励
    纪中集训2019.11.05
    【2019.10.25 OI-Killer的模拟赛】3.鸡数
    【华东师附国庆模拟赛】Day2 1.矩阵
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4871388.html
Copyright © 2011-2022 走看看