Is It A Tree? 【HDU - 1325】【树的性质,不要忘记考虑入度】

  题意:给你一串有向边,问它们能否构成一棵树。

  T了的原因:因为负整数就要直接结束并非“-1 -1”;

  WA的原因:要考虑入度。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int e1, e2, ans;
set<int> indu;
set<int> st;
bool flag;
void init()
{
    flag=true;
    indu.clear();
    st.clear();
    ans=0;
}
int main()
{
    int Cas=0;
    while(scanf("%d%d", &e1, &e2)!=EOF)
    {
        if(e1<0 && e2<0) break;
        if(!e1 && !e2) { printf("Case %d is a tree.\n", ++Cas); continue; }
        init();
        st.insert(e1);  st.insert(e2);
        indu.insert(e2);
        ans++;
        while(scanf("%d%d", &e1, &e2) && (e1 && e2))
        {
            st.insert(e1);  st.insert(e2);
            if(indu.count(e2)) flag=false;
            indu.insert(e2);
            ans++;
        }
        if(ans == st.size()-1 && flag) printf("Case %d is a tree.\n", ++Cas);
        else printf("Case %d is not a tree.\n", ++Cas);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/83449393