要注意数据范围哟,由于是无向图,所以边的数量是点的数量的两倍!!!
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1000010;
int n;
int h[N],e[2*N],ne[2*N],idx;//边的数量是点的数量的两倍,即2*N;
bool st[N];
vector<int> ans;
void add(int a,int b)//这里是数组模拟链表的插入操作,用的是邻接表,也可使用vector容器做
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u)
{
st[u]=true;
int sum=1,res=0;//res 表示当删除这个节点时,剩下的联通块最大的数量
for(int i=h[u];i!=-1;i=ne[i])
{
int j=e[i];
if(!st[j])
{
int s=dfs(j);
res=max(res,s);
sum+=s;
}
}
res=max(res,n-sum);
if(res <= n/2) ans.push_back(u);
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n;
memset(h,-1,sizeof h);
for(int i=0;i<n-1;i++)
{
int a,b;
cin >> a >> b;
add(a,b),add(b,a);
}
dfs(1);
if(ans.size())
{
while(ans.size())
{
cout << ans.back() << endl;
ans.pop_back();
}
}
else cout << "NONE" << endl;
return 0;
}