PAT1053-Path of Equal Weight

Source: Internet
Author: User

I am not serious about reading the questions. I am entangled for a long time: Each path occupies a line with printed weights from the root to the leaf in order. in fact, the final output is sorted by the path (int) stack [n] is saved to (char) S [I], and S [I] is sorted by string to obtain the output results that meet the requirements. C language source code: [cpp] # include <stdio. h> # include <stdlib. h> # include <string. h> typedef struct child {int num; int w;} child; typedef struct node {int w; int top; child c [200];} node; node A [200]; int cmp (const void * a, const void * B) {child * aa = (child *) a; child * bb = (child *) B; return bb-> w-aa-> w;} int cmp1 (const void * a, const void * B) {return strcmp (char *) B, (char *) a);} int Stack [200], top, sum, s; char S [200] [200]; int tops [200], nu; void dfs (int k) {int I; sum + = A [k]. w; Stack [top ++] = A [k]. w; if (A [k]. top = 0) {if (sum = s) {tops [nu] = top; for (I = 0; I <top; I ++) S [nu] [I] = Stack [I]; S [nu] [I] = '\ 0'; nu ++ ;}} else for (I = 0; I <A [k]. top; I ++) dfs (A [k]. c [I]. num); sum-= A [k]. w; top --;} int main () {int n, m, I, num, j, x; scanf ("% d", & n, & m, & s); for (I = 0; I <200; I ++) {A [I]. top = 0; tops [I] = 0 ;}for (I = 0; I <n; I ++) scanf ("% d", & A [I]. w); for (I = 0; I <m; I ++) {scanf ("% d", & num); scanf ("% d ", & A [num]. top); for (j = 0; j <A [num]. top; j ++) {scanf ("% d", & x); A [num]. c [j]. num = x; A [num]. c [j]. w = A [x]. w;}/* qsort (A [num]. c, A [num]. top, sizeof (A [num]. c [0]), cmp); */} www.2cto.com top = 0; sum = 0; nu = 0; dfs (0); qsort (S, nu, sizeof (S [0]), cmp1); for (I = 0; I <nu; I ++) {for (j = 0; j <(int) strlen (S [I])-1; j ++) printf ("% d", S [I] [j]); printf ("% d \ n ", S [I] [j]);} return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.