csu1010: Water Drinking

/*

本题的题意:

沙漠中有很多骆驼和一个池塘,0表示池塘,1-N表示骆驼,
输入的两个数表示两只骆驼,其中前面的那一头靠近池塘,
所有的骆驼队列不交叉不相连,求站在队尾但是离水井最近的骆驼编号

经过分析最后还是要先构造一个树,然后寻找离0最近的一个点,当结果是相等的级别的时候将结果返回最小的那个值

*/

参考代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define maxn 100010
 4
 5 int pre[maxn], dis[maxn];
 6 //pre 数组代表当前骆驼的前一个骆驼, dis 数组代表当前骆驼到水井的距离
 7 int main()
 8 {
 9     int n;
10     while(scanf("%d", &n) != EOF){
11         int x, y, i, ans, mind;
12         memset(dis, 0, sizeof(dis));
13         //父节点初始化 并查集必备
14         for(i = 0; i < n; i++)
15             pre[i] = i;
16         for(i = 0; i < n; i++){
17             scanf("%d %d", &x, &y);
18             pre[y] = x;
19             dis[x] = 1;
20         }
21         for(mind = n + 1, ans = i = 0; i <= n; i++){//注意:mind的初始值要大于n
22             //如果dis[i]为0 即该节点无子节点
23             if(!dis[i]){
24                 x = i; //x 是一个临时变量
25                 //如果这个节点有父节点 递归求出距离
26                 while(pre[x] != x){
27                     x = pre[x];
28                     dis[i]++;
29                 }
30                 //找出最大距离所在的骆驼
31                 if(!x && dis[i] < mind){
32                     mind = dis[i];
33                     ans = i;
34                 }
35             }
36         }
37         printf("%d\n", ans);
38     }
39     return 0;
40 }

csu1010: Water Drinking

时间: 2024-10-26 05:19:45

csu1010: Water Drinking的相关文章

CSUOJ 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.

2019ACM-ICPC沈阳网络赛-K-Guanguan&#39;s Happy water(思维+暴力)

Guanguan's Happy water 4000ms 262144K Rather than drinking happy water, Guanguan loves storing happy water. So he bought a refrigerator and stored a_iai? bottles of cola into it every day. When the storage is finished on the kk-th day, the refrigerat

Water Problem

water problem 发布时间: 2015年10月10日 15:34   时间限制: 1000ms   内存限制: 256M 描述 题意很简单 给你N个数, Q个查询 每次查询给你一个区间[L, R] 你要找出 [L, R] 这个区间里面取模M后的最大值. 输入 第一行一个T,表示测试数据组数.第二行两个整数N, M (1<=N<=10^5, 1<=M<=10^9).第三行给你N个整数 整数范围在1到10^9之间.第四行给你一个整数Q. ( 1<=Q<=10^5)

leetcode -eleven:Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

Container With Most Water ——解题笔记

[题目] Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a con

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

[poj 2331] Water pipe ID A*迭代加深搜索(dfs)

Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description The Eastowner city is perpetually haunted with water supply shortages, so in order to remedy this problem a new water-pipe has been built. Builders s

407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note: Both m and n are less than 110. The height of each unit cell is greater th

LeetCode11:Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe