LightOJ1417 Forwarding Emails (Strong connected component + Indent + memory Search)

Source: Internet
Author: User

The problem is that each person receives the message and sends the message to someone he knows so that he can get the most out of the information at the beginning.

    • First, identify the SCC in the diagram and record the number of points in each SCC, and if it passes to an SCC, the information will be visible to everyone inside.
    • Then the SCC shrinks to form a DAG, and the direct memory search, D (U) searches for the maximum number of people that can be transmitted from the U-point start.
    • The last is to find the answer.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMAXN 555556 #defineMAXM 555557 8 structedge{9     intU,v,next;Ten }EDGE[MAXM]; One intNE,HEAD[MAXN]; A voidAddedge (intUintv) { -Edge[ne].u=u; Edge[ne].v=v; edge[ne].next=Head[u]; -head[u]=ne++; the } -  - intSIZE[MAXN],BN,BELONG[MAXN]; - intStack[maxn],top; + BOOLINSTACK[MAXN]; - intDN,DFN[MAXN],LOW[MAXN]; + voidTarjan (intu) { Adfn[u]=low[u]=++DN; atStack[++top]=u; instack[u]=1; -      for(intI=head[u]; i!=-1; I=Edge[i].next) { -         intv=edge[i].v; -         if(dfn[v]==0){ - Tarjan (v); -low[u]=min (low[u],low[v]); in}Else if(Instack[v]) { -low[u]=min (low[u],dfn[v]); to         } +     } -     if(dfn[u]==Low[u]) { the         intV ++Bn; *          Do{ $v=stack[top--];Panax Notoginsenginstack[v]=0; -belong[v]=Bn; the++Size[bn]; +} while(u!=v); A     } the } +  - intD[MAXN]; $ intDfsintu) { $     if(D[u])returnD[u]; -     intres=size[u],tmp=0; -      for(intI=head[u]; i!=-1; I=Edge[i].next) { the         intv=edge[i].v; -tmp=Max (Tmp,dfs (v));Wuyi     } the     returnd[u]=res+tmp; - } Wu  - intMain () { About     intt,n,a,b; $scanf"%d",&t); -      for(intCse=1; cse<=t; ++CSE) { -Ne=0; -memset (head,-1,sizeof(head)); Ascanf"%d",&n); +          for(intI=0; i<n; ++i) { thescanf"%d%d",&a,&b); - Addedge (A, b); $         } the  thetop=bn=dn=0; thememset (Instack,0,sizeof(Instack)); thememset (DFN,0,sizeof(DFN)); -memset (Size,0,sizeof(size)); in          for(intI=1; i<=n; ++i) { the             if(dfn[i]==0) Tarjan (i); the         } About  the         intTmp=ne; Ne=0; thememset (head,-1,sizeof(head)); the          for(intI=0; i<tmp; ++i) { +             intu=belong[edge[i].u],v=BELONG[EDGE[I].V]; -             if(U==V)Continue; the Addedge (u,v);Bayi         } the  thememset (D,0,sizeof(d)); -         intmx=0; -          for(intI=1; i<=bn; ++i) { themx=Max (Mx,dfs (i)); the         } the          for(intI=1; i<=n; ++i) { the             if(d[belong[i]]==MX) { -printf"Case %d:%d\n", cse,i); the                  Break; the             } the         }94     } the     return 0; the}

LightOJ1417 Forwarding Emails (Strong connected component + Indent + memory Search)

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.