zoukankan      html  css  js  c++  java
  • [PAT] A1053 Path of Equal Weight

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

    题目大意

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

    思路

    算法笔记P307解法存在一个问题:一开始在输入时就对一个结点下的所有子节点按照权重排序,则如果子节点权重相等的话,分不清谁大谁小。
    解决方案:路径全部保存下来,最后再排序。

    AC代码

    #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("
    ");
    		}
    	}
    	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("
    ");
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    CachedRowSet使用
    mybatis There is no getter for property named 'xx' in 'class java.lang.String
    基于tcpdump的Android智能移动终端数据包捕获完整解决方案
    analytics详解
    android开发图片分辨率
    缩放图片,解决bitmap 内存溢出out of memory的问题
    使用windowAnimations定义Activity及Dialog的进入退出效果
    读取本地已有的.db数据库
    MyBatis 问题列表
    cxf 相关问题
  • 原文地址:https://www.cnblogs.com/yue36/p/13296626.html
Copyright © 2011-2022 走看看