define _CRT_SECURE_NO_WARNI NGS include iostream include queue include

  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
int parnt[] = { -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3 };
int level[13] = { 0 };
int getLevel(int cur)
{
if (level[cur] != -1)
return level[cur];
if (parnt[cur] == -1)
{
level[cur] = 1;
return 1;
}
level[cur] = getLevel(parnt[cur]) + 1;
return level[cur];
}
struct edge
{
int a, b;
edge(int a = 0, int b = 0) :a(a), b(b){};
};
edge arr[13];
class PuchkiDug
{
private:
int* heads;
int* links;
edge* edges_p;
int n;
int m;
public:
PuchkiDug(edge* allEdges, int n, int m) : n(n), m(m)
{
edges_p = allEdges;
heads = new int[n];
links = new int[m];
memset(heads, -1, sizeof(int) * n);
memset(links, -1, sizeof(int) * m);
for (int i = 0; i < m; ++i)
{
links[i] = heads[edges_p[i].a];
heads[edges_p[i].a] = i;
}
}
void printData()
{
for (int i = 0; i < n; ++i)
{
int cr = heads[i];
while (cr != -1)
{
cout << edges_p[cr].b << " ";
cr = links[cr];
}
cout << endl;
}
}
void getParentTree(int* outp, int start)
{
memset(outp, -1, sizeof(int)* n);
queue<int> que;
que.push(start);
outp[start] = -2;
while (!que.empty())
{
int cr = que.front();
que.pop();
int hd = heads[cr];
while (hd != -1)
{
int to = edges_p[hd].b;
if (outp[to] == -1)
{
outp[to] = cr;
que.push(to);
}
hd = links[hd];
}
}
}
};
int main()
{
memset(level, -1, sizeof(int) * 13);
arr[0] = edge(0, 1);
arr[1] = edge(0, 2);
arr[2] = edge(0, 3);
arr[3] = edge(1, 4);
arr[4] = edge(1, 5);
arr[5] = edge(1, 6);
arr[6] = edge(2, 7);
arr[7] = edge(2, 8);
arr[8] = edge(2, 9);
arr[9] = edge(3, 10);
arr[10] = edge(3, 11);
arr[11] = edge(3, 12);
arr[12] = edge(3, 0);
PuchkiDug puch(arr, 13, 13);
int tstarr[13];
puch.getParentTree(tstarr, 3);
// freopen("out.txt", "w", stdout);
for (int i = 0; i < 13; ++i)
{
if (tstarr[i] < 0)
continue;
cout << i << "->" << tstarr[i] << ";" << endl;
}
// puch.printData();
}