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, &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)