Simple question.
First, I use the adjacency table to save. Then for each leaf node Dfs, DFS depth over K on return, found the leaf node is recorded, the final sequence, and then output the answer.
Because the node number is more wonderful, so use two map to convert it. This code is very ugly, written in the middle of the night, vaguely.
#include <cstdio>#include<cstring>#include<cmath>#include<map>#include<string>#include<vector>#include<iostream>#include<algorithm>using namespacestd;Chars[ +];map<int,int>Zh;map<int,int>Ff;vector<int>ljb[100000];intflag[100000], rr[100000];intls, K, ye;intu[ +], k[ the];structans{intA, B, C;} ans[10000+Ten];intGH =0;BOOLcmpConstAns&a,Constans&b) { if(A.A = = B.A&&A.B = = b.b)returna.c<B.C; if(A.A = = B.A)returnff[a.b]<ff[b.b]; returnff[a.a]<FF[B.A];}voidDFS (intNowintb) { if(b>k)return; inti; if(Flag[now] = =1&& Now! =ye) {Ans[gh]. A=ye; ANS[GH]. B=Now ; ANS[GH]. C=b; GH++; return; } for(i =0; I<ljb[now].size (); i++) { if(Rr[ljb[now][i]] = =0) {Rr[ljb[now][i]]=1; DFS (Ljb[now][i], b+1); } }}intMain () {intN, I, J, O; while(~SCANF ("%d", &K)) {scanf ("%d", &N); Zh.clear (); Ff.clear (); memset (Flag,0,sizeof(flag)); intp =1, tot; GH =0; zh[774388357] =p; FF[P]=774388357; P++; for(i =0; i<100000; i++) ljb[i].clear (); for(o =0; o<n; o++) {scanf ("%d%s", &K[o], s); LS=0; if(Zh[k[o]] = =0) Zh[k[o]] = p, ff[p] = K[o], p++; Flag[zh[k[o] ]=1; intLen =strlen (s); J=0; tot =0; for(i =0; I <= Len; i++) { if(S[i] = =','|| S[i] = =' /') { if(Zh[ls] = =0) Zh[ls] = p,ff[p] = ls,p++; U[tot]= Zh[ls], tot++, ls =0; } Elsels = ls *Ten+ S[i]-'0'; } ljb[1].push_back (u[0]); ljb[u[0]].push_back (1); for(i =1; i<tot; i++) {Ljb[u[i-1]].push_back (U[i]); Ljb[u[i]].push_back (U[i-1]); } Ljb[u[tot-1]].push_back (Zh[k[o]); Ljb[zh[k[o]]].push_back (U[tot-1]); } for(i =0; i<n; i++) {ye=Zh[k[i]]; memset (RR,0,sizeof(RR)); Rr[ye]=1; DFS (Ye,0); } sort (ans, ans+GH, CMP); for(i =0; i<gh; i++) {cout<<Ff[ans[i]. A]; cout<<" "; cout<<Ff[ans[i]. B]; cout<<" "; cout<<Ans[i]. C cout<<Endl; } } return 0;}
ACdream1728 SJY ' s first Task