noj 吝啬的国度
来源:互联网 发布:淘宝店描述在哪里 编辑:程序博客网 时间:2024/04/28 06:39
建立一个双向的图,从出发点遍历一遍用数组存储上一个顶点即可。
import java.util.*;public class Main {static List<Integer> list;static Map<Integer,List<Integer>> map;static void store(int a,int b){list=map.get(a);if(list==null) list=new ArrayList<Integer>();list.add(b);map.put(a, list);}static void before(int n,int s,Map<Integer,List<Integer>> map){int pre[]=new int[n+1];boolean vis[]=new boolean[n+1];int key=s;vis[key]=true;List<Integer> list=map.get(s);for(int k=0;k<list.size();k++) pre[list.get(k)]=key;Queue<List<Integer>> q=new LinkedList<List<Integer>>();q.offer(list);while(!q.isEmpty()){List<Integer> lis=q.poll();for(int i=0;i<lis.size();i++){key=lis.get(i);if(!vis[key]){vis[key]=true;List<Integer> value=map.get(key);q.offer(value);for(int k=0;k<value.size();k++) {int t=value.get(k);if(!vis[t])pre[t]=key;}}}}pre[s]=-1;for(int i=1;i<n;i++) System.out.print(pre[i]+" ");System.out.println(pre[n]);}public static void main(String[] args){Scanner cin=new Scanner(System.in);int m=cin.nextInt();while(m--!=0){map=new HashMap<Integer,List<Integer>>();int n=cin.nextInt();int s=cin.nextInt();for(int i=1;i<n;i++){int a=cin.nextInt();int b=cin.nextInt();store(a,b);store(b,a);}before(n,s,map);}}}
- noj 吝啬的国度
- NOJ 20 吝啬的国度
- nyoj_20 吝啬的国度
- 吝啬的国度
- 吝啬的国度
- NYOJ 吝啬的国度
- NYOJ 吝啬的国度
- 吝啬的国度
- nyoj20(吝啬的国度)
- 吝啬的国度
- 吝啬的国度
- 【搜索】吝啬的国度
- 吝啬的国度
- NY20 吝啬的国度
- NYOJ20-吝啬的国度
- 吝啬的国度
- 吝啬的国度
- 吝啬的国度
- VB 函数入口点选择
- _tcstol 字符串到数字的转化
- TCP与UDP
- Ajax的来源及工作原理
- windows sdk编程系列文章 ---- 实现smc
- noj 吝啬的国度
- 网页锚点的的使用
- GUI系统之SurfaceFlinger(12)VSync信号的产生和处理
- Post和Get的区别
- “AS3.0高级动画编程”学习:第四章 寻路(AStar/A星/A*)算法 (上)
- BFS | 3278 | Catch That Cow
- DB2/Oracle/SQL server判断某个字符串或单个字符在源字符串中出现的位置
- 常见android手机分辨率(xxhdpi,xhdpi)
- C# winform DataGridView 常见属性