日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1053 Path of Equal Weight

發布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1053 Path of Equal Weight 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1053?Path of Equal Weight?(30?分)

Given a non-empty tree with root?R, and with weight?W?i???assigned to each tree node?T?i??. The?weight of a path from?R?to?L?is defined to be the sum of the weights of all the nodes along the path from?R?to any leaf node?L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.

Input Specification:

Each input file contains one test case. Each case starts with a line containing?0<N≤100, the number of nodes in a tree,?M?(<N), the number of non-leaf nodes, and?0<S<2?30??, the given weight number. The next line contains?N?positive numbers where?W?i???(<1000) corresponds to the tree node?T?i??. Then?M?lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where?ID?is a two-digit number representing a given non-leaf node,?K?is the number of its children, followed by a sequence of two-digit?ID's of its children. For the sake of simplicity, let us fix the root ID to be?00.

Output Specification:

For each test case, print all the paths with weight S in?non-increasing?order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence?{A?1??,A?2??,?,A?n??}?is said to be?greater than?sequence?{B?1??,B?2??,?,B?m??}?if there exists?1≤k<min{n,m}?such that?A?i??=B?i???for?i=1,?,k, and?A?k+1??>B?k+1??.

Sample Input:

20 9 24 10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2 00 4 01 02 03 04 02 1 05 04 2 06 07 03 3 11 12 13 06 1 09 07 2 08 10 16 1 15 13 3 14 16 17 17 2 18 19

Sample Output:

10 5 2 7 10 4 10 10 3 3 6 2 10 3 3 6 2

解析:用vector存樹,按照weight從大到小排序,再dfs中記錄路徑。

#include<set> #include<map> #include<list> #include<queue> #include<deque> #include<cmath> #include<stack> #include<cstdio> #include<string> #include<bitset> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #include<bits/stdc++.h> using namespace std;#define e exp(1) #define p acos(-1) #define mod 1000000007 #define inf 0x3f3f3f3f #define ll long long #define ull unsigned long long #define mem(a,b) memset(a,b,sizeof(a)) int gcd(int a,int b) {return b?gcd(b,a%b):a; }struct node{int weight;vector<int> children; }; vector<node> tree; vector<int> path; int n,m,w;bool cmp(int a,int b) {return tree[a].weight>tree[b].weight; }void dfs(int s,int sum) {if(sum>w)return ;if(sum==w){if(tree[s].children.size()!=0)return ;printf("%d",tree[0].weight);for(int i=0; i<path.size(); i++){printf(" %d",path[i]);}printf("\n");}for(int i=0; i<tree[s].children.size(); i++){int now=tree[s].children[i];path.push_back(tree[now].weight);dfs(now,sum+tree[now].weight);path.pop_back();} } int main() {scanf("%d%d%d",&n,&m,&w);tree.resize(n);for(int i=0; i<n; i++){scanf("%d",&tree[i].weight);}int id,k;for(int i=0; i<m; i++){scanf("%d%d",&id,&k);tree[id].children.resize(k);for(int j=0; j<k; j++){scanf("%d",&tree[id].children[j]);}sort(tree[id].children.begin(),tree[id].children.end(),cmp);}dfs(0,tree[0].weight);return 0; }

?

總結

以上是生活随笔為你收集整理的1053 Path of Equal Weight的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产丝袜一区二区 | 爱逼综合 | 精品一区二区毛片 | 欧美日韩视频免费 | 午夜精品电影 | 亚色视频在线观看 | 国产欧美日韩精品区一区二污污污 | 亚洲欧美婷婷 | 黄在线免费观看 | 人人爽视频 | 天天干天天天 | 一级特黄视频 | 在线99热 | 黄色片免费播放 | 精品久久一区二区三区 | 日本在线视频免费观看 | 国产午夜伦理 | 青青草免费观看 | 超碰激情 | 久久av在线播放 | 国产午夜精品久久久久久久久久 | 国产成人在线影院 | 狠狠躁18三区二区一区 | 在线观看日韩中文字幕 | av鲁丝一区鲁丝二区鲁丝 | 青青草超碰在线 | 舌奴调教日记 | av黄在线| 亚洲日本韩国在线 | 一区二区国产电影 | 久99久视频 | 午夜在线不卡 | 男人和女人插插 | 国产黄a三级三级看三级 | 欧美成人精品二区三区99精品 | 四虎啪啪 | 久久国内偷拍 | 日本午夜免费福利视频 | 国产一区二区视频免费在线观看 | 欧美人与禽zozzo禽性配 | 国产精品mv| 手机在线亚洲 | 亚洲国产成人精品久久 | 手机成人在线视频 | 国产精品久免费的黄网站 | 女人被灌满精子 | 潘金莲三级野外 | 亚洲欧美日韩久久 | 久久久久国产 | 四虎在线精品 | 亚洲码视频 | 国产13页 | 国产99久久久久 | 日韩久久不卡 | 欧美日韩视频免费观看 | 青娱乐在线免费观看 | 中文字幕资源网 | 狠狠涩| 尤物国产视频 | 黄色一区二区三区 | a网站在线观看 | 男生操女生屁股 | 国产又粗又猛 | 香蕉国产在线视频 | 少妇与公做了夜伦理69 | 欧美变态网站 | 伊人精品视频 | 亚洲一区精品在线观看 | 亚洲欧美高清在线 | 亚洲精品美女视频 | 欧美性受xxxx白人性爽 | 一亲二脱三插 | 国产黄在线播放 | 天天干天天操天天操 | 国产欧美一区二区 | 亚洲综合天堂 | 姑娘第5集高清在线观看 | 先锋影音一区二区三区 | 成年人网站免费视频 | 色人阁视频 | 成人乱人乱一区二区三区一级视频 | 久久无码专区国产精品s | 国产深夜福利在线 | 欧美性生活精品 | 精品国产一区二区三区久久狼黑人 | 免费一级a毛片夜夜看 | 91精品国产乱码 | 国产性猛交xxxⅹ交酡全过程 | 亚洲熟妇中文字幕五十中出 | xxxxxx日本 | 色吟av| 中文字幕超碰在线 | 国产超碰自拍 | 欧美视频在线观看一区二区 | 久久精品美乳 | 少妇看片 | 99热日本| 亚洲一区在线免费 | 精品国产免费av |