题目有点难度,用邻接表即可,
没有什么难点,上code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> adj[105];
int nNode, nEdge;
bool hasfather[105];
int main() {
cin >> nNode >> nEdge;
for (int i = 1; i <= nEdge; i ++) {
int from, to;
cin >> from >> to;
adj[from].push_back(to);
hasfather[to] = true;
}
int root;
for (int i = 1; i <= nNode; i ++) {
if (!hasfather[i]) {
root = i;
break;
}
}
cout << root << endl;
int imax = 1;
for (int i = 2; i <= nNode; i ++) {
if (adj[i].size() > adj[imax].size()) {
imax = i;
}
}
cout << imax << endl;
sort(adj[imax].begin(), adj[imax].end());
for (int i = 0; i < adj[imax].size() ; i ++)
cout << adj[imax][i] << " ";
return 0;
}