ds 6.1 brothers
来源:互联网 发布:乔任梁死因分析知乎 编辑:程序博客网 时间:2024/06/06 11:22
/***************Problem from :dsProblem describe : bfs算法计算每层节点个数 data: 2016.12.7****************/#include<iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<cmath>#include<map>#include<stack>#include<queue>#include<ctime>#include<cstring>#include<vector>#include<string>#define ll __int64#define inf 0x3f3f3f3f3fusing namespace std;const int maxn = 100005;struct node{int fa;int l;int r;int id;}v[maxn];//Childs记录每个节点的儿子个数 cnt记录每层节点个数 int Childs[maxn]={0}, cnt[maxn]={0};void bfs(int root){queue<int>que;que.push(root);int k=0;cnt[k] = 1;v[root].id = k;int ans=0;while(!que.empty()){int p = que.front();que.pop();if(v[p].id!=k) //当前这层已经遍历完了 共统计这层的总数并且记录 {cnt[k]=ans;ans=0; k++;}if(v[p].l) {v[v[p].l].id = k+1;que.push(v[p].l);}if(v[p].r){v[v[p].r].id = k+1; //标记 该节点所在层数 que.push(v[p].r);}ans++;}cnt[k] = ans;//最后一层要记录 return ;}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int i, n, q, x, ans=0;scanf("%d", &n);v[0].fa = 0;Childs[0]=1;//假设根节点的父节点为 0 防止查询 根节点是出现错误 for(i=1; i<=n; i++){scanf("%d%d", &v[i].l,&v[i].r);if(v[i].l) v[v[i].l].fa = i, Childs[i]++; if(v[i].r) v[v[i].r].fa = i, Childs[i]++;}bfs(1);scanf("%d", &q);while(q--){scanf("%d", &x);printf("%d %d\n", Childs[v[x].fa]-1, cnt[v[x].id]-Childs[v[x].fa]);}return 0;}
0 0
- ds 6.1 brothers
- ds
- ds
- ds
- ds
- ds
- DS
- ds
- ds
- ds
- ds
- ds
- ds
- DS
- ds
- ds
- Two Brothers
- 干吧跌!~brothers!~~
- 4种get方法传参
- java web 项目配置日志框架log4j
- [UVALive]7338 树链剖分+线段树
- 数组中只出现一次的2个数
- hold on and hold off
- ds 6.1 brothers
- 【Codeforces Beta Round #48】 Codeforces 51F Caterpillar
- ROS机器人Diego 1#制作(十一)将letv xtion点云数据转换成激光数据
- 4 种方法识别Linux系统 USB 设备
- C++ Primer课后练习9.47
- jQuery的选择器(五)
- php实现页面静态化
- python 中 sort(), sorted(), argsort()
- java类的成员变量、静态成员变量、一般方法、静态方法