【判断是否为树】HDU 1325 Is It A Tree?

HDU 1325 Is It A Tree?

题意:给定多组有向边的起点和终点(结点的标号不一定是连续的,也不一定是从1开始,是随机的),判断这些边能不能组成一颗树。

给定条件:只有一个节点是根节点,并且除了根节点之外的所有的节点只有一条边指向它。

思路1:

我们将所有的结点都放进set里面,并且用deep[ ]来存有多少条边指向该结点。然后当输入为0 0时,我们就需要遍历一遍set,看其中的结点有没有大于 1 的,如果有就直接不是一颗树。判断完之后我们就需要判断是不是满足所有结点 - 1 == 边的个数这个构成树的条件,如果满足才是真正的一颗树。两者其实可以说是缺一不可。

如果只有第一个判断条件,那么如果是两颗分离的树,答案就错了。
如果只有第二个判断条件,那么如果有自环,或者有双向边,答案也错了。

所以说,两者缺一不可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>

using namespace std;
typedef long long ll;
const int maxN = 1e5+5 ;

int deep[maxN], edge = 0;
set<int>st;

void init()
{
    memset(deep, 0, sizeof(deep));
    edge = 0;
}

bool DEEP()
{
    priority_queue<int> pq;
    while(!st.empty())
    {
        int tmp = *st.begin(); st.erase(st.begin());
        pq.push(deep[tmp]);
    }
    if( pq.top() > 1 )
        return false;
    return true;
}

int main()
{
    int a, b;
    int Case = 0;
    while(~scanf("%d%d", &a, &b) && a != -1 && b != -1)
    {
        if(a == 0 && b == 0)
        {
            int node = st.size();
            if( DEEP() && node - 1 == edge)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                printf("Case %d is a tree.\n", ++ Case);
            else
                printf("Case %d is not a tree.\n", ++ Case);
            init();
            continue;
        }
        edge ++;
        deep[b] ++;
        st.insert(a); st.insert(b);
    }
    return 0;
}

思路2:

首先我们将所有被指向的点放进multiset里面(边的个数),将所有的点放进set里面(结点的个数)。所以我们要判断 结点的个数 - 1 是否等于 边的个数,以及multiset里面是不是有重复的元素即可。

注意multiset和set的清空。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>

using namespace std;
typedef long long ll;
const ll maxN=1e5+5;

multiset<int>mulst;
set<int>st;

void init()
{
    st.clear();
    mulst.clear();
}

bool judge()
{
    while(!mulst.empty())
    {
        int tmp = *mulst.begin();
        int num = mulst.count(tmp);
        if(num > 1)
            return false;
        mulst.erase(tmp);
    }
    return true;
}

int main()
{
    int a, b, Case = 0;
    while(~ scanf("%d%d", &a, &b) && a != -1 && b != -1)
    {
        if(a == 0 && b == 0)
        {
            if( st.size() - 1 == mulst.size() && judge() )
                printf("Case %d is a tree.\n", ++ Case);
            else
                printf("Case %d is not a tree.\n", ++ Case);
            init();
            continue;
        }
        mulst.insert(b);
        st.insert(a); st.insert(b);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值