提交时间:2024-01-23 16:31:17

运行 ID: 125174

#include<bits/stdc++.h> using namespace std; int n, m, tree[1005]; int main(){ int root, maxr, sum = 0, maxx = 0; cin >> n >> m; for (int i = 1; i <= m; i++){ int x, y; cin >> x >> y; tree[y] = x; } for (int i = 1; i <= n; i++){ if (tree[i] == 0) { root = i; break; } } for (int i = 1; i <= n; i++){ sum = 0; for(int j = 1; j <= n; j++){ if (tree[j] == i) sum ++; } if (sum > maxx){ maxx = sum; maxr = i; } } cout << root << " " << maxr << endl; }