邻接表+树的遍历

44 篇文章 1 订阅
33 篇文章 0 订阅

题目描述:

给定一颗树,树中包含 n 个结点(编号 1∼n)和 n−1 条有向边。

输入格式

第一行包含整数 n,表示树的结点数。 

接下来 n−1行,每行包含两个整数 a 和 b,表示点 a 和点 b 之间存在一条边。 

输出格式:

以邻接表的形式输出各个点之间的关系

数据范围

1 ≤ n ≤ 10^5

输入样例

9
1 2
1 7
1 4
2 8
2 5
4 3
3 9
4 6

输出树的结构:

思路:

根据样例模拟建树

用邻接表存储这颗树:

static int e[] = new int[N]; // 存边连接的第二个点的编号,即表头的临接点编号
static int ne[] = new int[N]; // 记录每个表头的下一个结点
static int h[] = new int[N]; // 存每一个表头,为每一个点都开一个表头,都初始化为-1
static int idx; // 存每个表头临接点的下标

static void add(int a, int b) 
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}

 考虑1号点:(模拟建表)

 

AC代码:

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e4 + 10;

    static int e[] = new int[N]; // 存边连接的第二个点的编号,即表头的临接点编号
    static int ne[] = new int[N]; // ne[i]的值是下标,是下标为i的节点的next节点的下标。
    static int h[] = new int[N]; // 存每一个表头的下一元素,为每一个点都开一个表头,都初始化为-1
    static int idx; // 存每个表头临接点的下标

    static void add(int a, int b)
    {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx ++;
    }

    public static void main(String[] args ) throws IOException
    {
        Arrays.fill(h,-1);
        int n  = rd.nextInt(); // n个节点

        int k = n - 1;
        while(k -- > 0)
        {
            int a = rd.nextInt(),b = rd.nextInt();
            add(a,b);
        }

        for(int i = 1 ; i <= n ; i ++) // 枚举每一个节点
        {
            pw.print(i + ":");
            for(int j = h[i] ; j != -1 ; j = ne[j])  // 遍历每一个点的邻接表,因为每个点都开了邻接表
            {
                pw.print("->" + e[j]); // 输出表头的邻接节点
            }
            pw.println();
        }
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class math
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }

    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }

    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }

        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());

        // 对因子排序(升序)
        Collections.sort(a);

        return a;
    }

    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

class Line implements Comparable<Line>
{
    double k; // 斜率
    double b; // 截距

    public Line(double k, double b)
    {
        this.k = k;
        this.b = b;
    }

    @Override
    public int compareTo(Line o)
    {
        if (this.k > o.k) return 1;
        if (this.k == o.k)
        {
            if (this.b > o.b) return 1;
            return -1;
        }
        return -1;
    }
}

class mqm
{
    int fa[] = new int[1005];
    void init()
    {
        for(int i = 1 ; i <= 1000 ; i ++)  fa[i] = i;
    }
    void merge(int x, int y) { fa[find(x)] = find(y); }
    int find(int x)
    {
        if(x != fa[x])  fa[x] = find(fa[x]);
        return fa[x];
    }
    boolean query(int x, int y) { return find(x) == find(y); }
}

 

 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

21RGHLY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值