45 lines
1008 B
C++
45 lines
1008 B
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
|
||
//结点数<=100
|
||
int n, m, tree[101] = {0};
|
||
|
||
int main() {
|
||
int i, x, y, root, maxroot, sum, j, Max = 0;
|
||
cin >> n >> m;//节点和边的数目
|
||
for (i = 1; i <= m; i++) {
|
||
cin >> x >> y;
|
||
tree[y] = x; //y是x的孩子
|
||
}
|
||
//找出树根
|
||
for (i = 1; i <= n; i++) {
|
||
// i的父亲节点为0,即没有父亲节点
|
||
if (tree[i] == 0) {
|
||
root = i;
|
||
break;
|
||
}
|
||
}
|
||
//找孩子最多的节点maxroot
|
||
for (i = 1; i <= n; i++) {
|
||
sum = 0;
|
||
for (j = 1; j <= n; j++) {
|
||
if (tree[j] == i) sum++;
|
||
}
|
||
if (sum > Max) {
|
||
Max = sum;
|
||
maxroot = i;
|
||
}
|
||
}
|
||
/*
|
||
cout << root << endl;
|
||
cout << maxroot << endl;
|
||
|
||
//maxroot的孩子
|
||
for (i = 1; i <= n; i++) {
|
||
if (tree[i] == maxroot) {
|
||
cout << i << " ";
|
||
}
|
||
}*/
|
||
return 0;
|
||
} |