1. 程式人生 > >BUPT復試專題—統計節點個數(2013)

BUPT復試專題—統計節點個數(2013)

desc pro .so ems include earch truct ont ret

題目描述

給出一棵有向樹,一共有n個節點,如果一個節點的度(入度+出度)不小於它所有兒子以及它父親的度(如果存在父親或兒子),那麽我們稱這個節點為p節點,現在你的任務是統計p節點的個數。

如樣例,第一組的p節點為1,2,3;第二組的p節點為0。

輸入

第一行為數據組數T。
每組數據第一行為表示樹的節點數。

後面的行,每行兩個數,代表節點編號和兒子節點的編號。

輸出

每組數據輸出一行,為一個整數,代表這棵樹上p節點的個數。

樣例輸入

2
5
0 1
1 2
2 3
3 4
3
0 2
0 1

樣例輸出

3
1

來源

2013機考B題

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
struct donser
{
    int father;
    int son1;
    int son2;
    int num;
};
int main() 
{
    int T;
    cin>>T;
    while(T--)
    {
        
int num=0,j=0,cout_num=0; donser tree[200]; while(j<19) { tree[j].father=-1;tree[j].son1=-1;tree[j].son2=-1;tree[j].num=0; j++; } cin>>num;j=num-1; while(j--) { int m,n; cin>>m>>n;
if(tree[m].son1==-1) { tree[m].son1=n; tree[m].num++; } else if(tree[m].son1!=-1) { tree[m].son2=n; tree[m].num++; } tree[n].father=m; tree[n].num++; } for(int i=0;i<num;i++) { int father=tree[i].father; int son1=tree[i].son1; int son2=tree[i].son2; int num=tree[i].num; if((father!=-1&&tree[father].num<=num)||father==-1) { if((son1!=-1&&tree[son1].num<=num)||son1==-1) { if((son2!=-1&&tree[son2].num<=num)||son2==-1) { cout_num++; } } } } cout<<cout_num<<endl; } return 0; }

BUPT復試專題—統計節點個數(2013)