利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #include <fstream> using namespace std; vector<vector<int>> tree; vector<int> ans; void BFS(int s) { queue<pair<int, int>>…
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100) AAAAAccepted code: #include<bits/stdc++.h> using namespace std; vector<]; queue<int>q; ; ; ; ; ; void BFS(){ int x=q.front();//队头元素调用 if(v…
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int cost = 0; int curFloor = 0; while (num--) { int floor; cin>>floor; int tmp = floor - curFloor; cost += tmp > 0 ? 6 * tmp : -4 * tmp; cost += 5; c…
关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> #include <iostream> using namespace std; int main() { int num; while (scanf("%d", &num) != EOF) { char earlest[20]; char lastest[20]; ch…
简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOMER_MAX 1000+1 #define INF 0x6fffffff #ifndef LOCAL // #define LOCAL #endif LOCAL int n; // number of windows <=20 int m ;// queue capacity <=10 int k;…
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> using namespace std; bool isPrime(int n) { if(n < 2) return false; if(n == 2) return true; if(n % 2 == 0) return false; for(int i = 3; i < n; i += 2)…
简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #include <iomanip> using namespace std; #define N 3 int main() { char res[3]={'W','T','L'}; char max_res[N]; int i,j; float tmp,sum=1,odd; for(i=0;i<N…
这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这样的搜索, 就是我们要求的独立区域的个数. #include <iostream> #include <fstream> #include <memory.h> using namespace std; const int maxNum = 1001; bool visit…
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; /* 统计每层的叶子节点个数 建树,然后dfs即可 */ ; int n,m; int layer[maxn]; //统计每层的叶子节点个数 ; vector<int…
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, th…