kAri OJ 0092.统计节点个数

来源:互联网 发布:书法字典软件 编辑:程序博客网 时间:2024/06/05 11:51

HINT:我实在不知道我的程序怎么就通不过北邮的OJ了,所以先在这里记录一下,以后有工夫再回来解决这东西难过

题目描述

给出一棵有向树,一共有N(1<N1000)个节点,如果一个节点的度(入度+出度)不小于它所有儿子以及它父亲的度(如果存在父亲或儿子),那么我们称这个节点为p节点,现在你的任务是统计p节点的个数。

如样例,第一组的p节点为1,2,3;第二组的p节点为0。

输入格式

第一行为数据组数T(1T100)。
每组数据第一行为N表示树的节点数。后面为N1行,每行两个数x,y(0x,y<N),代表yx的儿子节点。

输出格式

每组数据输出一行,为一个整数,代表这棵树上p节点的个数。

其实吧,这道题的思路很容易出来,就是先用一个数组夹链表的方式先存储这个树,顺便记录各个节点的度,存储完之后再对每个节点依次进行判断,并改变p节点的数目。我的代码如下:


#include<cstdio>using namespace std;typedef struct child{int name;child *next;}child;typedef struct node{int degree;int fathername;child *firstchild;}node;int main(void){int nog;scanf("%d", &nog);for (int i = 1; i <= nog; i++){int nodenum, pnodenum = 0;scanf("%d", &nodenum);node nodes[1000];for(int j=0;j<nodenum-1;j++){            nodes[j].degree=0;            nodes[j].firstchild=NULL;}for (int j = 0; j<nodenum - 1; j++){int x, y;scanf("%d %d", &x, &y);++nodes[x].degree; ++nodes[y].degree;nodes[y].fathername = x;child *temp;if (nodes[x].firstchild == NULL){nodes[x].firstchild = new child;nodes[x].firstchild->name = y;nodes[x].firstchild->next = NULL;}else{temp = new child;temp->name = y;temp->next = nodes[x].firstchild;nodes[x].firstchild = temp;}}for (int j = 0; j<nodenum - 1; j++){bool judge = true;if (j!=0)if (nodes[j].degree<nodes[nodes[j].fathername].degree)judge = false;for (child *temp = nodes[j].firstchild; temp != NULL&&judge == true; temp = temp->next){if (nodes[j].degree<nodes[temp->name].degree)judge = false;}if (judge)++pnodenum;}printf("%d\n", pnodenum);}return 0;}

我自己尝试了各种测试样例,运行结果都没错,但一上OJ就歇菜。。。好吧这篇博文先到这儿,存个档以后再搞起。

0 0
原创粉丝点击