这是一个简单的BFS题目,谁给标注了一个unionFind
写了一个unionFind,在uiond的时候,没法处理啊
package info.frady.adv;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
/**
2
10 9
1 6
1 9
7 10
7 5
10 8
6 2
9 2
2 4
2 5
10 8
1 2
1 9
7 10
7 5
10 8
6 2
9 2
2 4
#1 5
#2 3
*/
public class Excel2 {
static NodeExcel2[] nodes;
public static void main(String[] args) throws Exception{
//System.setIn(new FileInputStream("C:\\Users\\86153\\Desktop\\SW\\excel2\\sample_input.txt"));
System.setIn(new FileInputStream("C:\\Users\\86153\\Desktop\\SW\\excel2\\eval_input.txt"));
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(reader.readLine());
int T=Integer.parseInt(st.nextToken());//测试用例的个数
for (int i = 0; i <T ; i++) {
st=new StringTokenizer(reader.readLine());
int N=Integer.parseInt(st.nextToken());//N个点
int M=Integer.parseInt(st.nextToken());//M次关联
nodes=new NodeExcel2[N+1];
for (int j = 0; j <nodes.length ; j++) {
nodes[j]=new NodeExcel2(j);
}
for (int j = 0; j <M ; j++) {
st=new StringTokenizer(reader.readLine());
int start=Integer.parseInt(st.nextToken());
int end=Integer.parseInt(st.nextToken());
nodes[start].sonList.add(end);
//System.out.println(Arrays.toString(pid));
}
System.out.printf("#%d %d\n",(i+1),processBFS(1));
}
reader.close();
}
public static int processBFS(int start){
LinkedList<Integer> list=new LinkedList<>();
list.add(start);
int count=0;
while(!list.isEmpty()){
int index=list.pop();
if(index!=1){
count++;
}
for (int sindex:nodes[index].sonList) {
list.add(sindex);
}
}
return count;
}
}
class NodeExcel2{
int index;
List<Integer> sonList;
public NodeExcel2(int index){
this.index=index;
this.sonList=new ArrayList<>();
}
}