tzoj 3647: Hawk-and-Chicken

描述

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.

输入

There are several test cases. First is an 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.

输出

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.

样例输入

2
4 3
3 2
2 0
2 1

3 3
1 0
2 1
0 2

样例输出

Case 1: 2
0 1
Case 2: 2
0 1 2

题意

给定一系列点和有向边,这些点可以通过有向边向下一个点传递一张票,但自己的票无法传递给自己,询问获得的最大数量的票和获得这些票的点,如果存在多个获得最大票数的点一起输出

题解

强连通分量加缩点

先用tarjan计算出存在强连通分量的点集并缩块成点,这些点能够构成一棵树

最大值必然存在于这棵树的叶子节点上

所以反向建边,叶子节点所能到达的所有点的票数之和就是该点获得的最大票数(感觉拓扑也行但不知道为什么一直wa)

叶子节点在新建的树上入度为零,所以统计所有入度为零的点的票数,其最大值就是答案,记得答案要减一,不包括本身

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef unsigned long long ULL;
    
mt19937 rnd(random_device{}());
uniform_int_distribution<int> dist(0,INT_MAX);
//#define int long long
const int N = 5010,M = 60010,mod = 998244353,INF = 1e8;

int h[N],ne[M],v[M],idx,h1[N];
int idx1 = 1,dfn[N],low[N],st[N],w[N],du[N];
int stk[N],tt = -1,idx2;
int p[N],ans,vis[N],tk[N];

void add(int x,int y)
{
	v[idx] = y,ne[idx] = h[x],h[x] = idx++;
}

void add1(int x,int y)
{
	v[idx] = y,ne[idx] = h1[x],h1[x] = idx++;
}

void init()
{
	memset(h,-1,sizeof(h));
    memset(h1,-1,sizeof(h1));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(st,0,sizeof(st));
	memset(w,0,sizeof(w));
    memset(du,0,sizeof(du));
    memset(vis,0,sizeof(vis));
    memset(tk,0,sizeof(tk));
	idx = 0,idx1 = 1,idx2 = 0;
    ans = 0,tt = -1;
}

void tarjan(int u)
{
	dfn[u] = low[u] = idx1++;
	stk[++tt] = u;
	st[u] = 1;
	for(int i=h[u];i!=-1;i=ne[i])
	{
		int j = v[i];
		if(!dfn[j])
		{
			tarjan(j);
			low[u] = min(low[u],low[j]);
		}
		else if(st[j])
			low[u] = min(low[u],dfn[j]);
	}
	if(dfn[u]==low[u])
	{
		idx2++;
		int t;
		do{
			t = stk[tt--];
			p[t] = idx2;
			w[idx2]++;
			st[t] = 0;
		}while(t!=u);
	}
}

int dfs(int u)
{
    int sum = 0;
    for(int i=h1[u];i!=-1;i=ne[i])
    {
        int j = v[i];
        if(vis[j]) continue;
        vis[j] = 1;
        sum += dfs(j)+w[j];
    }
    return sum;
}

/*void topsort()
{
    queue<int> q;
    for(int i=1;i<=idx2;i++)
        if(du[i]==0) q.push(i);
    while(q.size())
    {
        int t = q.front();
        ans = max(ans,w[t]);
        q.pop();
        for(int i=h1[t];i!=-1;i=ne[i])
        {
            int j = v[i];
            w[j] += w[t];
            if(--du[j]==0) q.push(j); 
        }
    }
}*/

int main()
{
	int n,m,cnt = 0;
    int T;
    cin>>T;
	while(T--)
	{
		init();
        cnt++;
        cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			int u,v;
			scanf("%d %d",&u,&v);
			add(u,v);
		}
		for(int i=0;i<n;i++)
			if(!dfn[i])
				tarjan(i);
		for(int i=0;i<n;i++)
			for(int j=h[i];j!=-1;j=ne[j])
			{
				int k = v[j];
				if(p[k]!=p[i])
					add1(p[k],p[i]),du[p[i]]++;
			}
		for(int i=1;i<=idx2;i++)
        {
            if(du[i]==0)
            {
                memset(vis,0,sizeof(vis));
                vis[i] = 1;
                tk[i] = max(tk[i],dfs(i)+w[i]-1);
            }
            ans = max(ans,tk[i]);
        }
        vector<int> res;
        for(int i=0;i<n;i++)
            if(ans==tk[p[i]])
                res.push_back(i);
        printf("Case %d: %d\n",cnt,ans);
        for(int i=0;i<res.size();i++)
        {
            if(i) cout<<' ';
            cout<<res[i];
        }
        cout<<endl;
	}
	
	
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值