Is It A Tree?(并查集)

本文介绍了一种判断给定边集是否构成一棵树的算法实现,通过使用并查集来验证每条边是否会导致环的产生,并确保只有一个根节点存在。

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

题目:

  给出一对对的数字a,b,表示从a到b有一条边。判断这是不是一棵树。

  • 多case,每个case以0 0 结尾
  • 输入以-1 -1结尾

分析:①每个节点(除了根结点)只有一个入度;②只有一个根结点。

森林:多个根。

入度:指向同一个结点的边数。

将点逐个加入集合中,然后判断就行了。

#include<stdio.h>
#include<algorithm>
using namespace std;
int flagt;//判断是否成树
int vis[10005];//标记用过的结点
int pre[10005];
int find(int x)//压缩路径 并查找
{
    return pre[x]==x?x:pre[x]=find(pre[x]);
}
void Merge(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fy!=fx)
        pre[fy]=fx;
}
void init()
{
    int i;
    for(i=1;i<=10005;i++)
    {
        pre[i]=i;
        vis[i]=0;
    }
}
int judge_tree(int n) //判断是否只有一个根结点。
{
    int i=1;
    while(i<=n && !vis[i])
        i++;

    int f1=find(i);

    while(i<=n)
    {
        if(vis[i] && find(i)!=f1)
            return 0;
        i++;
    }

    return 1;
}
int main()
{
    int i,j,k=1;
    int x,y,range;
    flagt=1;
    range=0;
    init();

    while(scanf("%d%d",&x,&y)!=EOF)
    {


        if(x<0 || y<0)
            return 0;

        if(x==0 || y==0) //输入0 0时判断。
        {
            if(flagt && judge_tree(range))
                printf("Case %d is a tree.\n",k++);

            else
                printf("Case %d is not a tree.\n",k++);
            flagt=1;
            range=0;
            init();
            continue;
        }

        if(!flagt)
            continue;

       range=max(range,max(x,y));//所涉及到的最数值最大的点。

        vis[x]=vis[y]=1;
        
        if(find(x)==find(y))//他们有共同的根结点,并且他们之间还有父子关系(即有边相连),则无法形成树。
            flagt=0;

        Merge(x,y);

    }
    return 0;
}


Problem Statement Given is a weighted undirected connected graph G with N vertices and M edges, which may contain self-loops and multi-edges. The vertices are labeled as Vertex 1, Vertex 2, …, Vertex N. The edges are labeled as Edge 1, Edge 2, …, Edge M. Edge i connects Vertex a i ​ and Vertex b i ​ and has a weight of c i ​ . Here, for every pair of integers (i,j) such that 1≤i<j≤M, c i ​  =c j ​ holds. Process the Q queries explained below. The i-th query gives a triple of integers (u i ​ ,v i ​ ,w i ​ ). Here, for every integer j such that 1≤j≤M, w i ​  =c j ​ holds. Let e i ​ be an undirected edge that connects Vertex u i ​ and Vertex v i ​ and has a weight of w i ​ . Consider the graph G i ​ obtained by adding e i ​ to G. It can be proved that the minimum spanning tree T i ​ of G i ​ is uniquely determined. Does T i ​ contain e i ​ ? Print the answer as Yes or No. Note that the queries do not change T. In other words, even though Query i considers the graph obtained by adding e i ​ to G, the G in other queries does not have e i ​ . What is minimum spanning tree? The spanning tree of G is a tree with all of the vertices in G and some of the edges in G. The minimum spanning tree of G is the tree with the minimum total weight of edges among the spanning trees of G. Constraints 2≤N≤2×10 5 N−1≤M≤2×10 5 1≤a i ​ ≤N (1≤i≤M) 1≤b i ​ ≤N (1≤i≤M) 1≤c i ​ ≤10 9 (1≤i≤M) c i ​  =c j ​ (1≤i<j≤M) The graph G is connected. 1≤Q≤2×10 5 1≤u i ​ ≤N (1≤i≤Q) 1≤v i ​ ≤N (1≤i≤Q) 1≤w i ​ ≤10 9 (1≤i≤Q) w i ​  =c j ​ (1≤i≤Q,1≤j≤M) All values in input are integers.c++code
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值