3607 - 树的数量
输入森林中的结点关系,统计森林中树的数量,输出树的根。
Input
第一行:n:结点数量;k:边数;(n,k<=100)
以下k行:每行两个结点编号:i,j:i是j的父结点(I,j<=100)。
Output
输出:
第一行:树的数量。
第二行:依次输出森林中树的根结点编号(从小到大)。
Examples
Input
9 7 1 2 2 3 4 6 4 5 7 8 9 1 9 4
Output
2 7 9
Solution C++
#include<cstdio> #include<queue> using namespace std; int s,n,m,f[101],d[101]; queue<int>q; int main() { int i,a,b,fa,fb; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) f[i]=i,d[i]&=0; for(i=0;i<m;i++) { scanf("%d%d",&a,&b); f[b]=a; } for(s=0,i=1;i<=n;i++) if(i==f[i]) s++,q.push(i); printf("%d\n%d",s,q.front()); q.pop(); while(!q.empty()) printf(" %d",q.front()),q.pop(); } return 0; }