-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_List_Sorting.cpp
53 lines (48 loc) · 979 Bytes
/
Linked_List_Sorting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<algorithm>
#include<string>
struct list
{
int date;
int next;
};
list l[1000000];
list g[1000000];
int count = 0;
bool cmp(list a,list b)
{
return a.date < b.date;
}
int main()
{
std::ios::sync_with_stdio(false);
int n, begin;
std::cin >> n >> begin;
for (int i = 0; i < n; i++)
{
int adress;
std::cin >> adress;
std::cin >> l[adress].date >> l[adress].next;
}
while (begin!=-1)
{
g[count].date = l[begin].date;
g[count].next = begin;
count++;
begin = l[begin].next;
}
std::sort(g, g + count, cmp);
std::cout << count<<" ";
for (int i = 0; i < count; i++)
{
std::string s = std::to_string(g[i].next);
for (int j = s.size(); j < 5; j++)
{
s = "0" + s;
}
std::cout << s << std::endl
<< s << " " << g[i].date << " ";
}
std::cout << -1;
return 0;
}