SW练习_ADV_excel2_BFS

 这是一个简单的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<>();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值