AcWing 1254. 找树根和孩子
原题链接
简单
作者:
哈哈哈hh
,
2020-06-26 22:46:35
,
所有人可见
,
阅读 586
写的有点乱 哈哈,但是有点简单
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1110;
int da[N];
vector<vector<int>> ch(N);
int g[N][N];
vector<int> A;
int n,k;
int main()
{
cin >> n >> k;
int max_ = 0;
int max_index = 0;
for(int i = 1; i <= k; i++)
{
int a,b;
cin >> a >> b;
A.push_back(a);
g[a][b] = 1;
da[b]++;
ch[a].push_back(b);
if(max_ < ch[a].size())
{
max_index = a;
max_ = ch[a].size();
}
}
int root = 1;
for(int i = 0; i < A.size(); i++)
{
if(da[A[i]] == 0)
{
root = A[i];
break;
}
}
cout << root << endl;
cout << max_index << endl;
sort(ch[max_index].begin(),ch[max_index].end());
for(int i = 0; i < ch[max_index].size(); i++)
{
cout << ch[max_index][i] << " ";
}
cout <<endl;
//找到root
}