L2 - 022. 체인 리 셋
1063 단어 PAT & 사다리
#include
#include
#include
using namespace std;
struct node1{
int data;
int next;
};
struct node2{
int data;
int next;
int add;
};
vector a(100005);
vector b,c;
int main(void){
int head,n;
cin>>head>>n;
for(int i = 0; i < n; i++){
int add;
cin>>add;
cin>>a[add].data>>a[add].next;
}
int s = head;
while(s != -1){
struct node2 temp;
temp.data = a[s].data;
temp.next = a[s].next;
temp.add = s;
b.push_back(temp);
s = a[s].next;
}
int fir = 0,en = b.size() - 1;
for(int i = 0; i < b.size(); i++){
if(i % 2){
c.push_back(b[fir]);
fir++;
}
else{
c.push_back(b[en]);
en--;
}
}
printf("%05d ",c[0].add);
for(int i = 0; i < c.size() - 1; i++){
printf("%d %05d
%05d ",c[i].data,c[i + 1].add,c[i + 1].add);
}
printf("%d -1
",c[c.size() - 1].data);
return 0;
}