HDOJ 3639 Hawk-and-Chicken

本文介绍了一种解决幼儿园游戏中角色分配问题的算法。通过投票和传递支持的方式确定扮演鹰的角色,采用图论中的强连通分量和拓扑排序思想实现算法。提供了两种实现方案:深度优先搜索(DFS)和广度优先搜索(BFS)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Hawk-and-Chicken

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


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

一:DFS

#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset(a,(b),sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
#define maxn 5005
#define INF 2e9;
int low[maxn],dfn[maxn],in[maxn],vis[maxn];
int instack[maxn],point[maxn],ans[maxn],dp[maxn];
int n,m,tot,num,sum;
vector<int>G1[maxn],G2[maxn];
stack<int>S;
void init()
{
    tot=num=0;
    f(i,0,n)
    {
        G1[i].clear();
        G2[i].clear();
        low[i]=dfn[i]=in[i]=vis[i]=0;
        instack[i]=point[i]=ans[i]=dp[i]=0;
    }
    while(!S.empty())
    {
        S.pop();
    }
}
void tarjan(int x)
{
    low[x]=dfn[x]=tot++;
    vis[x]=instack[x]=1;
    S.push(x);
    f(i,0,G1[x].size())
    {
        int v=G1[x][i];
        if(!vis[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(instack[v])
        {
            low[x]=min(low[x],dfn[v]);
        }
    }
    if(low[x]==dfn[x])
    {
        int cnt=0;
        while(1)
        {
            int v=S.top();
            S.pop();
            instack[v]=0;
            cnt++;
            point[v]=num;
            if(v==x) break;
        }
        ans[num]=cnt;
        num++;
    }
}
void dfs(int x)
{
    vis[x]=1;
    sum+=ans[x];
    f(i,0,G2[x].size())
    {
        if(!vis[G2[x][i]])
            dfs(G2[x][i]);
    }
}
int main()
{
    int T,t=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int x,y;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            G1[x].push_back(y);
        }
        f(i,0,n)
        {
            if(!vis[i])
                tarjan(i);
        }
        f(i,0,n)
        f(j,0,G1[i].size())
        {
            if(point[i]!=point[G1[i][j]])
            {
                in[point[i]]++;
                G2[point[G1[i][j]]].push_back(point[i]);
            }
        }
        int res=-1;
        f(i,0,num)
        {
            if(!in[i])
            {
                sum=0;
                mst(vis,0);
                dfs(i);
                dp[i]=sum;
                res=max(res,sum);
            }
        }
        int flag=1;
        printf("Case %d: %d\n",t++,res-1);
        f(i,0,n)
        {
            if(dp[point[i]]==res)
            {
                if(flag)
                {
                    printf("%d",i);
                    flag=0;
                }
                else printf(" %d",i);
            }
        }
        printf("\n");
    }
    return 0;
}

二:BFS
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset(a,(b),sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
#define maxn 5005
#define INF 2e9;
int low[maxn],dfn[maxn],in[maxn],vis[maxn];
int instack[maxn],point[maxn],ans[maxn],dp[maxn];
int n,m,tot,num,sum;
vector<int>G1[maxn],G2[maxn];
stack<int>S;
void init()
{
    tot=num=0;
    f(i,0,n)
    {
        G1[i].clear();
        G2[i].clear();
        low[i]=dfn[i]=in[i]=vis[i]=0;
        instack[i]=point[i]=ans[i]=dp[i]=0;
    }
    while(!S.empty())
    {
        S.pop();
    }
}
void tarjan(int x)
{
    low[x]=dfn[x]=tot++;
    vis[x]=instack[x]=1;
    S.push(x);
    f(i,0,G1[x].size())
    {
        int v=G1[x][i];
        if(!vis[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(instack[v])
        {
            low[x]=min(low[x],dfn[v]);
        }
    }
    if(low[x]==dfn[x])
    {
        int cnt=0;
        while(1)
        {
            int v=S.top();
            S.pop();
            instack[v]=0;
            cnt++;
            point[v]=num;
            if(v==x) break;
        }
        ans[num]=cnt;
        num++;
    }
}
void bfs(int x)
{
    queue<int>q;
    vis[x]=1;
    q.push(x);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        sum+=ans[v];
        f(i,0,G2[v].size())
        {
            if(!vis[G2[v][i]])
            {
                vis[G2[v][i]]=1;
                q.push(G2[v][i]);
            }
        }
    }
}
int main()
{
    int T,t=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int x,y;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            G1[x].push_back(y);
        }
        f(i,0,n)
        {
            if(!vis[i])
                tarjan(i);
        }
        f(i,0,n)
        f(j,0,G1[i].size())
        {
            if(point[i]!=point[G1[i][j]])
            {
                in[point[i]]++;
                G2[point[G1[i][j]]].push_back(point[i]);
            }
        }
        int res=-1;
        f(i,0,num)
        {
            if(!in[i])
            {
                sum=0;
                mst(vis,0);
                bfs(i);
                dp[i]=sum;
                res=max(res,sum);
            }
        }
        int flag=1;
        printf("Case %d: %d\n",t++,res-1);
        f(i,0,n)
        {
            if(dp[point[i]]==res)
            {
                if(flag)
                {
                    printf("%d",i);
                    flag=0;
                }
                else printf(" %d",i);
            }
        }
        printf("\n");
    }
    return 0;
}




1. 用户与身体信息管理模块 用户信息管理: 注册登录:支持手机号 / 邮箱注册,密码加密存储,提供第三方快捷登录(模拟) 个人资料:记录基本信息(姓名、年龄、性别、身高、体重、职业) 健康目标:用户设置目标(如 “减重 5kg”“增肌”“维持健康”)及期望周期 身体状态跟踪: 体重记录:定期录入体重数据,生成体重变化曲线(折线图) 身体指标:记录 BMI(自动计算)、体脂率(可选)、基础代谢率(根据身高体重估算) 健康状况:用户可填写特殊情况(如糖尿病、过敏食物、素食偏好),系统据此调整推荐 2. 膳食记录与食物数据库模块 食物数据库: 基础信息:包含常见食物(如米饭、鸡蛋、牛肉)的名称、类别(主食 / 肉类 / 蔬菜等)、每份重量 营养成分:记录每 100g 食物的热量(kcal)、蛋白质、脂肪、碳水化合物、维生素、矿物质含量 数据库维护:管理员可添加新食物、更新营养数据,支持按名称 / 类别检索 膳食记录功能: 快速记录:用户选择食物、输入食用量(克 / 份),系统自动计算摄入的营养成分 餐次分类:按早餐 / 午餐 / 晚餐 / 加餐分类记录,支持上传餐食照片(可选) 批量操作:提供常见套餐模板(如 “三明治 + 牛奶”),一键添加到记录 历史记录:按日期查看过往膳食记录,支持编辑 / 删除错误记录 3. 营养分析模块 每日营养摄入分析: 核心指标计算:统计当日摄入的总热量、蛋白质 / 脂肪 / 碳水化合物占比(按每日推荐量对比) 微量营养素分析:检查维生素(如维生素 C、钙、铁)的摄入是否达标 平衡评估:生成 “营养平衡度” 评分(0-100 分),指出摄入过剩或不足的营养素 趋势分析: 周 / 月营养趋势:用折线图展示近 7 天 / 30 天的热量、三大营养素摄入变化 对比分析:将实际摄入与推荐量对比(如 “蛋白质摄入仅达到推荐量的 70%”) 目标达成率:针对健
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值