1053. Path of Equal Weight (30)
来源:互联网 发布:淘宝首页资源位 编辑:程序博客网 时间:2024/05/01 11:55
题目传送门:https://www.patest.cn/contests/pat-a-practise/1053
AC代码
//不难 但一定要细心,要求是叶子节点到根的weight之和,叶子节点 根,缺一不可.
#include <iostream>#include <string>#include <cstdio>#include <vector>#include <queue>#include <algorithm>#include <stack>#include <map>using namespace std;struct Node{ int weight,id,childNum; Node * parent; Node(int _id,int _weight):weight(_weight),id(_id),parent(NULL),childNum(0){};};int main(){ int N,M,t,targetWeight,id,k; scanf("%d %d %d",&N,&M,&targetWeight); vector<Node *>tree; for (int i=0; i<N; ++i) { scanf("%d",&t); Node * p = new Node(i,t); tree.push_back(p); } for (int i=0; i<M; ++i) { scanf("%d %d",&id,&k); tree[id]->childNum = k; for (int j=0; j<k; ++j) { scanf("%d",&t); tree[t]->parent = tree[id]; } } vector<vector<int>>allPath; for (int i=0; i<N; ++i) { int curWeight = 0; Node * p = tree[i]; if(p->childNum) continue; vector<int>curPath; while (true) { curWeight = curWeight + p->weight; curPath.push_back(p->weight); if(curWeight ==targetWeight){ if(p==tree[0]) allPath.push_back(curPath); break; } else if(curWeight>targetWeight) break; p = p->parent; if(!p) break; } } sort(allPath.begin(), allPath.end(), [](vector<int> a,vector<int> b){ reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int len = a.size()<b.size()?(int)a.size():(int)b.size(); bool flag = false; for (int i=0; i<len; ++i) { if(a[i]!=b[i]){ flag = a[i]>b[i]; break; } } return flag; }); for (int i=0; i<allPath.size(); ++i) { for (int j=(int)allPath[i].size()-1; j>=0; --j) { printf(j==0?"%d\n":"%d ",allPath[i][j]); } } return 0;}
0 0
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)-PAT
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 集训队专题(8)1005 小明系列故事——未知剩余系
- 分布式架构下的会话追踪实践【基于Cookie和Redis实现】
- UART 初始化及使用示例代码
- 模板匹配函数详解
- PAT甲级1001-Public Bike Management (30)
- 1053. Path of Equal Weight (30)
- Asp.net B/S模式下使用Console.Write()
- 字符串和字典的相互转化
- 共享onload事件的使用(javascriptDOM编程艺术学习笔记)
- java内存模型
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- Numpy使用3
- UISegment
- Android bootanimation 制作过程