92. Count the number of nodes
time limit Ms
Memory Limit 65536 KBTitle Description
Given a tree with a direction, there are altogether N ( 1<N≤ ) node, if the degree (degrees + degrees) of a node is not less than the degree of all its sons and its father (if there is a father or son), then we call this node the P node, and now your task is to count the number of P nodes.
For example, the first group has a P node of 0, and the second group has a P node.
Input format
Number of first-action data groups T ( 1≤T≤ )。
First behavior for each group of data N The number of nodes representing the tree. Followed by n 1 Rows, two numbers per line x , y ( 0 ≤ x y < n ), representing y Is x Son of a node.
Output format
Each set of data outputs a row, an integer representing the number of P nodes on the tree.
Input sample
250 11 22 33 430 20 1
Output sample
31
#include <stdio.h> #include <stdlib.h>int head[1001],du[1001],k;typedef struct edge{int v,next;} Edge; Edge edge[2002];void addedge (int u,int v) {edge[k].v=v; Edge[k].next=head[u]; head[u]=k++;} int main () {int t; while (~SCANF ("%d", &t)) {while (t--) {int n,i,j; scanf ("%d", &n); k=0; for (i=0;i<=n;i++) {head[i]=-1; du[i]=0; } for (i=0;i<n-1;i++) {int u,v; scanf ("%d%d", &u,&v); Addedge (U,V); Addedge (V,u); du[u]++; du[v]++; } int sum=0; for (i=0;i<n;i++) {int flag=0; for (j=head[i];j!=-1;j=edge[j].next) {int v=edge[j].v; if (Du[i]<du[v]) {flag=1; Break }} if (!flag) sum++; } printf ("%d\n ", sum); }} return 0;}
KAri OJ 92 Statistic node number adjacency table