CSUOJ 1010 Water Drinking(BFS)

来源:互联网 发布:全国人口普查数据查询 编辑:程序博客网 时间:2024/06/06 08:25

1010: Water Drinking


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
题目大意:在一个沙漠中有一个水池,一些骆驼在排队喝水。骆驼排成了很多队伍,队伍的起点都是水池,求排在队伍最后但是离水池最近的骆驼编号。

解题思路:建图然后BFS,用一个数组记录某个骆驼是否在队尾,最后比较距离,输出距离最近且编号最小的骆驼即可。

代码如下:

#include <bits/stdc++.h>#define INF 1e9using namespace std;const int maxn = 100005;int head[maxn],cnt,d[maxn],n;bool en[maxn];struct Edge{    int to,next;}edge[maxn];void init(){    cnt = 0;    memset(head,-1,sizeof(head));    memset(en,0,sizeof(en));}void add_edge(int from,int to){    edge[cnt].to = to;    edge[cnt].next = head[from];    head[from] = cnt++;}queue<int> que;void bfs(){    while(que.size()) que.pop();    for(int i = 0;i <= n;i++) d[i] = INF;    d[0] = 0;    que.push(0);    while(que.size()){        int u = que.front();        que.pop();        if(head[u] == -1){            en[u] = true;            continue;        }        for(int i = head[u];~i;i = edge[i].next){            int v = edge[i].to;            d[v] = d[u] + 1;            que.push(v);        }    }    return ;}int main(void){    int t,a,b;    while(cin >> n){        init();        for(int i = 0;i < n;i++){            cin >> a >> b;            add_edge(a,b);        }        bfs();        int minVal = INF;        int pos = INF;        for(int i = 1;i <= n;i++){            if(en[i] && minVal > d[i]){                minVal = d[i];                pos = i;            }        }        cout << pos << endl;    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大风号无法上传视频暂停服务怎么办 过了竞牌保证金交付时间怎么办 亚马逊产品上架后货物没到怎么办 工行企业网银证书过期了怎么办 海淘转运地址国家填错了怎么办 集装箱实重与申报重量不一样怎么办 微博复制的淘口令找不到了怎么办 买了移动手机不能用联通卡怎么办 移动手机用联通卡网速慢怎么办 移动手机插联通卡没反应怎么办 移动手机办了联通大王卡怎么办 qq被冻结但是有至尊宝怎么办 qq被冻结了有至尊宝怎么办 移动电话卡注销了里面的钱怎么办 罗麦的oa上经理喜报没截图怎么办 工行融e联登录密码忘了怎么办 融e借有额度秒拒怎么办 工行银行柜台办理融e借怎么办 地球末日生存破解版金币没了怎么办 手机被别人骗走了里面的微信怎么办 在微信里面被做微商的骗了钱怎么办 在qq上骗了人50怎么办 被3m多酶清洗液滴到皮肤怎么办 做3m赔了9万怎么办 在携程网上订的酒店发票怎么办 滴滴滴取消订单电话打不通怎么办 百度云容量2068g满了怎么办 淘宝电脑端描述图片间隔大怎么办 手机上下载游戏自动扣费怎么办 苹果手机下载游戏自动扣费怎么办 用手机账号登陆游戏换手机怎么办 在qq上被骗充q币怎么办 微信借钱后删了好友怎么办 财付通绑定了其它人的身份证怎么办 微信支付密码忘了没绑银行卡怎么办 微信没有绑银行卡忘记密码怎么办 威信解绑银行卡支付密码望了怎么办 买了个qq号绑定了财付通怎么办 淘宝买的东西下架了怎么办 微信红包充话费充错了怎么办 手机qq不能发红包或转账怎么办