吝啬的国度_数组存储
来源:互联网 发布:linux telnet连接失败 编辑:程序博客网 时间:2024/04/29 23:41
package 搜索;
import java.util.Scanner;
/*
* 采用数组存储,类似深搜。效率优
*/
public class 吝啬的国度_数组存储 {
static int result[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
int m = sc.nextInt();
int s = sc.nextInt();
result = new int[m + 1];
int i, j, a, b;
for (i = 0; i < m - 1; i++) {
a = sc.nextInt();
b = sc.nextInt();
if (result[a] != 0) {
update(b);
result[b] = a;
} else {
result[a] = b;
}
}
update(s);
result[s] = -1;
for (i = 1; i <= m; i++) {
System.out.print(result[i] + " ");
}
}
}
private static void update(int b) {
int i = result[b];
if (i != 0) {//注意这里是if
update(i);
result[i] = b;
}
}
}
import java.util.Scanner;
/*
* 采用数组存储,类似深搜。效率优
*/
public class 吝啬的国度_数组存储 {
static int result[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
int m = sc.nextInt();
int s = sc.nextInt();
result = new int[m + 1];
int i, j, a, b;
for (i = 0; i < m - 1; i++) {
a = sc.nextInt();
b = sc.nextInt();
if (result[a] != 0) {
update(b);
result[b] = a;
} else {
result[a] = b;
}
}
update(s);
result[s] = -1;
for (i = 1; i <= m; i++) {
System.out.print(result[i] + " ");
}
}
}
private static void update(int b) {
int i = result[b];
if (i != 0) {//注意这里是if
update(i);
result[i] = b;
}
}
}
0 0
- 吝啬的国度_数组存储
- 吝啬的国度_链表_数组存储
- 吝啬的国度_链表
- nyoj20吝啬的国度_广搜
- nyoj_20 吝啬的国度
- 吝啬的国度
- noj 吝啬的国度
- 吝啬的国度
- NYOJ 吝啬的国度
- NYOJ 吝啬的国度
- 吝啬的国度
- nyoj20(吝啬的国度)
- 吝啬的国度
- 吝啬的国度
- 【搜索】吝啬的国度
- 吝啬的国度
- NY20 吝啬的国度
- NYOJ20-吝啬的国度
- SpringMVC入门之八:使用Apache Tiles视图定义布局
- 设计模式(4)-单例模式
- 图像有用区域_撞墙法
- [2015-12-29]position详解
- 支付宝被骗怎么办?
- 吝啬的国度_数组存储
- mongoDB使用方法
- 值得推荐的C/C++框架和库
- 吝啬的国度_链式前向星
- 解决mysql服务无法启动问题
- 吝啬的国度_链表_数组存储
- 关于softmax损失函数的推导
- 吝啬的国度_链表
- [2016-05-06]CSS之float详解