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

2024-08-23 02:38

本文主要是介绍北邮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;//failedint 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){//postOrderif (index!=-1){//traverseNode &nowN=treeList[index];for (int i=0;i<nowN.sonList.size();i++){freeTree(nowN.sonList[i]);}//visitfreeNode(index);}
}
int main(){int t,n;int x,y;int count;scanf("%d",&t);while (t--){//initiatefor (int i=0;i<MAXSIZE;i++){treeList[i].initNode();}cursor=0;for (int i=0;i<n;i++){degree[i]=0;}count=0;//inputscanf("%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);//Ôò´´½¨ }           }//searchfor (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++;}//outputprintf("%d\n",count);}return true;
} 

这篇关于北邮OJ-92. 统计节点个数-13计院上机B的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098123

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

hdu1496(用hash思想统计数目)

作为一个刚学hash的孩子,感觉这道题目很不错,灵活的运用的数组的下标。 解题步骤:如果用常规方法解,那么时间复杂度为O(n^4),肯定会超时,然后参考了网上的解题方法,将等式分成两个部分,a*x1^2+b*x2^2和c*x3^2+d*x4^2, 各自作为数组的下标,如果两部分相加为0,则满足等式; 代码如下: #include<iostream>#include<algorithm

spoj705( 求不相同的子串个数)

题意:求串s的不同子串的个数 解题思路:任何子串都是某个后缀的前缀,对n个后缀排序,求某个后缀的前缀的个数,减去height[i](第i个后缀与第i-1 个后缀有相同的height[i]个前缀)。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstrin

day-51 合并零之间的节点

思路 直接遍历链表即可,遇到val=0跳过,val非零则加在一起,最后返回即可 解题过程 返回链表可以有头结点,方便插入,返回head.next Code /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}*

XTU 1233 n个硬币连续m个正面个数(dp)

题面: Coins Problem Description: Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing head face or tail face o

flume系列之:查看flume系统日志、查看统计flume日志类型、查看flume日志

遍历指定目录下多个文件查找指定内容 服务器系统日志会记录flume相关日志 cat /var/log/messages |grep -i oom 查找系统日志中关于flume的指定日志 import osdef search_string_in_files(directory, search_string):count = 0

hdu4267区间统计

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import

hdu4417区间统计

给你一个数列{An},然后有m次查询,每次查询一段区间 [l,r] <= h 的值的个数。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamRead

hdu3333区间统计

题目大意:求一个区间内不重复数字的和,例如1 1 1 3,区间[1,4]的和为4。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;