B - Is It A Tree?

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. 
There is exactly one node, called the root, to which no directed edges point. 

Every node except the root has exactly one edge pointing to it. 

There is a unique sequence of directed edges from the root to each node. 

For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not. 

   


In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not. 

Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero. 
Output
For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1). 
Sample Input
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
 
 

解题思路:本题与上一题相差甚微。只需要加一个判断被指几次的数组以及数清楚到底有几个根(数根:只用在遍历的时候查出有几个是pre[i]=i即可)

1.有且只有一个最上层的根节点,它不能被指向。

         2.除此之外所有的节点都只能被指向一次(所以称作有向的图),因此需要再用一个 数组来记录被指向的次数。

         3.依然采用节点数减去线条数等于1,来判断是否会形成回路

          




#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
using namespace std;

#define MAXN 100010
bool Set[MAXN];
int pre[MAXN];
int in[MAXN];
bool flag=1;

int Find(int x) //查 一层一层向上查找父亲
{
    int r=x;
    while(pre[x]!=r)
    {
       r=pre[x];
    }
    return r;
}

void join(int x,int y) //并 把x的父亲设为y
{
    int fx=Find(x),fy=Find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
    }
    else     //这里是比较创新的一个位置因为说明:如果有一个孤立的节点就直接No了
    {
        flag=0;//围成一个圈
    }
}
int main()
{
    //freopen("d:\\A.txt","r",stdin);
    int a,b;
    int cut=0;//记录有几个 非孤立的节点

    int root_num = 0;
    int k=0;
    memset(Set,0,sizeof(Set));
    memset(in,0,sizeof(in));
    for(int i=1;i<=MAXN;i++)
    {
        pre[i]=i;//把所有的父亲都设为本身
    }
    while(cin>>a>>b)
    {
        if(a==-1&&b==-1)break;
       if(a!=0||b!=0)
       {

           if(Set[a]==0)
           {
               cut++;
               Set[a]=1;//用过了
           }
           if(Set[b]==0)
           {
               cut++;
               Set[b]=1;
           }
           if(a<b)//总是把大的数设为节点,就避免了重复
           {
               join(a,b);//a被指向的次数加1
               in[a]++;
           }
           else
           {
               join(b,a);
               in[b]++;
           }
       }
       else
       {
           k++;
           int n=0;    //记录有几条边
           for(int i=1;i<=MAXN;i++)
           {
               if(pre[i]!=i)//说明已经和别人建立联系的 方法 算出有几根线
               {
                   n++;
               }
               if(in[i]>1)
               {
                   flag=0;
               }
               if(Set[i]==1&&(pre[i]=i))
               {
                   root_num++;
               }
           }
           for(int i=1;i<MAXN;i++)
           {
               pre[i]=i;
               Set[i]=0;           //还原语句,因为不能再回去初始化了
               in[i]=0;
           }

           //cout<<"n:"<<n<<"cut:"<<cut<<"flag"<<flag;
           if(flag && (n==(cut-1)||n==0&cut==0)&& root_num)cout<<"Case "<<k<<" is a tree."<<endl;
           else cout<<"Case "<<k<<" is not a tree."<<endl;
           flag=1;
           cut=0;
           root_num=0;
       }
    }

    return 0;
}

http://www.xukeyang.com/index.php/archives/35/
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我会为你提供详细的伪代码解释B-Tree算法的过程。下面是代码示例: ``` B-Tree Algorithm class Node { int n; // Number of keys in the node boolean leaf; // True if it's a leaf node, false otherwise int[] keys; // Keys stored in the node Node[] children; // Pointers to the children nodes } // Insert a new key into the B-Tree public void insert(Node root, int key) { Node r = root; if (r.n == 2 * t - 1) { // The root is full, create a new root Node s = new Node(); root = s; s.leaf = false; s.n = 0; s.children[0] = r; splitChild(s, 1, r); insertNonFull(s, key); } else { insertNonFull(r, key); } } // Split a full child node into two non-full child nodes private void splitChild(Node x, int i, Node y) { Node z = new Node(); z.leaf = y.leaf; z.n = t - 1; for (int j = 0; j < t - 1; j++) { // Copy the keys of y to z z.keys[j] = y.keys[j + t]; } if (!y.leaf) { // If y is not a leaf, copy its children to z for (int j = 0; j < t; j++) { z.children[j] = y.children[j + t]; } } y.n = t - 1; for (int j = x.n; j >= i + 1; j--) { // Move x's children to the right x.children[j + 1] = x.children[j]; } x.children[i + 1] = z; // Insert z as x's child for (int j = x.n - 1; j >= i; j--) { // Move x's keys to the right x.keys[j + 1] = x.keys[j]; } x.keys[i] = y.keys[t - 1]; // Set the key of y as x's key x.n++; // Increment x's key count } // Insert a key into a non-full node private void insertNonFull(Node x, int key) { int i = x.n - 1; if (x.leaf) { while (i >= 0 && key < x.keys[i]) { // Find the position to insert the key x.keys[i + 1] = x.keys[i]; i--; } x.keys[i + 1] = key; x.n++; } else { while (i >= 0 && key < x.keys[i]) { // Find the child node to insert the key i--; } i++; if (x.children[i].n == 2 * t - 1) { // If the child node is full, split it splitChild(x, i, x.children[i]); if (key > x.keys[i]) { // Choose which child to insert the key i++; } } insertNonFull(x.children[i], key); // Recursively insert the key } } ``` 上面的代码是一个经典的B-Tree算法的伪代码,可以将其用任何一种编程语言实现。该算法支持高效地插入、查找和删除数据,适用于大型数据存储。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值