[Baojiaobaohui] Use the tree definition to judge the tree! (HDU1272) (HDU1325) (POJ1308)

The title of these questions are to judge the tree, but the output is different

These definitions do tree, that tree is sure to know what
learning is very confused when the general direction is not clear
what is the general direction of it?
两个字:图论
The tree is definitely a picture, but the picture is not necessarily a tree.
You can do this happily by figuring out these few knowledge points

  • What is degree? Degrees are sides. Degrees are divided into in degrees and out degrees (out edges and in edges). We have a point V here, the edge pointing to V is the in-degree, and the point out from V is the out-degree
  • What is a tree? There is only one graph with a point-in-degree of 0, and the other point-in-degree of 1 is a tree.

Well, once what is a tree is solved, then all problems are solved. what? You don't understand? Then I will list them one by one

  1. Only one entry with a degree of 0 ensures that the tree has only one root node
  2. Except for the root node, all other points have in-degrees, which guarantees that this must be a connected graph (think carefully, you will know that there must be in-degrees)
  3. Except for the root node, the intensities of other points are all 1, ensuring that there is only one root node for each point (this also ensures that the graph cannot have a ring)

So, you can also do this problem with the check set, the operation is:

  1. The connected component is 1 (The number of Find (i) == i is only 1)
  2. And the picture has no rings. (It can be judged in the merge operation)

Of course, there is a ghost in this question-even if there is no point, it is a tree.
Fig reason can not be the empty set , since the topic and feel Kongjishise , then it would add a special sentence it ......

Here is a list of codes made with the nature of the tree.

#include <bits/stdc++.h>///注意poj不能用万能头,自行更改吧
using namespace std;
int T,n,a,b;
int e[100005];///入度
int book[100005];///标记这个点是否出现
int main()
{
    int mx=0;///我要遍历存在的点。。这里是用数组下标,所以我要保存遍历到哪里
    int cnt=1;///这个just是用来输出的时候看是第几个例子而已……不用管
    int isp=1;///这就是判断是不是空集的标记
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        if(a+b<0)break;///-1 -1的时候结束
        if(a==0 && b==0)///两个都是0的时候判断
        {
            int ans=0;///入度为0的个数(有几个根,很明显只能有一个根)
            int flag=1;///是否除了根节点以外都是入度为1的呢?
            for(int i=1;i<=mx;i++)
            {
                if(book[i] && e[i]==0)///判断出现并且入度为0,那就是根
                {
                    ans++;
                }
                else if(book[i] && e[i]!=1)///不是根,那就判断入度是不是1
                {
                    flag=0;
                }
            }
            ///hdu 1272的只有输出不一样!(HDU1325)(POJ1308)都是这个板子
            if((flag==1 && ans==1) || isp)printf("Case %d is a tree.\n",cnt);
            else printf("Case %d is not a tree.\n",cnt);
            ///这些都是初始化
            memset(e,0,sizeof(e));
            memset(book,0,sizeof(book));
            mx=0;
            cnt++;
            isp=1;
        }
        else{
            isp=0;
            e[b]++;///入度
            book[a]=1;///标记这个点是否出现
            book[b]=1;
            mx=(max(a,max(mx,b)));
        }
    }
    return 0;
}


Published 6 original articles · received 1 · views 560

Guess you like

Origin blog.csdn.net/qq_42482358/article/details/105473827