Water Drinking

在一片沙漠中,骆驼们排队饮水,通过输入的队列关系,找到离水源最近的队尾骆驼。利用并查集或广度优先搜索算法解决此问题。

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



Description

The Happy Desert is full of sands. There is only a kind of animal called camel living on the Happy Desert. ‘Cause they live here, they need water here. Fortunately, they find a pond which is full of water in the east corner of the desert. Though small, but enough. However, they still need to stand in lines to drink the water in the pond.

Now we mark the pond with number 0, and each of the camels with a specific number, starting from 1. And we use a pair of number to show the two adjacent camels, in which the former number is closer to the pond. There may be more than one lines starting from the pond and ending in a certain camel, but each line is always straight and has no forks.

Input

There’re multiple test cases. In each case, there is a number N (1≤N≤100000) followed by N lines of number pairs presenting the proximate two camels. There are 99999 camels at most.

Output

For each test case, output the camel’s number who is standing in the last position of its line but is closest to the pond. If there are more than one answer, output the one with the minimal number.

Sample Input

1
0 1
5
0 2
0 1
1 4
2 3
3 5
5
1 3
0 2
0 1
0 4
4 5

Sample Output

1
4
2

题意:
沙漠中有很多骆驼和一个池塘,0表示池塘,1-N表示骆驼,输入的两个数表示两只骆驼,其中前面的那一头靠近池塘,所有的骆驼队列不交叉不相连,求站在队尾但是离水井最近的骆驼编号
知识点:
并查集变形
#include<stdio.h>
#include<string.h>
#define maxn 100010
int pre[maxn], dis[maxn];
//pre 数组代表当前骆驼的前一个骆驼, dis 数组代表当前骆驼到水井的距离
int main()
{
    int n;
    while(scanf("%d", &n) != EOF){
        int x, y, i, ans, mind;
        memset(dis, 0, sizeof(dis));
        //父节点初始化 并查集必备
        for(i = 0; i < n; i++)
            pre[i] = i;
        for(i = 0; i < n; i++){
            scanf("%d %d", &x, &y);
            pre[y] = x;
            dis[x] = 1;
        }
        for(mind = n + 1, ans = i = 0; i <= n; i++){//注意:mind的初始值要大于n
            //如果dis[i]为0 即该节点无子节点
            if(!dis[i]){
                x = i; //x 是一个临时变量
                //如果这个节点有父节点 递归求出距离
                while(pre[x] != x){
                    x = pre[x];
                    dis[i]++;
                }
                //找出最大距离所在的骆驼
                if(!x && dis[i] < mind){
                    mind = dis[i];
                    ans = i;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
采用bfs
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define M 100005
#define INF 1<<29
#define CLS(x,v)  memset(x,v,sizeof(x))
int right[M];
int head[M];
void link(int x,int y){right[x]=y;}
int main()
{
    int n,cnt;
    int a,b;
    while(~scanf("%d",&n))
    {
        CLS(right,-1);
        cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            if(a==0)head[cnt++]=b;
            else link(a,b);
        }
        int ans=n,flag=1;
        while(flag)
        {
           for(int i=0;i<cnt;i++)
           {
               //看哪个树枝最短,如果有多个,则取多个的最小
               if(right[head[i]]==-1)
               {
                   ans=min(ans,head[i]);
                   flag=0;
               }
               head[i]=right[head[i]];
           }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值