HDU 3639 Hawk-and-chicken (Strong connected component + Tree DP)

Source: Internet
Author: User

Title Address: HDU 3639

First with the strong connected components of the contraction point, after the contraction point, and then re-indent after the block in reverse order of the composition, the value of each block is inside the number of points, then the largest of the votes must be re-composition after the degree of 0 of the block, and then find out the maximum value can be.

The code is as follows:

#include <iostream> #include <string.h> #include <math.h> #include <queue> #include < algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>using namespace std; #define LL Long long#define pi ACOs ( -1.0) const int Mod=1e9+7;const int Inf=0x3f3f3f3f;const double eqs=1e-9; const int Maxn=5000+10;const int Maxm=30000+10;int HEAD[MAXN], CNT, LOW[MAXN], DFN[MAXN], BELONG[MAXN], INSTACK[MAXN], St K[MAXN], dp[maxn];int ans, indx, top;struct node{int u, V, next;}        edge[maxm];void Add (int u, int v) {edge[cnt].v=v;        Edge[cnt].next=head[u]; head[u]=cnt++;}        void Tarjan (int u) {low[u]=dfn[u]=++indx;        Instack[u]=1;        Stk[++top]=u;                for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;                        if (!dfn[v]) {Tarjan (v);                Low[u]=min (Low[u],low[v]);                       } else if (Instack[v]) { Low[u]=min (Low[u],dfn[v]);                }} if (Low[u]==dfn[u]) {ans++;                        while (1) {int v=stk[top--];                        Belong[v]=ans;                        dp[ans]++;                        instack[v]=0;                if (u==v) break;        }}}void init () {memset (head,-1,sizeof (head));        memset (dfn,0,sizeof (DFN));        memset (instack,0,sizeof (instack));        Memset (Dp,0,sizeof (DP)); Cnt=ans=indx=top=0;} int HEAD1[MAXN], cnt1, VIS[MAXN], IN[MAXN], max1, sum;struct node1{int u, V, next;}        edge1[maxm];void add1 (int u, int v) {edge1[cnt1].v=v;        Edge1[cnt1].next=head1[u]; head1[u]=cnt1++;}        void Dfs (int u) {vis[u]=1;        Sum+=dp[u];                for (int i=head1[u];i!=-1;i=edge1[i].next) {int v=edge1[i].v;                if (!vis[v]) {DFS (v); }}}void init1 () {memset (head1,-1,sizeOf (HEAD1));        memset (vis,0,sizeof (VIS));        memset (In,0,sizeof (in));        cnt1=0; max1=0;}        int C[MAXN], tot, d[maxn];int main () {int T, n, M, I, J, u, V, case=0;        scanf ("%d", &t);                while (t--) {case++;                scanf ("%d%d", &n,&m);                Init ();                        while (m--) {scanf ("%d%d", &u,&v);                Add (U,V);                } for (i=0;i<n;i++) {if (!dfn[i]) Tarjan (i);                } init1 (); for (i=0;i<n;i++) {for (J=head[i];j!=-1;j=edge[j].next) {if (belong[                                        I]!=BELONG[EDGE[J].V]) {add1 (belong[edge[j].v],belong[i]);                                in[belong[i]]++;                }}} memset (D,-1,sizeof (d)); for (i=1;i<=ans;i++) {if (!in[i]) {sum=0;                                memset (vis,0,sizeof (VIS));                                DFS (i);                                D[i]=sum;                        Max1=max (Max1,d[i]);                }} tot=0;                for (i=0;i<n;i++) {if (d[belong[i]]==max1) c[tot++]=i;                } printf ("Case%d:%d\n", case, max1-1);                        for (i=0;i<tot;i++) {printf ("%d", c[i]);                if (i!=tot-1) printf ("");        } puts (""); } return 0;}


HDU 3639 Hawk-and-chicken (Strong connected component + Tree DP)

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.