判断是否是一棵树需要以下条件
1.只有一个根节点(保证只有一棵树)
2.其他所有的节点都只有一个父亲节点(满足前两条不一定可以,因为还有可能是一个独立与这个树的环,他的所有父亲节点都只有一个,同时也不增加根节点的个数)
3.不存在环(可以用并查集判断,记录他的祖父节点,当一个节点的祖父节点是自己的时候,说明存在一个那样的环)
(注意是空树的特判!!!!!)
代码:

代码块
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int maxx = 1e6+100;
int ary[maxx];
int zufu[maxx];
const int inf = -10;

int main() {
    string s;
    int cont = 0,nofather = 0,twofather = false,havecircle = false,kongshu = true;
    int fa, son;
    while (cin>>fa>>son) {
        if (fa == -1 && son == -1) {
            break;
        }
        if (fa == 0 && son == 0) {
            if ((twofather || nofather != 1 || havecircle ) && !kongshu) {//nofather代表的是根节点
                cout << "Case " << ++cont << " is not a tree." << endl;;
            }
            else {
                cout << "Case " << ++cont << " is a tree." << endl;;
            }
            nofather = 0, twofather = false, havecircle = false, kongshu = true;
            memset(ary, 0, sizeof ary); memset(zufu, 0, sizeof zufu);
            continue;
        }
        kongshu = false;
            //判断有没有单独独立的环
        if (zufu[fa] == 0) {
            zufu[son] = fa;
        }
        else {
            zufu[son] = zufu[fa];
            if (zufu[son] == son) { havecircle = true; }
        }


        if (ary[fa] == 0) {//父亲不存在
            ary[fa] = inf;//这个点存在 但没有父亲节点
            nofather++;
        }

        if (ary[son] == inf) {nofather--;}//这个节点存在,但却没有父亲节点,现在找到了
        else if (ary[son] > 0) { twofather = true; }//有两个父亲节点
        ary[son] = fa;
    }
}