北邮OJ-92. 统计节点个数-13计院上机B

最终算法:
使用树模型,建立完整的包含父节点与子节点的树结构,这是为了后面在遍历比较与父与子的时候能够找到父找到子。然后关于degree的计算,
在输入父子关系的时候可以直接把父与子的degree分别+1(因为求的是总度,可以看成无向图的顶点的度来处理)。
错误建模:
1.使用了并查集模型:错误在于只能记录父节点而不能记录子节点
2.使用了图模型 :错误在于把树转化为图之后,要把简单的父子关系转化为每个顶点的边集,很是麻烦,模型本身就不对

题目描述
给出一棵有向树,一共有N(1

#include <iostream>
#include <cstdio>
#include <vector>
#define MAXSIZE 1010
#define data father
using namespace std;
typedef struct Node{
    int data;
    vector<int> sonList;
    bool turnOn;
    Node(){//initiate automatically except data 
        initNode();
    }
    void initNode(){
        turnOn=false;
        sonList.clear();
    }
}*Tree;
Node treeList[MAXSIZE];
int cursor;
int degree[MAXSIZE];

int createNode(int data){//return the index of new node 
    if (cursor+1==MAXSIZE)
        return -1;//failed
    int nowP=cursor;
    cursor++;
//  treeList[nowP].initNode();
    treeList[nowP].turnOn=true;
    treeList[nowP].data=data;
    return nowP;
}
int createNode(int data,int index){//return the index of new node 
//  treeList[nowP].initNode();
    treeList[index].turnOn=true;
    treeList[index].data=data;
    return index;
}
void freeNode(int index){
    treeList[index].initNode();
}
void freeTree(int index){//postOrder
    if (index!=-1){
        //traverse
        Node &nowN=treeList[index];
        for (int i=0;i<nowN.sonList.size();i++){
            freeTree(nowN.sonList[i]);
        }
        //visit
        freeNode(index);
    }
}
int main(){
    int t,n;
    int x,y;
    int count;
    scanf("%d",&t);
    while (t--){
        //initiate
        for (int i=0;i<MAXSIZE;i++){
            treeList[i].initNode();
        }
        cursor=0;
        for (int i=0;i<n;i++){
            degree[i]=0;
        }
        count=0;
        //input
        scanf("%d",&n);
        for (int i=0;i<n-1;i++){
            scanf("%d%d",&x,&y);
                //ͳ¼Æ½ÚµãÊý 
            degree[x]++;
            degree[y]++;
            if (treeList[x].turnOn!=true){//Èô´Ë¸¸½ÚµãÉÐ佨Á¢ 
                createNode(-1,x);//Ôò´´½¨ 
            }
            treeList[x].sonList.push_back(y);
            if (treeList[y].turnOn!=true){//Èô´Ë½ÚµãÉÐδ´´½¨ 
                createNode(x,y);//Ôò´´½¨ 
            }           
        }
        //search
        for (int i=0;i<n;i++){//±éÀúËùÓнáµã£¬ÅжÏÆäÊÇ·ñÊÇp½áµã 
            Node &nowNode=treeList[i];
            bool isP=true;
            if (degree[i]<degree[nowNode.data])
                isP=false;
            else{
                for (int j=0;j<nowNode.sonList.size();j++){
                    if (degree[i]<degree[nowNode.sonList[j]]){
                        isP=false;
                        break;
                    }
                }
            }
            if (isP==true)
                count++;
        }
        //output
        printf("%d\n",count);
    }
    return true;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值