北邮OJ-92. 统计节点个数-13计院上机B
来源:互联网 发布:数据脱敏处理 编辑:程序博客网 时间:2024/05/14 04:16
最终算法:
使用树模型,建立完整的包含父节点与子节点的树结构,这是为了后面在遍历比较与父与子的时候能够找到父找到子。然后关于degree的计算,
在输入父子关系的时候可以直接把父与子的degree分别+1(因为求的是总度,可以看成无向图的顶点的度来处理)。
错误建模:
1.使用了并查集模型:错误在于只能记录父节点而不能记录子节点
2.使用了图模型 :错误在于把树转化为图之后,要把简单的父子关系转化为每个顶点的边集,很是麻烦,模型本身就不对
题目描述
给出一棵有向树,一共有N(1
#include <iostream>#include <cstdio>#include <vector>#define MAXSIZE 1010#define data fatherusing 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
- 北邮OJ-92. 统计节点个数-13计院上机B
- 北邮OJ-96. 矩阵幂-12计院上机B
- 北邮OJ-277. 内存分配-14计院上机B
- 北邮OJ-87. 日期-13计院上机A
- 北邮OJ-104. 912星球的研究生-11计院上机B
- 北邮OJ-271. 旋转图像-14计院上机(第二套)B
- BUPT OJ 统计节点个数
- 九度OJ-题目1169:比较奇偶数个数-10计院上机A
- 北邮OJ-95-二进制数-12计院上机A
- 北邮OJ-97. 二叉排序树-12计院上机C
- 北邮OJ-276. 中位数-14计院上机A
- 北邮OJ-278. 图像识别-14计院上机C
- 北邮OJ-103. 反转单词-11计院上机A
- 北邮OJ-105. 寻找宝藏-11计院上机C
- 北邮OJ-89. 统计时间间隔-13网研上机B
- 北邮OJ-93. 中序遍历序列-13计院上机C
- 北邮OJ-94. 最小距离查询-13计院上机D
- 92. 统计节点个数
- OpenGL编程指南7:视图-
- 根据流读取文件类型
- 特性
- java基础篇1
- 创建文件的下载页面
- 北邮OJ-92. 统计节点个数-13计院上机B
- MySQL修改自动增长计数器
- ch8-异常和错误
- bootstrap 常用类名
- 双缓冲模式
- 性能指标之资源指标-内存-上期答疑
- MyBatis的foreach语句详解
- Redis的一个简单连接池
- 不同浏览器Cookie有效期问题