题意:
思路:
代码:
#include#include #include #include using namespace std;const int N=10005;vector Adj[N];bool vis[N];int n;void dfs1(int v){ vis[v]=true; for(auto u:Adj[v]) if(!vis[u]) dfs1(u);} void dfs2(int v,int h,int& maxHeight,set & st){ vis[v]=true; if(h>maxHeight){ maxHeight=h; st.clear(); st.insert(v); }else if(h==maxHeight){ st.insert(v); } for(auto u:Adj[v]){ if(!vis[u]) { dfs2(u,h+1,maxHeight,st); } }}int main(){ int u,v; scanf("%d",&n); for(int i=1;i 1){ printf("Error: %d components\n",cnt); }else{ set st1,st2,st; int maxHeight=0; fill(vis,vis+N,false); dfs2(1,1,maxHeight,st1); maxHeight=0; fill(vis,vis+N,false); dfs2(*(st1.begin()),1,maxHeight,st2); //求并集 set_union(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin())); for(auto it:st) printf("%d\n",it); } return 0;}