Ratatöskr

题目描述
Ratatöskr is a squirrel that lives in a giant (but finite) mythical tree called Yggdrasil. He likes to gossip, which sets the other inhabitants of the tree against each other. Ratatöskr is thus hunted by the two ravens of Odin, which are called Hugin and Munin, to bring him to justice.
The tree is made up of nodes connected by branches. Initially, the ravens and the squirrel sit on three different nodes. Now the following happens repeatedly:
• On Odin’s signal, one of the ravens launches into the air and flies to another node of the tree (or possibly back to its previous position), while the other stays where it is.
• During this maneuver, Ratatöskr can travel along the branches to reach another node, but may not pass through a node where a raven sits. He is much quicker than the ravens and will reach his destination before the flying raven lands.
Ratatöskr gets captured if one of the ravens flies to his position and there is no other node he can escape to.
Help Odin determine an optimal strategy for capture, i.e. the minimum number of signals he has to give until Ratatöskr is guaranteed to be captured by a raven.

输入
The input consists of:
• one line with a single integer n (3 ≤ n ≤ 80), the number of nodes in the tree. The nodes are labeled 1, . . . , n.
• one line with a single integer r (1 ≤ r ≤ n), the initial position of the squirrel Ratatöskr.
• one line with a single integer h (1 ≤ h ≤ n), the initial position of the raven Hugin.
• one line with a single integer m (1 ≤ m ≤ n), the initial position of the raven Munin.
• n − 1 lines each containing two integers i and j (1 ≤ i < j ≤ n), indicating a branch between nodes i and j.
The positions r, h and m are distinct. There is at most one branch between any two nodes and every node is reachable from every other node by a sequence of branches.

输出
One line containing an integer s, the number of signals that the ravens need to capture Ratatöskr in an optimal strategy. If Ratatöskr can escape them indefinitely, output impossible.

样例输入
4
1
2
4
1 4
2 4
3 4

样例输出
1

思路
枚举每一个点,如果该点为乌鸦所占的点,则计算存在松鼠的子树的深度,如果不是乌鸦所占的点,则计算以该点作为根节点的子树的深度,取最小值即可

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=105;
typedef pair<string,string> P;
const int inf=0x3f3f3f3;

int n,m,r,h;
vector<int>mp[N];
int dp[N];
bool vis[N];
inline void add(int u,int v)
{
    mp[v].push_back(u);mp[u].push_back(v);
}

void dfs(int u,int v)
{
    dp[u]=1;
    if(u==r) vis[u]=true;
    for(int i=0;i<mp[u].size();i++)
    {
        int vt=mp[u][i];
        if(vt==v) continue;
        dfs(vt,u);
        dp[u]=max(dp[u],dp[vt]+1);
        if(vis[vt]) vis[u]=true;
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&r,&h,&m);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    int ans=inf;
    for(int i=1;i<=n;i++)
    {
        if(i==h || i==m)
        {
            memset(vis,false,sizeof(vis));
            dfs(i,-1);
            for(int j=0;j<mp[i].size();j++)
            {
                int v=mp[i][j];
                if(vis[v]) ans=min(ans,dp[v]);
            }
        }
        else
        {
            dfs(i,-1);
            ans=min(ans,dp[i]);
        }
    }
    printf("%d\n",ans);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值