真尴尬1180

本文介绍了一个用于判断一组边是否构成树结构的算法。通过构建图并检查连通性、环和根节点等条件来实现这一目标。代码使用C++编写,并详细展示了如何添加边、进行并查集操作以及广度优先搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结束的标志是两个小于0的数……
被套路了……..

#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10005;
const int maxm = 10005;
struct Edge
{
    int from,to,next;
};
Edge e[maxm];
int head[maxn],edgeNum = 0,uset[maxn],du[maxn];

void addEdge(int from,int to)
{
    e[edgeNum].from = from; e[edgeNum].to = to; e[edgeNum].next = head[from];
    head[from] = edgeNum; edgeNum++;
}
int Find(int x)
{
    if(x == uset[x]) return x;
    else return uset[x] = Find(uset[x]);
}
int bfs(int rt)
{
    queue<int> q;
    q.push(rt);
    int num = 1;
    while(!q.empty())
    {
        int fa = q.front(); q.pop();
        for(int j = head[fa]; j != 0; j = e[j].next)
        {
            q.push(e[j].to);
            num++;
        }
    }
    return num;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int from,to,cas = 1;
    while(scanf("%d%d" ,&from,&to) != EOF)
    {
        if(from <= 0 || to <= 0) break;
        if(from == 0 && to == 0)
        {
            printf("Case %d is a tree.\n",cas++); continue;
        }
        memset(head,0,sizeof(head));
        memset(du,0,sizeof(du));
        edgeNum = 1;
        set<int> myset;
        myset.insert(from);
        myset.insert(to);
        addEdge(from,to);
        du[to]++;
        while(scanf("%d%d" ,&from,&to))
        {
            if(from == 0 && to == 0) break;
            myset.insert(from);
            myset.insert(to);
            addEdge(from,to);
            du[to]++;
        }
        for(int i = 0; i < maxn; i++) uset[i] = i;
        bool OK = true; //认为不存在重边,环
        for(int i = 1; i < edgeNum; i++)
        {
            int x = Find(e[i].from);
            int y = Find(e[i].to);
            if(x != y) uset[x] = y;
            else
            {
                OK = false; break;
            }
        }
        if(!OK) //存在重边,环
        {
            printf("Case %d is not a tree.\n",cas++); continue;
        }
        int num = 0; //连通分量个数
        for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr)
        {
            int x = *itr;
            if(uset[x] == x) num++;
            if(num >= 2) break;
        }
        if(num != 1)
        {
            printf("Case %d is not a tree.\n",cas++); continue;
        }
        else //只有一个连通分量
        {
            int rt = -1;
            for(set<int>::iterator itr = myset.begin(); itr != myset.end(); ++itr)
            {
                int x = *itr;
                if(du[x] == 0)
                {
                    rt = x; break;
                }
            }
            if(rt == -1) //没能找到根
            {
                printf("Case %d is not a tree.\n",cas++); continue;
            }
            else
            {
                int cnt = bfs(rt);
                if(cnt != myset.size())
                {
                    printf("Case %d is not a tree.\n",cas++);
                }
                else
                {
                    printf("Case %d is a tree.\n",cas++);
                }
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值