天梯

输入样例:

00100 6
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出样例:

68237 6 00100
00100 1 99999
99999 5 12309
12309 2 00000
00000 4 33218
33218 3 -1
 1 #include <bits/stdc++.h>
 2 const int INF=0x3f3f3f3f;
 3 typedef long long LL;
 4 const double eps =1e-8;
 5 const int mod=1e9+7;
 6 const int maxn=1e5+10;
 7 using namespace std;
 8 
 9 struct node
10 {
11     string ad;//地址 
12     int num;//数据 
13     string next;//下一个 
14 }P[maxn];
15 int rk[maxn];//排名为i的节点的序号 
16 string rt;//首地址 
17 int n;
18 int st;//开始的序号 
19 map<string,int> mp;
20 
21 int main()
22 {
23     #ifdef DEBUG
24     freopen("sample.txt","r",stdin);
25     #endif
26     
27     cin>>rt>>n;
28     for(int i=1;i<=n;i++)
29     {
30         cin>>P[i].ad>>P[i].num>>P[i].next;
31         mp[P[i].ad]=i;
32         if(P[i].ad==rt) st=i;
33     }
34     int cnt=0;
35     while(st!=-1)
36     {
37         rk[++cnt]=st;
38         if(P[st].next!="-1") st=mp[P[st].next];
39         else st=-1;
40     }
41     int L=cnt,R=1;
42     for(int i=1;i<=cnt;i++)
43     {
44         if(i%2)
45         {
46             int t=rk[L];
47             if(i!=cnt) cout<<P[t].ad<<' '<<P[t].num<<' '<<P[rk[R]].ad<<endl;
48             else cout<<P[t].ad<<' '<<P[t].num<<' '<<"-1";
49             L--;
50         }
51         else
52         {
53             int t=rk[R];
54             if(i!=cnt) cout<<P[t].ad<<' '<<P[t].num<<' '<<P[rk[L]].ad<<endl;
55             else cout<<P[t].ad<<' '<<P[t].num<<' '<<"-1";
56             R++;
57         }
58     }
59     
60     return 0;
61 }

-

原文地址:https://www.cnblogs.com/jiamian/p/12578735.html