CSU 1010 water drinking
来源:互联网 发布:腾讯视频 网络连接异常 编辑:程序博客网 时间:2024/04/30 03:07
1010: Water Drinking
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 224 Solved: 72
[Submit][Status][Web Board]
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
10 150 20 11 42 33 551 30 20 10 44 5
Sample Output
14 2本题考查的是并查集。可是现在还不懂那个,就用的邻接链表图,虽然很笨,但索性最后AC了。。。需要注意的是RUNTIME ERROR: (1)不能用while(true)代替while(scan.hasNext()) (2)Java好像不能像C/C++那样提前定义足够大的数组,要尽量根据读入参数确定数组长度import java.util.ArrayList;import java.util.Scanner;public class Main {private LinkNode first;public Main(){}public Main(int a){first=new LinkNode();first.root=a;}public void Insert(int a){if(first.next==null){first.next=new LinkNode(a);}else{LinkNode temp=first.next;first.next=new LinkNode(a);first.next.next=temp;}}public static int min(int[] a){int m;m=a[0];for(int temp:a){if(temp<m){m=temp;}}return m;}public static void main(String[] args) {// TODO Auto-generated method stubScanner scan=new Scanner(System.in);while(scan.hasNext()){int times=scan.nextInt();Main[] link=new Main[times+1];for(int i=0;i<link.length;i++){link[i]=new Main(i);}for(int i=0;i<times;i++){int a=scan.nextInt();int b=scan.nextInt();link[a].Insert(b);}ArrayList<Integer> num=new ArrayList<Integer>();LinkNode temp=link[0].first;while(temp.next!=null){num.add(temp.next.root);temp=temp.next;}int len=num.size();int[] endnum=new int[len];int[] count=new int[len];for(int i=0;i<len;i++){int f=num.get(i);LinkNode t=link[f].first;int countindex=0;while(t.next!=null){t=link[t.next.root].first;countindex++;}endnum[i]=t.root;count[i]=countindex;}int closest=min(count);ArrayList<Integer> c=new ArrayList<Integer>();for(int i=0;i<len;i++){if(count[i]==closest){c.add(endnum[i]);}}int clen=c.size();int[] result=new int[clen];for(int i=0;i<clen;i++){result[i]=c.get(i);}System.out.println(min(result));}}}class LinkNode{public int root;public LinkNode next;public LinkNode(){}public LinkNode(int a){root=a;next=null;}}
- CSU 1010 water drinking
- csu oj 1010 Water Drinking(BFS)
- COJ-1010-Water Drinking
- Water Drinking
- CSUOJ 1010 Water Drinking(BFS)
- CSU1010-Water Drinking-BFS
- Is Your Drinking Water Giving You Diabetes?
- Water --- CSU 1550: Simple String
- CSU 1320 Scoop water【卡特兰数】
- CSU-1010
- Soft Drinking
- 数学 ( 卡特兰数 )——Scoop water ( CSU 1320 )
- 文章标题 CSU 1838: Water Pump (单调栈)
- CSU
- CSU
- CSU
- CSU
- CSU
- Java synchronized详解
- <模电学习1>Multisim 12.0 搭建并仿真51单片机最小系统
- 信息检索:搜索引擎---怎样量化评价搜索引擎的结果质量
- 初始UML
- hdu 1426 Sudoku Killer【深度优先搜索】
- CSU 1010 water drinking
- 使用java的JavaCompiler模拟Proxy自动编译
- 仿ViewPager
- FPGA之ARM内核的板级仿真方案续
- leetcode - Merge Intervals
- 电路原理图基础
- icns图标集的制作 相关命令(iconutil)
- 三种巧妙方法判断机器是大端模式还是小端模式
- document.body.scrollTop用法