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
 

 

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
#include
#include
#include<string.h>
#include<set>
#include
#include
#include
#include
#include
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)
    {
        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)
    {
        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)
            {
                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)
        {
            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算法进行缩点重新建图处理
*/

 

 

你可能感兴趣的:(HDU 3969 Hawk-and-Chicken(dfs+tarjan缩点优化,网上最详细解析!!!))