Is It A Tree?
Is It A Tree?
https://ac.nowcoder.com/acm/problem/105905
用什么并查集啊!!
树的定义白学了嘛!!!
根节点没有入度,其他节点有且仅有一个入度(唯一的父亲)
特判树没有节点的时候,这个很坑
#include <iostream> #include <algorithm> #include <map> using namespace std; map<int,int>mp; int n,m,in[10000009],id,k=1,l,r; int main() { while( cin >> l >> r ) { if( l+r==-2 ) break; if( l+r==0 ) { int q=0,w=0,flag=0; for(int i=1;i<=id;i++) { if( in[i]==1 ) q++; if( in[i]==0 ) w++; } if( w==1&&q==id-1 ) flag=1; if( id==0 ) flag=1; if( flag ) cout << "Case " << k << " is a tree.\n"; else cout << "Case " << k << " is not a tree.\n"; for(int i=1;i<=id;i++) in[i]=0; mp.clear(); id=0; k++; } else { if( !mp[l] ) mp[l]=++id; if( !mp[r] ) mp[r]=++id; in[ mp[r] ]++; } } }