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 }
  • 相关阅读:
    3-8
    3-7
    3-5
    3-4
    3-3
    3-2
    3-1
    2-11
    2-10
    2-9
  • 原文地址:https://www.cnblogs.com/Comfortable/p/11360009.html
Copyright © 2011-2022 走看看