题意:

思路:


%2C%E8%BF%98%E8%A6%81%E6%B3%A8%E6%84%8F%E7%A9%BA%E6%A0%91%EF%BC%8C%E5%8D%B3%E5%BD%93%E8%BE%93%E5%85%A5%E6%A0%B7%E4%BE%8B%E6%98%AF0%5Cquad%200%E6%97%B6%EF%BC%8C%E8%BE%93%E5%87%BAis%5C%20a%5C%20tree&preview=true)
#include <cstdio>
#include <set>
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int fa[N],in[N];
int m;//边的数量
bool isTree;
set<int> s;
void init(){
for(int i = 1;i < N;i++) fa[i] = i,in[i] = 0;
m = 0;
isTree = 1;
s.clear();
}
int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool merge(int x,int y){
int fx = find(x),fy = find(y);
if(fx == fy) return 0;
fa[fx] = fy;
return 1;
}
void check(){
if(m != s.size() - 1 && s.size() != 0) isTree = 0;
}
int main(){
int u,v,cas = 1;
init();
while(~scanf("%d%d",&u,&v)){
if(u < 0 && v < 0) break;
if(u == 0 && v == 0){
check();
if(isTree){
printf("Case %d is a tree.\n",cas++);
}else{
printf("Case %d is not a tree.\n",cas++);
}
init();
}else{
m++;
isTree &= merge(u,v);
if(++in[v] > 1) isTree = false;
s.insert(u),s.insert(v);
}
}
}