【PAT】1094. The Largest Generation
来源:互联网 发布:yy头像psd源码 免费 编辑:程序博客网 时间:2024/06/02 00:43
考查点:BFS或DFS,求树的最大节点层
思路:BFS代码长点,DFS较短,BFS只需设置个变量last来记录每一层的最后一个节点;DFS只需两个参数,一个节点一个层数,每次调用都要更新当前层的节点数,可以用哈希数组保存层数的节点值
提交情况:一开始输入时候%d写错成%k调了半天,之后因为找根节点时候没把0排除,第一次提交没考虑只有一个根节点的情况之后ac
BFS的代码:
#define LOCAL#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#define FOR(i, x, y) for(int i = x; i < y; i++)#define rFOR(i, x, y) for(int i = x; i > = y; i--)#define MAXN 110#define oo 0x3f3f3f3fusing namespace std;int n;int h[MAXN];vector<int> child[MAXN];int ans;int alevel;void BFS(int root){ queue<int> q; int level=1; int cnt=1; int first=0,last=root; q.push(root); while(!q.empty()){ int t=q.front(); q.pop(); FOR(i,0,child[t].size()) { q.push(child[t][i]); } if(t==last){ cnt=q.size(); last=q.back(); level++; } if(cnt>ans){ ans=cnt;alevel=level; } }}int main(){ #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // LOCAL int lv,id,k,x; scanf("%d%d",&n,&lv); FOR(i,0,lv) { scanf("%d%d",&id,&k); FOR(j,0,k) { scanf("%d",&x); child[id].push_back(x); h[x]=1; } } int root; FOR(i,1,n+1){ if(h[i]==0) { root=i;break; } } BFS(root); if(n!=1){ printf("%d %d",ans,alevel); }else printf("1 1"); return 0;}DFS的代码:
#define LOCAL#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#define FOR(i, x, y) for(int i = x; i < y; i++)#define rFOR(i, x, y) for(int i = x; i > = y; i--)#define MAXN 110#define oo 0x3f3f3f3fusing namespace std;vector<int> child[MAXN];int ha[MAXN];void DFS(int index,int level){ ha[level]++; FOR(j,0,child[index].size()) { DFS(child[index][j],level+1); }}int main(){ #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // LOCAL int n,m,parent,k,ch; scanf("%d%d",&n,&m); FOR(i,0,m) { scanf("%d%d",&parent,&k); FOR(j,0,k) { scanf("%d",&ch); child[parent].push_back(ch); } } DFS(1,1); int maxLevel=-1,maxValue=0; FOR(i,1,MAXN) { if(ha[i]>maxValue){ maxValue=ha[i]; maxLevel=i; } } printf("%d %d\n",maxValue,maxLevel); return 0;}0 0
- PAT 1094. The Largest Generation
- PAT 1094. The Largest Generation
- PAT--1094. The Largest Generation
- 【PAT】1094. The Largest Generation
- PAT-The Largest Generation
- PAT A 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation(BFS)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- 【PAT】1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT-A 1094. The Largest Generation
- pat-1094. The Largest Generation (25)
- PAT(A) - 1094. The Largest Generation (25)
- 1094. The Largest Generation (25) PAT甲级
- PAT 1094. The Largest Generation (25)
- PAT-A-1094. The Largest Generation (25)
- GitHub的使用教程之《注册账号》
- 考察对类加载的理解(问题篇)
- 设计模式:学习笔记(1)——单例模式
- Linux面试题&答案01
- 110UVA没有循环的快速排序STL
- 【PAT】1094. The Largest Generation
- Codeforces 673C Bear and Colors【暴力枚举】
- 考察对类加载的理解(答案篇)
- composer常用知识总结
- POJ - 2488 A Knight's Journey解题报告
- 小心jsch的sftp连接泄露
- 预定义常量_WIN32,WIN32,_WIN64的区别
- 装箱与拆箱
- jdk8 特性例子 接口默认方法 default method