标签:来源 while ble == define push fine include label
★★ 输入文件:counttree.in
输出文件:counttree.out
简单对比
时间限制:1 s
内存限制:128 MB
输入第一行包含一个整数N,以下N行每行包含一个整数,其中第i行的整数表示编号为i的节点的父亲节点的编号,根的父亲节点编号为0。
输出包含N行,其中第i行给出编号为i的节点的统计结果。
3 2 3 0
0 1 2
在此键入。
20%的数据1<=n<=1000
100%的数据1<=n<=100000
爆搜AC:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<string> 7 #include<queue> 8 9 using namespace std; 10 const int N=100010; 11 12 int head[N]; 13 int now=1; 14 int n; 15 queue<int>q; 16 int tot; 17 18 struct node 19 { 20 int u,v,nxt; 21 } E[N]; 22 23 inline int read() 24 { 25 int x=0,f=1; 26 char c=getchar(); 27 while(c<‘0‘||c>‘9‘) 28 { 29 if(c==‘-‘)f=-1; 30 c=getchar(); 31 } 32 while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar(); 33 return x*f; 34 } 35 36 inline void add(int u,int v) 37 { 38 E[now].u=u; 39 E[now].v=v; 40 E[now].nxt=head[u]; 41 head[u]=now; 42 now++; 43 } 44 45 inline void bfs(int x) 46 { 47 tot=0; 48 q.push(x); 49 while(!q.empty()) 50 { 51 int top=q.front(); 52 q.pop(); 53 if(top<x)tot++; 54 for(int i=head[top]; i!=-1; i=E[i].nxt) 55 { 56 int v=E[i].v; 57 q.push(v); 58 } 59 } 60 printf("%d\n",tot); 61 62 } 63 64 int main() 65 { 66 freopen("counttree.in","r",stdin); 67 freopen("counttree.out","w",stdout); 68 69 n=read(); 70 71 for(int i=1; i<=n; i++) 72 head[i]=-1; 73 for(int i=1; i<=n; i++) 74 { 75 int u=read(); 76 add(u,i); 77 } 78 79 for(int i=1; i<=n; i++) 80 bfs(i); 81 82 return 0; 83 84 }
DFS序的做法80分:
1 #include<algorithm> 2 #include<cstdio> 3 #define maxn 100001 4 5 using namespace std; 6 7 struct Edge 8 { 9 int vi; 10 int vj; 11 int next; 12 }edge[maxn * 4]; 13 14 int topedge = 0;int topx = 0; 15 int dfsx[maxn * 5];int b[maxn];int e[maxn];//在dfs序中出现次序 0 16 int head[maxn]; 17 int kai;int N; 18 19 inline int read() 20 { 21 int x=0,f=1; 22 char c=getchar(); 23 while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} 24 while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar(); 25 return x*f; 26 } 27 28 void push(int vi,int vj) 29 { 30 topedge ++; 31 edge[topedge].vj = vj; 32 edge[topedge].next = head[vi]; 33 head[vi] = topedge; 34 } 35 36 void dfs(int x) 37 { 38 dfsx[++topx] = x; 39 b[x] = topx; 40 for(int i = head[x];i != 0 ;i = edge[i].next) 41 { 42 dfs(edge[i].vj); 43 } 44 e[x] = topx; 45 } 46 47 int main() 48 { 49 freopen("counttree.in","r",stdin); 50 freopen("counttree.out","w",stdout); 51 N = read(); 52 for(int i = 1;i <= N;i ++) 53 { 54 int num = read(); 55 if(num == 0) 56 { 57 kai = i; 58 continue; 59 } 60 push(num,i); 61 } 62 dfs(kai); 63 for(int i = 1;i <= N;i ++) 64 { 65 int answ = 0; 66 for(int j = b[i] + 1;j <= e[i];j ++) 67 if(dfsx[j] < i) 68 answ ++; 69 printf("%d\n",answ); 70 } 71 return 0; 72 }
标签:来源 while ble == define push fine include label
原文地址:http://www.cnblogs.com/lyqlyq/p/6910091.html