L - PC is for kicking

Otvio is a calm guy with a guilty pleasure: kicking PCs. Now he is participating in a programming competition where the computers are arranged as a tree, that is, the N PCs are connected by N - 1 cables and there is only one cable path from one PC to another.

Otvio wants to kick as many PCs as possible. To do so, he will walk along a cable path, kicking all the PCs in the path. He will start at PC a, the one he will use in the contest. However, to prevent getting caught, he will not pass by PCs he already kicked nor kick any PC twice.

Laerco, knowing Otvio’s guilty pleasure, wants to know the maximum number of PCs Otvio can kick satisfying the conditions above. Remember that Otvio will kick his own PC for sure.

Input
The first line of the input consist of two integers integer N and a (1 ≤ N, a ≤ 105), the number of PCs in the contest and the number of Otvio’s PC, in this order. Each of the next N - 1 lines contains two integers u and v (1 ≤ u, v ≤ N), indicating that there is a cable connecting PCs u and v.

Output
Output a single integer - the maximum number of PCs Otvio can kick.

Example
Input
5 2
1 2
1 3
2 5
3 4
Output
4

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
struct cnode
{
    int v;
    int next;
}edg[maxn << 1];
int head[maxn << 1];
int vis[maxn];
int tot;
void add(int u, int v)
{
    edg[tot].v = v;
    edg[tot].next = head[u];
    head[u] = tot++;
}
int dfs(int u)
{
    int ans = 1;
    vis[u] = 1;
    for(int i = head[u]; i != 1; i = edg[i].next)
    {
        int v = edg[i].v;
        if(vis[v] == 0)
        {
            ans = max(ans,dfs(v)+1);
        }
        
    }
    return ans;
}
int main()
{
    int n, s;
    scanf("%d %d",&n,&s);
    memset(head, -1, sizeof(head));
    for(int i = 1; i <= n - 1; i++)
    {
        int x, y;
        scanf("%d %d",&x,&y);
        add(x,y);
        add(y,x);
    }
    int z = dfs(s);
    printf("%d\n",z);
    return  0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值