SW练习_POJ2367_拓扑排序

正确的版本

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main{
    static int[] in;
    static int[] out;
    static int[] dis;
    static List<Integer>[] f;//f[i].get(j) 表示第i个节点的第j个连接点的编号
    static LinkedList<Integer> list=new LinkedList<Integer>();//保存入度为0的节点

    static List<Integer> res=new ArrayList<Integer>();
    public static void main(String agrs[]) throws Exception{
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(reader.readLine());
        int N=Integer.parseInt(st.nextToken());//点的个数
        in=new int[N+1];
        out=new int[N+1];
        dis=new int[N+1];
        f=new List[N+1];
        for (int i = 0; i <f.length ; i++) {
            f[i]=new ArrayList<Integer>();
            f[i].add(0);
        }
        for (int i = 1; i <=N ; i++) {
            st=new StringTokenizer(reader.readLine());
            int c=Integer.parseInt(st.nextToken());
            while(c!=0){
                in[c]++;
                out[i]++;
                f[i].add(c);
                c=Integer.parseInt(st.nextToken());
            }
        }
        for (int i = 1; i <=N ; i++) {//将入度为0的加入进来
            if(in[i]==0){
                list.add(i);
            }
        }
        while (!list.isEmpty()){
            //Collections.sort(list);//让序号小的排到前面
            int c=list.pop();
            res.add(c);
            for (int i = 1; i <f[c].size() ; i++) {//将这个点的关联节点 入度 -1
                int to=f[c].get(i);
                in[to]--;
                if(in[to]==0){
                    list.add(to);
                    dis[to]=dis[c]+1;
                }
            }
        }

        for (int i = 0; i < res.size(); i++) {
            System.out.print(res.get(i)+" ");
        }





        reader.close();


    }
}

以下是开始学习时候,写的版本

这个题目,比较水,有些地方我觉得还需要优化,竟然AC了

import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main{

    public static void main(String[] args) throws Exception{
        BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(reader.readLine());
        List<Integer> res=new ArrayList<Integer>();
        Map<Integer,Integer> degreeMap=new HashMap<Integer,Integer>();//保存每个点的度
        List[] sonList=new ArrayList[N+1];//保存每个点的连接节点,  一共7个节点
        for (int i = 0; i <sonList.length; i++) {
            sonList[i]=new ArrayList();
            degreeMap.put(i,0);
        }
        for (int i =1; i <=N; i++) {
            String[] str=reader.readLine().split(" ");
            int from=i;
            for (int j = 0; j <str.length ; j++) {
                int to=Integer.parseInt(str[j]);
                if(to!=0){

                    int weight= degreeMap.containsKey(to) ? degreeMap.get(to):0;
                    weight++;
                    degreeMap.put(to,weight);//将到达节点的入度+1
                    sonList[from].add(to);//将到达节点加入到 开始节点的 连接信息中
                }
            }

        }

        LinkedList<PointTuopu> pList=new LinkedList<PointTuopu>();
        for (int i = 1; i <sonList.length ; i++) {

            PointTuopu p=new PointTuopu(i,degreeMap.get(i),sonList[i]);
            pList.add(p);
        }
        Collections.sort(pList);//将节点信息进行排序
        while(!pList.isEmpty()){
            PointTuopu p=pList.poll();//弹出第一个节点
            res.add(p.point);
            // 将相连的点的出度-1
            for ( Integer s:p.slist ) {//s是当前节点相邻的节点,这些节点的入度都需要减去1
                for (PointTuopu pt:pList) {
                    if(pt.point==s){
                        pt.degree--;
                    }
                }

            }
            Collections.sort(pList);
        }
        //System.out.println(res);
        for (int i = 0; i < res.size(); i++) {
            System.out.print(res.get(i)+" ");
        }
    }
}
class PointTuopu implements Comparable<PointTuopu>{
    int point;//保存当前节点的id
    int degree;//保存当前节点的入度
    List<Integer> slist=new ArrayList<Integer>();//保存当前节点的连接节点

    public PointTuopu() {
    }

    public PointTuopu(int point, int degree, List<Integer> slist) {
        this.point = point;
        this.degree = degree;
        this.slist = slist;
    }

    @Override
    public int compareTo(PointTuopu o) {
        return this.degree!=o.degree ? this.degree-o.degree :this.point-o.point;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值