解题思路:
注意事项:
参考代码:
package Year_2017;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class 发现环 {
private static int index = 0;
private static int[] arr,visited;
private static Map<Integer,ArrayList<Integer>> map = new HashMap<Integer,ArrayList<Integer>>();
private static ArrayList<Integer> res = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int n = sc.nextInt();
arr = new int [n+1];
visited = new int [n+1];
for(int i=1;i<=n;i++) {
//给结点编一个默认号
arr[i] = i;
}
for(int i=1;i<=n;i++) {
int u_head = sc.nextInt();
int v_tail = sc.nextInt();
if(map.get(u_head) == null) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(v_tail);
map.put(u_head, list);
}else { //如果u_head不为空
ArrayList<Integer> list = map.get(u_head);
list.add(v_tail);
map.put(u_head, list);
}
//================================================================
if(map.get(v_tail) == null) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(u_head);
map.put(v_tail, list);
}else { //如果u_head不为空
ArrayList<Integer> list = map.get(v_tail);
list.add(u_head);
map.put(v_tail, list);
}
//================================================================
//构造一个find,查看当前的值,有没有出现过
int x = find(u_head) ,y = find(v_tail);
if(x != y) {
arr[x] = y;
}else {
index = u_head;
dfs(u_head);
}
}//for(int i=1;i<=n;i++)
Collections.sort(res);
for(int i=0;i<res.size();i++) {
System.out.print(res.get(i));
if(i<res.size()-1) {
System.out.print(" ");
}else {
System.out.println();
}
}
}
sc.close();
}
private static boolean dfs(int x) {
// TODO Auto-generated method stub
if(visited[x] == 1) {
if(x==index) {
return true;
}
return false;
}
visited[x] = 1;
for(int i=0;i<map.get(x).size();i++) {
int v = map.get(x).get(i);
if(dfs(v)) {
res.add(v);
return true;
}
}
return false;
}
private static int find(int x) {
// TODO Auto-generated method stub
if(x == arr[x]) {
return x;
}else {
arr[x] = find(arr[x]);
return arr[x];
}
}
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:851 |
C语言训练-列出最简真分数序列* (C语言代码)浏览:604 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:500 |
【计算球体积】 (C语言代码)浏览:1101 |
The 3n + 1 problem (C语言代码)浏览:548 |
1118(求助_已解决)浏览:329 |
C语言程序设计教程(第三版)课后习题10.5 (C语言代码)浏览:534 |
钟神赛车 (C语言代码)浏览:592 |
1134题解(求分析)浏览:723 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:607 |