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
****************************************************************/