HDU-3639 Hawk-and-chicken (dfs+ strong connected component)

Source: Internet
Author: User

The main topic: There are N people to vote, want to choose the most popular person, voting rules are as follows
1. You can't vote for yourself.
2. Votes can be passed, for example A to vote B, B to vote C, then c to get a vote of a and b a vote

What is the maximum number of votes, and who will get the highest ticket?

Problem-solving ideas: Find out all the strong connected components, the people inside these strong connected components get the same number of votes, for the strong connected components within the point-1

The strongly connected components are then shrunk, connected by a bridge, and the opposite side is built.
DFS is then performed at all points with a degree of 0, resulting in the highest number of votes available for points within the connected component (the number of votes obtained at the point of 0 is the largest)

#include <cstdio>#include <cstring>#define N 50010#define M 30010#define MIN (a) < (b)? (a): (b))#define MAX (a) > (b)? (a): (b))structedge{intTo, next;} E[M];structnode{intx, y;} NODE[M];intHead[n], Pre[n], Sccno[n],Stack[N], lowlink[n], in[n], sum[n], num[n];intN, M, tot, Dfs_clock, scc_cnt, top;BOOLVis[n];voidAddedge (intUintV) {e[tot].to = v;    E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {scanf("%d%d", &n, &m);memset(Head,-1,sizeof(head)); tot =0; for(inti =0; I < m; i++) {scanf("%d%d", &node[i].x, &AMP;NODE[I].Y);    Addedge (node[i].x, NODE[I].Y); }}voidDfsintu) {pre[u] = lowlink[u] = ++dfs_clock;Stack[++top] = u;intV for(inti = Head[u]; I! =-1; i = e[i].next) {v = e[i].to;if(!pre[v])            {DFS (v);        Lowlink[u] = min (Lowlink[u], lowlink[v]); }Else if(!sccno[v])        {Lowlink[u] = min (Lowlink[u], pre[v]); }    }if(Lowlink[u] = = Pre[u])        {++scc_cnt; NUM[SCC_CNT] =0; while(1) {v =Stack[top--];            SCCNO[V] = scc_cnt; num[scc_cnt]++;if(v = = u) Break; }    }}intDFS2 (intu) {Vis[u] =true;intTMP = Num[u]; for(inti = Head[u]; I! =-1; i = e[i].next) {intv = e[i].to;if(!vis[v])        {tmp + = DFS2 (v); }    }returnTMP;}intCAS =1;voidSolve () {memset(Pre,0,sizeof(pre));memset(Sccno,0,sizeof(SCCNO)); Dfs_clock = scc_cnt = top =0; for(inti =0; I < n; i++)if(!pre[i]) DFS (i);memset(Head,-1,sizeof(head)); tot =0;memset(In,0,sizeof(in));memset(Sum,0,sizeof(Sum));intU, v; for(inti =0; I < m;        i++) {u = sccno[node[i].x]; v = sccno[node[i].y];if(U! = V)            {Addedge (V, u);        in[u]++; }    }intMax =-1; for(inti =1; I <= scc_cnt; i++) {if(In[i] = =0) {memset(Vis,0,sizeof(VIS));            Sum[i] + = DFS2 (i);        max = max (sum[i], max); }    }printf("Case%d:%d\n", cas++, Max-1);BOOLFlag =false; for(inti =0; I < n; i++) {if(Sum[sccno[i]] = = Max) {if(flag)printf(" ");printf("%d", i); Flag =true; }    }printf("\ n");}intMain () {intTestscanf("%d", &test); while(test--)        {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU-3639 Hawk-and-chicken (dfs+ strong connected component)

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.