HDU 3639 directed graph shrinkage + reverse graph + search

Source: Internet
Author: User

Question: to give a directed graph, each person can vote (many people can vote for one vote at a time), but one person can only support one person at a time, and the support can be passed. It is not counted that one person supports himself, the most supported users.

I started to think about shrinking points and then searching. The problem was that I was wrong! It is assumed that the calculation by number of votes is supported, not by number of people! You can also find the number of edges that can be reached in various tree-like DP/search methods .. Submitted wa...

After reading the questions again and again, I found out .. Error .. As long as the number of people... The problem is much simpler...

After shrinking to a directed acyclic graph: the number of people supported in each SCC is the number of people in the SCC,The point with the most reachable points is required. Of course, it is necessary to let go for convenience: the point that can be reached by the undirected graph is the most! As a result, it is easy to create an undirected graph and directly use DFS to record the answer... Submit... It took me more than two hours to answer this question .. Failed...

#include<iostream>#include<stack>#include<queue>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int inf=0x3f3f3f3f;const int maxv=5005,maxe=50005;int nume=0;int head[maxv];int e[maxe][3];void inline adde(int i,int j,int c){    e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;    e[nume++][2]=c;}int dfn[maxv];int low[maxv];int vis[maxv];int ins[maxv]; stack<int>sta;int scc[maxv];int numb=0;int times=0;int n,m;  int sccsize[maxv];void tarjan(int u){    dfn[u]=low[u]=times++;    ins[u]=1;    sta.push(u);    for(int i=head[u];i!=-1;i=e[i][1])    {        int v=e[i][0];        if(!vis[v])        {            vis[v]=1;            tarjan(v);            if(low[v]<low[u])low[u]=low[v];        }        else if(ins[v]&&dfn[v]<low[u])        {            low[u]=dfn[v];        }    }    if(low[u]==dfn[u])    {        numb++;int counted=0;        int cur;       do        {            cur=sta.top();            sta.pop();            ins[cur]=0;            scc[cur]=numb;            counted++;        }while(cur!=u);        sccsize[numb]=counted;    }}struct pi{    int id;    int w;};bool my (pi a,pi b){    if(a.w!=b.w)      return a.w>b.w;    else      return a.id<b.id;}int sums=0;vector<vector<int> >e2(maxv+1);int ind[maxv];void dfs(int u){    for(int i=0;i<e2[u].size();i++)    {        int v=e2[u][i];        if(!vis[v])        {            vis[v]=1;            sums+=sccsize[v];            dfs(v);        }    }}vector<int>sumss;void solve(){    for(int i=0;i<n;i++)   {       if(!vis[i])       {           vis[i]=1;           tarjan(i);       }   }    for(int i=0;i<n;i++)      for(int j=head[i];j!=-1;j=e[j][1])      {          int v=e[j][0];          if(scc[i]!=scc[v])         {            e2[scc[v]].push_back(scc[i]);            ind[scc[i]]++;         }      }      int minsums=0;      vector<pi>ans;    for(int i=1;i<=numb;i++)        {            sums=0;            memset(vis,0,sizeof(vis));            if(ind[i]==0)            {                vis[i]=1;                sums+=sccsize[i]-1;                dfs(i);              if(sums>=minsums)              {                  pi tp;                  tp.w=sums;                  tp.id=i;                  ans.push_back(tp);                  minsums=sums;              }            }        }        vector<int>realans;       for(int i=0;i<ans.size();i++)        {            if(ans[i].w==minsums)            {                realans.push_back(ans[i].id);            }        }    for(int j=0;j<realans.size();j++)        for(int i=0;i<n;i++)        {            if(scc[i]==realans[j])            {                sumss.push_back(i);            }        }     sort(sumss.begin(),sumss.end());     printf("%d\n",minsums);     for(int i=0;i<sumss.size();i++)     {          if(i!=sumss.size()-1)              printf("%d ",sumss[i]);          else               printf("%d\n",sumss[i]);     }}void read_build(){    int aa,bb,cc;    for(int i=0;i<m;i++)    {        scanf("%d%d",&aa,&bb);        adde(aa,bb,0);    }}void init(){    numb=times=nume=0;    sumss.clear();    for(int i=0;i<maxv;i++)      {          head[i]=-1;          ins[i]=dfn[i]=low[i]=scc[i]=vis[i]=0;          sccsize[i]=0;          ind[i]=0;          e2[i].clear();      }}int main(){    int T;    scanf("%d",&T);    int ct=1;    while(T--)    {        scanf("%d%d",&n,&m);        init();        read_build();        printf("Case %d: ",ct++);        solve();    }    return 0;}


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.