Hdu4694:important Sisters (Domination tree)

Source: Internet
Author: User
Tags eval

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

Input
There is multiple test cases. Process to the End of File.
The first line of all test case contains integers:the number of sisters 1≤n≤50,000 and the number of connections 0≤m≤100,000. The following m lines is m connections 1≤ai, Bi≤n, indicating that Ai can pass message to Bi.

Output
For each test case, output the sum of the serial numbers of important sisters of each sister, separated with single space.

Sample Input
3 2
3 2
2 1
5 7
3 2
1 2
2 1
3 1
3 2
5 3
5 4

Sample Output
6 5 3
9 10 8) 9 5

Test instructions
A graph of n points m edges is given, and the number of the required points of the N node to each point is calculated.

Exercises
Dominate the tree.
After finding the dominant point of each point, a small recursion from the DFN number to the large
Ans[i]={i (idom[i]=i) ans[idom[i]]+i (idom[i]!=i) ans[i]=\left\{\begin{aligned} & I \,\, (\,\,idom[i]=i\,\,) \ \ & Ans[\,idom[i]\,]+i\,\, (\,\,idom[i]!=i\,\,) \end{aligned} \right.

Code:

#include <bits/stdc++.h> using namespace std;
    inline Int Rd () {char ch=getchar (); int i=0,f=1;
    while (!isdigit (CH)) {if (ch== '-') F=-1;ch=getchar ();}
    while (IsDigit (ch)) {i= (i<<1) + (i<<3) +ch-' 0 '; Ch=getchar ();}
return i*f;
    } inline void W (long long x) {static int buf[50];
    if (!x) {Putchar (' 0 '); return;}
    if (x<0) {Putchar ('-'); x=-x;}
    while (x) {buf[++buf[0]]=x%10;x/=10;}
while (Buf[0]) putchar (buf[buf[0]--]+ ' 0 ');
} const int N=1E5+50,M=4E5+50;
int n,m;
    struct graph{int dfn[n],g[n],fa[n],id[n],v[m],nt[m],ec,tot;
    int Sdom[n],idom[n],mnsd[n],anc[n];long long sum[n];
    vector<int>pre[n],buc[n];
        inline void Getanc (int p) {if (anc[p]!=p) Getanc (Anc[p]);
        if (Dfn[sdom[mnsd[anc[p]]]]<dfn[sdom[mnsd[p]]]) mnsd[p]=mnsd[anc[p];
    ANC[P]=ANC[ANC[P]];
        } inline int eval (int p) {Getanc (P);
    return mnsd[p];
        } inline void Clear () {memset (dfn,0,sizeof (DFN)); memset (G,0,sizeof (g));
        memset (anc,0,sizeof (ANC));
        memset (sum,0,sizeof (sum));
    ec=0;tot=0;
    } inline void Add (int x,int y) {nt[++ec]=g[x];g[x]=ec;v[ec]=y;}
        inline void dfs (int now,int f=0) {fa[now]=f;dfn[now]=++tot;id[tot]=now;
        Sdom[now]=idom[now]=mnsd[now]=anc[now]=now;
            for (int j=g[now];j;j=nt[j]) {if (Dfn[v[j]]) continue;
        DFS (V[j],now);
        }} inline void Solve () {for (int i=1;i<=n;i++) pre[i].clear (), buc[i].clear ();
            for (int i=1;i<=m;i++) {int x=rd (), Y=rd ();
            Add (x, y);
        Pre[y].push_back (x);
        } dfs (n,0);
            for (int i=tot;i>=2;i--) {int u=id[i];
                for (int j=pre[u].size () -1;j>=0;j--) {int t=eval (pre[u][j]), if (!dfn[t]) continue;
            if (Dfn[sdom[u]]>dfn[sdom[t]]) sdom[u]=sdom[t];
            } int f=fa[u];
            Anc[u]=f;
            Buc[sdom[u]].push_back (U); FoR (int j=buc[f].size () -1;j>=0;j--) {int v=buc[f][j],t=eval (v);
                if (Dfn[sdom[t]]<dfn[sdom[v]]) idom[v]=t;
            else Idom[v]=sdom[v];
        } buc[f].clear ();
        } for (int i=2;i<=tot;i++) if (Idom[id[i]]!=sdom[id[i]]) idom[id[i]]=idom[idom[id[i]];
        SUM[ID[1]]=ID[1];
        for (int i=2;i<=tot;i++) sum[id[i]]=sum[idom[id[i]]]+id[i];
        for (int i=1;i<n;i++) W ((dfn[i]?sum[i]:0)), Putchar (");
        W ((dfn[n]?sum[n]:0));
    Putchar (' \ n ');
}}g;
        int main () {while (scanf ("%d%d", &n,&m)!=eof) {g.solve ();
    G.clear (); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.