有向树

Description

判断一个有向图是否为树,一般需要满足以下两个个条件:

1. 有且仅有一个节点,为根节点,没有任何有向边指向该节点

2. 除了根节点的任意节点,有且仅有一条有向边指向这个节点

Input

输入首先为边的数目,然后包含一串数字,数字成对出现,前一个数字表示有向边的开始节点,后一个数字表示有向边的终点。

Output

你的程序需要首先判断是否为树,如果是树,则输出tree,并输出树的高度,用空格隔开

如果不是树,则输出not tree,并输出不满足的条件1或者2,用空格隔开。如果两个条件都不满足,输出not tree 1即可

Sample Input

56 8 5 3 5 2 6 4 5 6

Sample Output

tree 3

HINT

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#define pa pair<int, int>
#define LL long long
  
using namespace std;
 
int m, x, y, count0, count1, maxn, n, depth, tot, root;
int first[100000], d[100000];
bool vi[100000];
struct Edge {
    int to, nxt;
} e[200000];
 
 
void Add_Edge(int x, int y) {
    e[++tot].to = y; e[tot].nxt = first[x]; first[x] = tot;
}
 
void dfs(int x, int d) {
    depth = max(depth, d);
    for (int i = first[x]; i; i = e[i].nxt) {
        dfs(e[i].to, d+1);
    }
}
 
int main () {
    scanf("%d", &m);
    maxn = 0;
    tot = 0;
    memset(vi, 0, sizeof(vi));
    memset(first, 0, sizeof(first));
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &x, &y);
        Add_Edge(x, y);
        vi[x] = 1;
        vi[y] = 1;
        d[y]++;
        maxn = max(maxn, max(x, y));
    } 
     
    count0 = count1 = 0;
    for (int i = 0; i <= maxn; i++)
        if (vi[i]) {
            n++;
            if (d[i] == 0) {
                count0++;
                root = i;
            }
            if (d[i] == 1) count1++;
        }
     
    if (count0 != 1) puts("not tree 1");
    else if (count1 != n-1) puts("not tree 2");
    else {
        depth = 0;
        dfs(root, 1);
        printf("tree %d\n", depth);
    }
 
     
    return 0;
}
 
/**************************************************************
    Problem: ****
    User: ****
    Language: C++
    Result: Accepted
    Time:4 ms
    Memory:3724 kb
****************************************************************/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值