Files
python/TangDou/XiTi/1407.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

45 lines
1008 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}