Time limit: 1 second for C / C + + and 2 seconds for other languages
Space limitation: C/C++ 131072K, other languages 262144 K
64bit IO Format: %lld
Space limitation: C/C++ 131072K, other languages 262144 K
64bit IO Format: %lld
Title Description
Kodori gives you a rooted tree. Find out how many subtrees satisfy the internal node number to be continuous in the value field
The meaning that some numbers are continuous in the range means that they form a continuous range in the range
Enter a description:
The first row has an integer n that represents the number of nodes in the tree. Next n – 1 lines, two integers x,y for each line, indicate that there is a directed edge from X to y. The input guarantee is a rooted tree.
Output Description:
Output a number to indicate the answer
Example 1
input
5 2 3 2 1 2 4 4 5
output
5
Explain
No. 1 in the subtree of node 1, with continuous value field Node 3 is numbered 3 in the subtree, and the value field is continuous No. 5 in the subtree of node 5, with continuous value range 4,5 in the subtree of node 4, with continuous value range No. 1,2,3,4,5 in the subtree of node 2, with continuous value range
remarks:
For 100% data, there are n < = 100000
Find out the number of nodes, the maximum and the minimum of each subtree, and judge whether the difference between the number and the maximum and the minimum is equal.
#include<map> #include<queue> #include<math.h> #include<vector> #include<string> #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #define inf 0x3f3f3f #define ll long long #define maxn 100005 using namespace std; int vis[maxn]; int dis[maxn]; int a[maxn]; int mi[maxn],ma[maxn]; struct node { int v; struct node *next; }*h[maxn]; void LA(int u,int v) { struct node *p=(struct node*)malloc(sizeof(struct node)); p->v=v; p->next=h[u]; h[u]=p; } void dfs(int x) { a[x]=1; ma[x]=x; mi[x]=x; struct node *p; if(h[x]==NULL) return ; for(p=h[x]; p!=NULL; p=p->next) { int u=p->v; dfs(u); a[x]+=a[u]; ma[x]=max(ma[x],ma[u]); mi[x]=min(mi[x],mi[u]); } } int main() { int t; while(~scanf("%d",&t)) { int n=t; t--; int m=0; memset(vis,0,sizeof(vis)); memset(ma,0,sizeof(ma)); memset(mi,0,sizeof(mi)); for(int i=0; i<maxn; i++)h[i]=NULL; while(t--) { int x,y; scanf("%d%d",&x,&y); LA(x,y); vis[y]=1; } int ans=0; for(int i=1; i<=n; i++) { if(vis[i]==0) dfs(i); } for(int i=1; i<=n; i++) { if(a[i]==(ma[i]-mi[i]+1)) ans++; } cout<<ans<<endl; } }