1. 程式人生 > >HDU 3969 Hawk-and-Chicken(dfs+tarjan縮點優化,網上最詳細解析!!!)

HDU 3969 Hawk-and-Chicken(dfs+tarjan縮點優化,網上最詳細解析!!!)

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4170    Accepted Submission(s): 1301


Problem Description Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: every kid in this game want to play the role of Hawk.
So the teacher came up with an idea: Vote. Every child have some nice handkerchiefs, and if he/she think someone is suitable for the role of Hawk, he/she gives a handkerchief to this kid, which means this kid who is given the handkerchief win the support. Note the support can be transmitted. Kids who get the most supports win in the vote and able to play the role of Hawk.(A note:if A can win
support from B(A != B) A can win only one support from B in any case the number of the supports transmitted from B to A are many. And A can't win the support from himself in any case.
If two or more kids own the same number of support from others, we treat all of them as winner.
Here's a sample: 3 kids A, B and C, A gives a handkerchief to B, B gives a handkerchief to C, so C wins 2 supports and he is choosen to be the Hawk.  

 

Input There are several test cases. First is a integer T(T <= 50), means the number of test cases.
Each test case start with two integer n, m in a line (2 <= n <= 5000, 0 <m <= 30000). n means there are n children(numbered from 0 to n - 1). Each of the following m lines contains two integers A and B(A != B) denoting that the child numbered A give a handkerchief to B.  

 

Output For each test case, the output should first contain one line with "Case x:", here x means the case number start from 1. Followed by one number which is the total supports the winner(s) get.
Then follow a line contain all the Hawks' number. The numbers must be listed in increasing order and separated by single spaces.  

 

Sample Input 2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2  

 

Sample Output Case 1: 2 0 1 Case 2: 2 0 1 2  

 

Author Dragon  

 

Source 2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU   分析: 題目意思:
給你一個有向圖,a可以到b,b可以到c,那麼c點就可以得到兩分
問你得到分數最高的點是哪些點(升序輸出),且最高的得分是多少 分析:
對於某個點x
就是算有多少個點可以到x
直接dfs的話,不行,得反向建圖,原因:
正向建圖的話,對x點,有的點因為dfs順序導致點標記的原因,導致某些點不到到達x
比如圖:
1->x
2->x
3->1
3->2
如果是先dfs1,然後dfs2的話,1和2都被標記了,不能通過了,導致3不能到達x,實際上3能到x
反向建圖的話,只要看x能到哪些點就可以了,可以直接dfs,不存在上述麻煩 只要反向建圖,不論是否有強連通分量,都是可以直接dfs的!!!!
是對出度為0的點dfs
但是看看資料範圍,直覺應該是會超時....
然後來了一發,果然超時 所以我們得進行優化
優化方法:
對強連通分量進行縮點處理
對某個強連通分量,假設該強連通分量有k個點,那麼該強連通分量的每個點的得分都是k-1
所以我們縮點,然後重新建圖,得到一個沒有強連通分量的有向圖
然後再跑dfs
所以我們用tarjan演算法進行縮點重新建圖處理   code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};

int getval()
{
    int ret(0);
    char c;
    while((c=getchar())==' '||c=='\n'||c=='\r');
    ret=c-'0';
    while((c=getchar())!=' '&&c!='\n'&&c!='\r')
        ret=ret*10+c-'0';
    return ret;
}

#define max_v 5005
int vis[max_v];
int dfn[max_v];
int low[max_v];
int stk[max_v];
int color[max_v];//染色
int indgree[max_v];//入度
int v[max_v];//存放每個點dfs的結果
int a[max_v];//存放需要輸出的分數最高的點
int used[max_v];//dfs的標記陣列
int b[max_v];//每種顏色所包括的點的數量
vector<int> G[max_v];//原圖
vector<int> G1[max_v];//新圖
int n,m;
int sig,cnt,sp;
int cu;

