标签:处理 set ted ace tin std less sep eps
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; int tree[N]; int lowbit(int x) { return x&(-x); } void update(int x,int c) { while(x<N) { tree[x]+=c; x+=lowbit(x); } } int getnum(int x) { int sum=0; while(x) { sum+=tree[x]; x-=lowbit(x); } return sum; } int query(int L,int R) { return getnum(R)-getnum(L-1); } struct is { int v,nex; }edge[N<<1]; int head[N<<1],edg; int in[N],out[N],tot; int n,p; void init() { memset(tree,0,sizeof(tree)); memset(head,-1,sizeof(head)); edg=0; tot=0; } void add(int u,int v) { edg++; edge[edg].v=v; edge[edg].nex=head[u]; head[u]=edg; } void dfs(int u,int fa) { in[u]=++tot; for(int i=head[u];i!=-1;i=edge[i].nex) { int v=edge[i].v; if(v==fa)continue; dfs(v,u); } out[u]=tot; } int main() { while(~scanf("%d%d",&n,&p)) { init(); if(n==0&&p==0)break; for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs(p,-1); for(int i=1;i<=n;i++) { printf("%d%c",query(in[i],out[i]),((i==n)?‘\n‘:‘ ‘)); update(in[i],1); } } return 0; }
hdu 3887 Counting Offspring dfs序+树状数组
标签:处理 set ted ace tin std less sep eps
原文地址:http://www.cnblogs.com/jhz033/p/6134926.html