Test instructions: Voting, voting can be passed, when a person votes, to the person's votes to the person voted, ask the last in ascending order to output the highest number of people.
Idea: Obviously all points within a connected block have the same number of votes, namely: 1 in Block, (1: Is himself). So first we have to Tarjan, and then find out that each block can be reached by several blocks (here you can reverse-build Dfs). The last person to output the maximum number of votes can be.
#include <iostream> #include <cstring> #include <cstdio> #include <stack> #include < Algorithm> #define MM (x) memset (x,0,sizeof (x)) using namespace std;const int n=5000+50;const int m=30000+50;int n,m; int dfn[n],low[n],num[n],paint[n],instack[n],index,col; int val[n],vis[n],res[n],ind[n];struct node{int v,next;}; struct Qxx{int head[n],cnt;node e[m];void Init () {memset (head); head,-1,sizeof;} void Add (int a,int b) {e[cnt].v=b;e[cnt].next=head[a];head[a]=cnt++;}} Qxx1,qxx2;stack<int>s;void Tarjan_init () {index=col=0;mm (DFN), mm (low), MM (num), mm (paint), mm (Instack), while ( !s.empty ()) S.pop (); }int Min (int a,int b) {if (a<b) return A;return B;} void Tarjan (int u) {dfn[u]=low[u]=++index;instack[u]=1;s.push (U); for (int i=qxx1.head[u];i+1;i=qxx1.e[i].next) {int V =qxx1.e[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]) {++col;int k=s.top (); while (u!=k) {s.pop ();p aint[k]=col;num[col]++;instack[k]=0;k=s.top ();} S.pop ();p aint[u]=col;num[col]++;instack[u]=0;}} int dfs (int u,int &sum) {vis[u]=1;sum+=num[u];for (int i=qxx2.head[u];i+1;i=qxx2.e[i].next) {int v=qxx2.e[i].v;if ( VIS[V]) Continue;dfs (v,sum);}} int main () {int t,ca=1;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); Tarjan_init (); qxx1. Init (); for (int i=1;i<=m;i++) {int a,b;scanf ("%d%d", &a,&b); Qxx1.add (A, b);} for (int i=0;i<n;i++) {if (!dfn[i]) Tarjan (i);} Qxx2. Init (); mm (Ind); for (int i=0;i<n;i++) {int a=paint[i];for (int j=qxx1.head[i];j+1;j=qxx1.e[j].next) {int b=paint[ Qxx1.e[j].v];if (a!=b) {qxx2.add (b,a); ind[a]++;}}} MM (val); for (int i=1;i<=col;i++) {if (ind[i]==0) {mm (VIS); int Sum=0;dfs (i,sum); val[i]=sum;}} int maxx=-1;for (int i=1;i<=col;i++) {if (Val[i]>maxx) {maxx=val[i];}} int num=0;for (int i=0;i<n;i++) {if (Val[paint[i]]==maxx) {res[num++]=i;}} Sort (res,res+num);p rintf ("Case%d:%d\n", ca++,maxx-1), and for (int i=0;i<num;i++) {if (i!=0) printf ("");p rintf ("%d", Res[i]);} printf ("\ n");} return 0;}
HDU 3639 hawk-and-chicken Tarjan pinch point +dfs