1. 发现环
标题:发现环
小明的实验室有N台电脑,编号1~N。原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络。在树形网络上,任意两台电脑之间有唯一的路径相连。
不过在最近一次维护网络时,管理员误操作使得某两台电脑之间增加了一条数据链接,于是网络中出现了环路。环路上的电脑由于两两之间不再是只有一条路径,使得这些电脑上的数据传输出现了BUG。
为了恢复正常传输。小明需要找到所有在环路上的电脑,你能帮助他吗?
输入
第一行包含一个整数N。 以下N行每行两个整数a和b,表示a和b之间有一条数据链接相连。
对于30%的数据,1 <= N <= 1000 对于100%的数据, 1 <= N <= 100000, 1 <= a, b <= N
输入保证合法。
输出
按从小到大的顺序输出在环路上的电脑的编号,中间由一个空格分隔。
样例输入: 5 1 2 3 1 2 4 2 5 5 3
样例输出: 1 2 3 5
资源约定: 峰值内存消耗 < 256M CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0 注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。 注意: 所有依赖的函数必须明确地在源文件中 #include , 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
2. 解析
这道题我一开始想要用搜索暴力做,遍历每个点,找成环的那个连通块。 但这样肯定超时了,有十万个点 可以用并查集+dfs优化,但写起来麻烦了点
拓扑排序做这道题会比较简单,代码也很好写
3. 代码
import java
.io
.BufferedInputStream
;
import java
.io
.BufferedReader
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.io
.InputStreamReader
;
import java
.io
.PrintWriter
;
import java
.util
.ArrayDeque
;
import java
.util
.ArrayList
;
import java
.util
.StringTokenizer
;
public class Main {
static InputReader in
= new InputReader(new BufferedInputStream(System
.in
));
static PrintWriter out
= new PrintWriter(System
.out
);
static ArrayList
<Integer>[] adj
= new ArrayList[100010];
static int[] degree
= new int[100010];
static boolean[] vis
= new boolean[100010];
static int n
;
static void TopSort() {
ArrayDeque
<Integer> q
= new ArrayDeque<>();
for (int i
= 1; i
<= n
; i
++) {
if (degree
[i
] == 1) {
q
.offerLast(i
);
vis
[i
] = true;
}
}
while (!q
.isEmpty()) {
int t
= q
.pollFirst();
for (int i
= 0; i
< adj
[t
].size(); i
++) {
int s
= adj
[t
].get(i
);
degree
[s
]--;
if (degree
[s
] == 1) {
vis
[s
] = true;
q
.offerLast(s
);
}
}
}
}
public static void main(String
[] args
) {
n
= in
.nextInt();
for (int i
= 0; i
< n
; i
++) {
int a
= in
.nextInt();
int b
= in
.nextInt();
if (adj
[a
] == null
) {
adj
[a
] = new ArrayList<>();
}
if (adj
[b
] == null
) {
adj
[b
] = new ArrayList<>();
}
adj
[a
].add(b
);
adj
[b
].add(a
);
degree
[a
]++;
degree
[b
]++;
}
TopSort();
boolean flag
= true;
for (int i
= 1; i
<= n
; i
++) {
if (vis
[i
] == false) {
if (flag
) {
out
.print(i
);
flag
= false;
} else {
out
.print(" " + i
);
}
}
}
out
.flush();
out
.close();
}
public static class InputReader {
public static BufferedReader br
;
public static StringTokenizer tokenizer
;
public InputReader(InputStream stream
) {
br
= new BufferedReader(new InputStreamReader(stream
), 32768);
tokenizer
= null
;
}
public String
next() {
while(tokenizer
== null
|| !tokenizer
.hasMoreTokens()) {
try {
tokenizer
= new StringTokenizer(br
.readLine());
} catch (IOException e
) {
e
.printStackTrace();
}
}
return tokenizer
.nextToken();
}
public String
readLine() {
String s
= null
;
try {
s
= br
.readLine();
} catch (IOException e
) {
e
.printStackTrace();
}
return s
;
}
public int nextInt() {
return Integer
.parseInt(next());
}
public Long
nextLong() {
return Long
.parseLong(next());
}
public Double
nextDouble() {
return Double
.parseDouble(next());
}
}
}