真能扯啊,同样的题目出了两遍。
package info.frady.adv;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
/**
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 Excel {
static NodeExcel2[] nodes;
static boolean[] marked;
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("C:\\Users\\86153\\Desktop\\SW\\excel\\系统判分用测试用例.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次关联
int C=Integer.parseInt(st.nextToken());//M次关联
nodes=new NodeExcel2[N+1];
marked=new boolean[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));
}
int count=0;
for (int j = 0; j <C ; j++) {
st=new StringTokenizer(reader.readLine());
int qq=Integer.parseInt(st.nextToken());
//count=count+processBFS(qq);
processBFS(qq);
}
for (int j = 1; j <marked.length ; j++) {
if(marked[j]){
count++;
}
}
System.out.printf("#%d %d\n",(i+1),count);
}
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();
marked[index]=true;
//if(index!=start){
count++;
// }
for (int sindex:nodes[index].sonList) {
list.add(sindex);
}
}
return count;
}
}
class NodeExcel{
int index;
List<Integer> sonList;
public NodeExcel(int index){
this.index=index;
this.sonList=new ArrayList<>();
}
}