[PAT] A1053 Path of Equal Weight

tech2022-09-03  114

(要熟练!)(树的遍历)

题目大意

(题目链接)https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512 题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径。

思路

算法笔记P307解法存在一个问题:一开始在输入时就对一个结点下的所有子节点按照权重排序,则如果子节点权重相等的话,分不清子节点的子节点谁大谁小,有可能子节点的子节点小的反而在前面了。(但PAT上的测试点没有出现这样的情况,所以这样写仍然是可以AC的。) 解决方案:路径全部保存下来,最后再排序。

AC代码

/*输入时即对子节点排序,有bug*/ #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <vector> #include <map> #include<algorithm> using namespace std; struct node { int weight; vector<int> child;//孩子的下标 }point[100]; int n, m, s; int ans[100]; bool cmp(int a, int b) { return point[a].weight > point[b].weight; } void DFS(int p, int sum, int pathlength) { if (sum > s)return; if (point[p].child.size() == 0) { if (sum == s) { printf("%d", point[0].weight); for (int i = 0;i <= pathlength - 1;i++) printf(" %d", point[ans[i]].weight); printf("\n"); } } else { for (int i = 0;i < point[p].child.size();i++) { ans[pathlength] = point[p].child[i]; DFS(point[p].child[i], sum + point[point[p].child[i]].weight, pathlength + 1); } } } int main() { int i, j; scanf("%d%d%d", &n, &m, &s); for (i = 0;i < n;i++) scanf("%d", &point[i].weight); for (i = 0;i < m;i++) { int father; scanf("%d", &father); int numchild, haizi; scanf("%d", &numchild); for (j = 0;j < numchild;j++) { scanf("%d", &haizi); point[father].child.push_back(haizi); } sort(point[father].child.begin(), point[father].child.end(), cmp); } DFS(0, point[0].weight, 0); return 0; }

正确代码

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <cstdio> #include <vector> #include <map> #include<algorithm> using namespace std; struct node { int weight; vector<int> child;//孩子的下标 }point[100]; int n, m, s; int path[100]; vector<vector<int>>ans; bool cmp(vector<int> a, vector<int> b) { return a > b; } void DFS(int p, int sum, int pathlength) { if (sum > s)return; if (point[p].child.size() == 0) { if (sum == s) { vector<int>tpath; for (int i = 0;i < pathlength;i++) tpath.push_back(point[path[i]].weight); ans.push_back(tpath); } } else { for (int i = 0;i < point[p].child.size();i++) { path[pathlength] = point[p].child[i]; DFS(point[p].child[i], sum + point[point[p].child[i]].weight, pathlength + 1); } } } int main() { int i, j; scanf("%d%d%d", &n, &m, &s); for (i = 0;i < n;i++) scanf("%d", &point[i].weight); for (i = 0;i < m;i++) { int father; scanf("%d", &father); int numchild, haizi; scanf("%d", &numchild); for (j = 0;j < numchild;j++) { scanf("%d", &haizi); point[father].child.push_back(haizi); } //sort(point[father].child.begin(), point[father].child.end(), cmp); } DFS(0, point[0].weight, 0); sort(ans.begin(), ans.end(), cmp); for (i = 0;i < ans.size();i++) { printf("%d", point[0].weight); for (j = 0; j < ans[i].size(); j++) printf(" %d", ans[i][j]); printf("\n"); } return 0; } /*第二次写*/ #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<algorithm> using namespace std; #define MAX 100 int n, m, s, weight[MAX], numans = 0; vector<int>Adj[MAX], ans[MAX], path; void DFS(int root, int w, int x) { path.push_back(root); if (Adj[root].size() == 0) { if (w == s) { ans[numans] = path; numans++; } } for (int i = 0; i < Adj[root].size(); i++) { int v = Adj[root][i]; DFS(v, w + weight[v], x + 1); path.pop_back(); } } bool cmp(vector<int> a, vector<int> b) { int i = 0; while (i < a.size() && i < b.size() && weight[a[i]] == weight[b[i]])i++; if (i == a.size())return 0; //有值的地方均相等,则路径长的排在前面 if (i == b.size())return 1; return weight[a[i]] > weight[b[i]]; } int main() { int i, j, k, f, t, root = 0; scanf("%d%d%d", &n, &m, &s); for (i = 0; i < n; i++) scanf("%d", &weight[i]); for (i = 0; i < m; i++) { scanf("%d%d", &f, &k); if (f == 0)root = f; for (j = 0; j < k; j++) { scanf("%d", &t); Adj[f].push_back(t); } } DFS(root, weight[root], 0); sort(ans, ans + numans, cmp); for (i = 0; i < numans; i++) { printf("%d", weight[0]); for (j = 1; j < ans[i].size(); j++) printf(" %d", weight[ans[i][j]]); printf("\n"); } return 0; }

 

最新回复(0)