zoukankan      html  css  js  c++  java
  • [支配树] Hdu P4694 Important Sisters

    Problem Description

    There are N clones of Misaka Mikoto (sisters) forming the Misaka network. Some pairs of sisters are connected so that one of them can pass message to the other one. The sister with serial number N is the source of all messages. All the other sisters get message directly or indirectly from her. There might be more than one path from sister #N to sister #I, but some sisters do appear in all of these paths. These sisters are called important sister of sister #K. What are the important sisters of each sister?

    题解

    • 支配树裸题

    代码

     1 #include<iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define N 50010
     5 #define ll long long
     6 using namespace std;
     7 ll val[N];
     8 int n,m,cnt,tot,mark[N],dn,head[N],f[N],fa[N],top[N],last[N],Best[N],dfn[N],id[N],s[N],d[N];
     9 struct edge{int to,from;}e[N*100];
    10 void ins(int x,int y)
    11 { 
    12     e[++cnt].to=y,e[cnt].from=head[x],head[x]=cnt; 
    13     e[++cnt].to=x,e[cnt].from=last[y],last[y]=cnt; 
    14 }    
    15 void add(int x,int y) { e[++cnt].to=y,e[cnt].from=top[x],top[x]=cnt; }
    16 void pre(int n)
    17 {
    18     id[dfn[n]=++dfn[0]]=n;
    19     for (int i=head[n];i;i=e[i].from) if (!dfn[e[i].to]) f[e[i].to]=n,pre(e[i].to);
    20 }
    21 int find(int x)
    22 {
    23     if (fa[x]==x) return x;
    24     int y=find(fa[x]);
    25     if (dfn[s[Best[x]]]>dfn[s[Best[fa[x]]]]) Best[x]=Best[fa[x]];
    26     return fa[x]=y;
    27 }
    28 void tarjan()
    29 {
    30     for(int i=dfn[0];i>1;--i)
    31     {
    32         int u=id[i];
    33         for (int j=last[u];j;j=e[j].from) if (dfn[e[j].to]) find(e[j].to),(dfn[s[u]]>dfn[s[Best[e[j].to]]])?s[u]=s[Best[e[j].to]]:0;
    34         add(s[u],u),fa[u]=f[u],u=f[u];
    35         for (int j=top[u];j;j=e[j].from)
    36         {
    37             find(e[j].to);
    38             if (s[Best[e[j].to]]==s[u]) d[e[j].to]=u; else d[e[j].to]=Best[e[j].to];
    39         }
    40     }
    41     for(int i=2;i<=dfn[0];i++) if (d[id[i]]!=s[id[i]]) d[id[i]]=d[d[id[i]]];
    42 }
    43 int main()
    44 {
    45     while (scanf("%d%d",&n,&m)!=EOF)
    46     {
    47         cnt=dfn[0]=0,++tot,memset(head,0,sizeof(int)*(n+2)),memset(last,0,sizeof(int)*(n+2)),
    48         memset(top,0,sizeof(int)*(n+2)),memset(val,0,sizeof(ll)*(n+2)),memset(dfn,0,sizeof(int)*(n+2)),memset(d,0,sizeof(int)*(n+2));
    49         for (int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),ins(x,y);
    50         for (int i=1;i<=n;i++) Best[i]=s[i]=fa[i]=i;    
    51         pre(n),tarjan();
    52         for (int i=1;i<=dfn[0];i++) val[id[i]]=val[d[id[i]]]+id[i];
    53         for (int i=1;i<=n;i++) printf("%lld%c",dfn[i]?val[i]:0,i==n?'
    ':' ');
    54     }
    55 }
  • 相关阅读:
    全区停水,测什么时候来水。
    和电脑打国标麻将,一定要注意
    这是一个可怕的交易
    起一卦,还是这样。什么时候能好起来。
    Linux内核源码真是个好东西
    命宫——天机化忌
    bzoj1024[SCOI2009]生日快乐
    bzoj2561最小生成树
    bzoj2423[HAOI2010]最长公共子序列
    bzoj2705[SDOI2012]Longge的问题
  • 原文地址:https://www.cnblogs.com/Comfortable/p/11360009.html
Copyright © 2011-2022 走看看