void init()
{
    me(vis,0);
    me(dfn,0);
    me(low,0);
    me(stk,0);
    me(color,0);
    me(indgree,0);
    me(v,0);
    me(a,0);
    me(b,0);
    for(int i=1;i<=n;i++)
    {
        G[i].clear();
        G1[i].clear();
    }
    sig=0;
    cnt=1;
    sp=-1;
    cu=0;
}

void tarjan(int u)
{
    dfn[u]=low[u]=cnt++;
    vis[u]=1;
    stk[++sp]=u;
    for(int j=0;j<G[u].size();j++)
    {
        int v=G[u][j];
        if(vis[v]==0)
            tarjan(v);
        if(vis[v]==1)
            low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        sig++;
        do
        {
            vis[stk[sp]]=-1;
            color[stk[sp]]=sig;
        }while(stk[sp--]!=u);
    }
}

int f(int x)//統計顏色x的點的數目
{
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        if(color[i]==x)
            sum++;
    }
    return sum;
}

int dfs(int u,int ans)
{
    used[u]=1;
    for(int j=0;j<G1[u].size();j++)
    {
        int v=G1[u][j];
        if(used[v]==0)
        {
            ans+=b[v];
            ans=dfs(v,ans);
        }

    }
    return ans;
}
void ff(int x)//將顏色為x的點放進輸出陣列
{
    for(int i=1;i<=n;i++)
    {
        if(color[i]==x)
            a[cu++]=i-1;
    }
}
int main()
{
    int t;
    int ca=1;
    int x,y;
    cin>>t;
    while(t--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&x,&y);
            x++,y++;
            if(count(G[x].begin(),G[x].end(),y)==0)//防止重邊
                G[x].push_back(y);
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
                tarjan(i);
        }
        for(int i=1;i<=sig;i++)
            b[i]=f(i);//記憶化優化
            
        //建新圖,反向
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<G[i].size();j++)
            {
                int v=G[i][j];
                if(color[v]!=color[i])
                {
                    if(count(G1[color[v]].begin(),G1[color[v]].end(),color[i])==0)//預防重邊
                    {
                        G1[color[v]].push_back(color[i]);
                        indgree[color[i]]++;
                    }
                }
            }
        }
        //跑新圖入度為0的點的dfs
        int maxv=-INF;
        for(int i=1;i<=sig;i++)
        {
            if(indgree[i]==0)
            {
                me(used,0);
                v[i]=dfs(i,0)+b[i]-1;
                maxv=max(maxv,v[i]);
            }
        }
        printf("Case %d: %d\n",ca++,maxv);
        for(int i=1;i<=sig;i++)
        {
            if(v[i]==maxv)//存在得分最高的多個點
            {
                ff(i);
            }
        }
        sort(a,a+cu);
        for(int i=0;i<cu;i++)
        {
            if(i==0)
                printf("%d",a[i]);
            else
                printf(" %d",a[i]);
        }
        printf("\n");
    }
    return 0;
}
/*
題目意思:
給你一個有向圖,a可以到b,b可以到c,那麼c點就可以得到兩分
問你得到分數最高的點是哪些點(升序輸出),且最高的得分是多少

分析:
對於某個點x
就是算有多少個點可以到x
直接dfs的話,不行,得反向建圖,原因:
正向建圖的話,對x點,有的點因為dfs順序導致點標記的原因,導致某些點不到到達x
比如圖:
1->x
2->x
3->1
3->2
如果是先dfs1,然後dfs2的話,1和2都被標記了,不能通過了,導致3不能到達x,實際上3能到x
反向建圖的話,只要看x能到哪些點就可以了,可以直接dfs,不存在上述麻煩

只要反向建圖,不論是否有強連通分量,都是可以直接dfs的!!!!
是對出度為0的點dfs
但是看看資料範圍,直覺應該是會超時....
然後來了一發,果然超時

所以我們得進行優化
優化方法:
對強連通分量進行縮點處理
對某個強連通分量,假設該強連通分量有k個點,那麼該強連通分量的每個點的得分都是k-1
所以我們縮點,然後重新建圖,得到一個沒有強連通分量的有向圖
然後再跑dfs
所以我們用tarjan演算法進行縮點重新建圖處理
*/