下面两题用DFS或BFS都可以比较顺的AC。关键是得到各个结点的深度。
1079 Total Sales of Supply Chain (25 分)
我的做法是先BFS得到各结点level,然后计算prices单价倍率数组,最后输出时再乘原始价格,尽量少些乘法次数= =。
⚠️16分和25分之间,之差float -> double,尽量用double啊
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define MAXN 100010
using namespace std;
int nn;
struct Node {
int level;
double amt = 0.0;
vector<int> children;
} tree[MAXN];
int BFS(int root) {
queue<int> mq;
tree[root].level = 0;
mq.push(root);
int curr, lvl = 0;
while (!mq.empty()) {
curr = mq.front();
mq.pop();
lvl = tree[curr].level + 1;
for (int i = 0; i < tree[curr].children.size(); ++i) {
int child = tree[curr].children[i];
tree[child].level = lvl;
mq.push(child);
}
}
return lvl;
}
int main() {
double ori, rate;
scanf("%d%lf%lf", &nn, &ori, &rate);
vector<int> retailer;
for (int i = 0; i < nn; ++i) {
int nch;
scanf("%d", &nch);
if (nch) {
int ch;
while (nch--) {
scanf("%d", &ch);
tree[i].children.emplace_back(ch);
}
} else {
scanf("%lf", &tree[i].amt);
retailer.emplace_back(i);
}
}
int mlvl = BFS(0);
vector<double> prices;
double rrr = 1.0, rr = 1.0 + rate / 100.0, total = 0.0;
for (int k = 0; k <= mlvl; ++k) {
prices.emplace_back(rrr);
rrr *= rr;
}
for (int j = 0; j < retailer.size(); ++j) {
total += tree[retailer[j]].amt * prices[tree[retailer[j]].level];
}
printf("%.1lf\n", total * ori);
return 0;
}
1090 Highest Price in Supply Chain (25 分)
⚠️DFS求结点深度时,要将depth作为参数一层一层传进去,而不能仅仅把depth作为全局变量。max_depth和零售商数量可用全局变量。
⚠️最高价零售商数量:计数最大深度结点即可。
#include <cstdio>
#include <cmath>
#include <vector>
#define MAXN 100010
using namespace std;
int nn, max_depth = -1, retailer_cnt = 0;
vector<int> nodes[MAXN];
void dfs(int root, int depth) {
int size = nodes[root].size();
if (size == 0) {
if (depth > max_depth) {
max_depth = depth;
retailer_cnt = 1;
} else if (depth == max_depth)
retailer_cnt++;
}
for (int i = 0; i < size; ++i) {
dfs(nodes[root][i], depth + 1);
}
}
int main() {
double ori_price, rate;
scanf("%d%lf%lf", &nn, &ori_price, &rate);
int root, father;
for (int i = 0; i < nn; ++i) {
scanf("%d", &father);
if (father == -1) root = i;
else nodes[father].emplace_back(i);
}
dfs(root, 0);
printf("%.2lf %d\n", ori_price * pow(1.0 + rate / 100.0, max_depth), retailer_cnt);
return 0;
}
1094 The Largest Generation (25 分)
#include <cstdio>
#include <vector>
#include <queue>
#define MAXN 101
using namespace std;
vector<int> tree[MAXN];
vector<int> cnts;
int nn, mm, level[MAXN];
void BFS(int root) {
queue<int> mq;
mq.push(root);
level[root] = 1;
cnts.emplace_back(1);
while (!mq.empty()) {
int curr = mq.front();
mq.pop();
for (int i = 0; i < tree[curr].size(); ++i) {
if (cnts.size() > level[curr])
cnts.emplace_back(0);
mq.push(tree[curr][i]);
level[tree[curr][i]] = level[curr] + 1;
cnts[level[curr] + 1]++;
}
}
}
int main() {
scanf("%d%d", &nn, &mm);
cnts.emplace_back(0);
int member, nch, child;
for (int i = 0; i < mm; ++i) {
scanf("%d%d", &member, &nch);
for (int j = 0; j < nch; ++j) {
scanf("%d", &child);
tree[member].emplace_back(child);
}
}
BFS(1);
int Mcnt = 0, gen = -1;
for (int i = 0; i < cnts.size(); ++i) {
if (cnts[i] > Mcnt) {
Mcnt = cnts[i];
gen = i;
}
}
printf("%d %d\n", Mcnt, gen);
return 0;
}
📅1205 离考研还有15天 我好刚啊
DFS版 更简单嗷
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int nn, cnts[100] = {0}, M = 0;
vector<int> gra[100];
void DFS(int root, int level) {
cnts[level]++;
M = max(level, M);
for (auto item: gra[root]) {
DFS(item, level + 1);
}
}
int main() {
int mm, anc = 1;
scanf("%d%d", &nn, &mm);
for (int i = 0; i < mm; i++) {
int par, temp, kid;
scanf("%d%d", &par, &temp);
for (int j = 0; j < temp; j++) {
scanf("%d", &kid);
gra[par].push_back(kid);
}
}
DFS(anc, 1);
int midx = 0;
for (int i = 1; i <= M; i++) {
if (cnts[i] > cnts[midx])
midx = i;
}
printf("%d %d\n", cnts[midx], midx);
return 0;
}
1004 Counting Leaves (30 分)
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int nn, n_hasleaf;
vector<int> nodes[101];
int level_leaf_cnt[101], Mlevel = -1;
void DFS(int root, int level) {
Mlevel = max(Mlevel, level);
int size = nodes[root].size();
if (size == 0) level_leaf_cnt[level]++;
else
for (int i = 0; i < size; ++i) {
DFS(nodes[root][i], level + 1);
}
}
int main() {
while (scanf("%d", &nn) != EOF) {
if (nn == 0) break;
scanf("%d", &n_hasleaf);
for (int i = 0; i < n_hasleaf; ++i) {
int father, cnt, child;
scanf("%d%d", &father, &cnt);
for (int j = 0; j < cnt; ++j) {
scanf("%d", &child);
nodes[father].emplace_back(child);
}
}
}
DFS(1, 0);
for (int i = 0; i <= Mlevel; ++i) {
printf("%d", level_leaf_cnt[i]);
if (i < Mlevel) printf(" ");
}
return 0;
}
网友评论