Hawk-and-Chicken

传送门



Hawk-and-Chicken

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


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
 

Recommend
lcy

题目大意:有N个人进行投票,想要选择出最受欢迎的人,投票的规则如下 
1.不能投给自己 
2.投票可以传递,比如A投给B一票,B投给C一票,那么C就得到了A的一票和B的一票

问票数最多能得多少,得到最高票的有哪些人

解题思路:求出所有的强连通分量,这些强连通分量里面的人得到的票数都是一样的,为强连通分量内的点数-1

接着将强连通分量进行缩点,用桥连接起来,反向建边 
接着以所有出度为0的点进行dfs,得出这个连通分量内的点所能得到的最高票数(出度为0的点得到的票数是最多的)

//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+10;
const int maxx=2e5+100;
const double EPS=1e-7;
const int MOD=10000007;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}

int vis[maxx],dfn[maxx],low[maxx],bel[maxx],num[maxx],res,pre[maxx],a[maxx],in[maxx];
int cont=1,n,m,num2[maxx],num3[maxx],ans[maxx];
struct node
{
    int u,v,w;
}s[maxx];
vector<int>G[maxx],G_new[maxx];
queue<int>Q;
stack<int>S;
vector<int>g[maxx];
void dfs(int x)
{
    dfn[x]=low[x]=cont++;
    S.push(x);vis[x]=1;
    int len=G[x].size();
    for(int i=0; i<len; i++)
        if(!vis[G[x][i]])
        {
            dfs(G[x][i]);
            low[x]=min(low[x],low[G[x][i]]);
        }
        else if(vis[G[x][i]]==1)
            low[x]=min(low[x],dfn[G[x][i]]);
    if(dfn[x]==low[x])
    {
        res++;
        while(1)
        {
            int t=S.top();
            S.pop();
            vis[t]=2;  //访问完成
            bel[t]=res;
            num[res]++;//有多少个人在这个编号里
            if(x==t)break;
        }
      //  if(res>1)ans++;
    }
}

void init()
{
    me(pre);me(vis);(low);me(bel);me(dfn);me(num);me(in);
    me(num);me(num2);me(num3);me(ans);
    for(int i=1;i<=n;i++)
    {
        G[i].clear();
        pre[i]=INF;
        g[i].clear();
    }
    me(s);
   // memset(a,INF,sizeof(a));
    res=0;cont=1;
}

int dfs1(int u)
{
    int sum=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(vis[v]) continue;
        vis[v]=1;
        sum+=num[v];
        sum+=dfs1(v);
    }
    return sum;
}
int t;
//void solve()
int main()
{
    cin>>t;
    for(int cas=1;cas<=t;cas++)
    {
        // while(~scanf("%d%d",&n,&m))
  //  {
        //close();
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            //scanf("%d%d%d",&x,&y,&z);
            scanf("%d%d",&x,&y);
            x++;y++;
           // s[i].u=x;s[i].v=y;s[i].w=z;
            G[x].push_back(y);
        }
        for(int i=1;i<=n;i++)
            if(!vis[i])  dfs(i);
        for(int i=1; i<=n; i++)
        {
            int len=G[i].size();
            for(int j=0; j<len; j++)
                if(bel[i]!=bel[G[i][j]])
                {
                    g[bel[G[i][j]]].push_back(bel[i]);
                    in[bel[i]]++;
                }
        }
        for(int i=1;i<=res;i++)
        {
            if(in[i]==0)
            {
                me(vis);
                num2[i]=dfs1(i);
            }
        }
        int maxc=-1;
        for(int i=1;i<=n;i++)
        {
            num3[i]=num2[bel[i]]+num[bel[i]]-1;
            maxc=max(maxc,num3[i]);
        }
        printf("Case %d: %d\n",cas,maxc);
        int val=1;
        for(int i=1;i<=n;i++)
        {
            if(num3[i]==maxc)
                ans[val++]=i-1;
        }
        for(int i=1;i<val;i++)
        {
            if(i==1) cout<<ans[i];
            else cout<<" "<<ans[i];
        }
        cout<<endl;
      //  cout<<ans<<endl;
    }
}
/*
int main()
{
    int t;
    //t=1;
  //  close();
    cin>>t;
    for(cas=1;cas<=t;cas++)
        solve();
}*/


Hawk-and-Chicken

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


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
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值