这道题还是不难的,基本上只要思路正确,不用写多长时间。
#include<bits/stdc++.h> using namespace std; int m,n,a[1000],b[1000],num[10000],ant=0,u,k,l,x,y,maxx=0; int main() { // freopen("找树根和孩子.in","r",stdin); //freopen("找树根和孩子.out","w",stdout); cin>>n>>m; for(int i=1;i<=m;i++) { cin>>x>>y; a[y]=x; } for(int i=1;i<=n;i++) { if(a[i]==0) { u=i; break; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(a[j]==i) { ant++; } } if(ant>maxx) { maxx=ant; l=i; } ant=0; } cout<<u<<"\n"; cout<<l<<"\n"; for(int i=1;i<=n;i++) { if(a[i]==l) cout<<i<<" "; } return 0; }
转载于:https://www.cnblogs.com/5t2y0/p/9558865.html
