#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 70000
#define M 70000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *ej[N], eo[N], *ei[M], eo_[M];
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int pp[N], dd[N];
void dfs1(int p, int i, int d) {
int o;
pp[i] = p, dd[i] = d;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs1(i, j, d + 1);
}
}
char type[M]; int ii[M], jj[M], ww[M];
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k)
if (ww[hh[j]] == ww[h])
j++;
else if (ww[hh[j]] < ww[h]) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
sort(hh, l, i);
l = k;
}
}
char used[N]; int pp_[N];
int find_(int i) {
return !used[i] ? i : (pp_[i] = find_(pp_[i]));
}
int ds[M];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
int join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return 0;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
return 1;
}
int xx[N], hh1[N], hh2[N]; char visited[M];
void dfs2(int p, int h) {
int o;
if (p != -1 && xx[p] == 0)
xx[p] = ww[h];
if (visited[h])
return;
visited[h] = 1;
for (o = eo_[h]; o--; ) {
int i = ei[h][o], h_ = h ^ hh1[i] ^ hh2[i];
if (i != p)
dfs2(i, h_);
}
}
int main() {
static int hh[M];
int n, m, h, h_, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(ej, eo, i, j), append(ej, eo, j, i);
}
dfs1(-1, 0, 0);
scanf("%d", &m);
for (h = 0; h < m; h++) {
static char s[2];
scanf("%s%d%d%d", s, &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
type[h] = s[0] == 'm' ? 0 : 1;
}
for (h = 0; h < m; h++)
hh[h] = h;
sort(hh, 0, m);
memset(used, 0, n * sizeof *used);
memcpy(pp_, pp, n * sizeof *pp_);
memset(hh1, -1, n * sizeof *hh1);
for (h = m - 1; h >= 0; h--) {
h_ = hh[h];
if (type[h_] == 0) {
i = ii[h_], j = jj[h_];
while (1) {
i = find_(i), j = find_(j);
if (i == j)
break;
if (dd[i] > dd[j])
hh1[i] = h_, used[i] = 1;
else
hh1[j] = h_, used[j] = 1;
}
}
}
memset(used, 0, n * sizeof *used);
memcpy(pp_, pp, n * sizeof *pp_);
memset(hh2, -1, n * sizeof *hh2);
for (h = 0; h < m; h++) {
h_ = hh[h];
if (type[h_] == 1) {
i = ii[h_], j = jj[h_];
while (1) {
i = find_(i), j = find_(j);
if (i == j)
break;
if (dd[i] > dd[j])
hh2[i] = h_, used[i] = 1;
else
hh2[j] = h_, used[j] = 1;
}
}
}
for (i = 0; i < n; i++)
if (hh1[i] == -1)
hh1[i] = hh2[i];
else if (hh2[i] == -1)
hh2[i] = hh1[i];
for (h = 0; h < m; h++)
ei[h] = (int *) malloc(2 * sizeof *ei[h]);
for (i = 0; i < n; i++)
if (hh1[i] != -1)
append(ei, eo_, hh1[i], i), append(ei, eo_, hh2[i], i);
memset(ds, -1, m * sizeof *ds);
for (i = 0; i < n; i++)
if (hh1[i] != -1 && !join(hh1[i], hh2[i]) && !visited[hh1[i]])
dfs2(-1, hh1[i]);
for (i = 1; i < n; i++)
printf("%d %d %d\n", pp[i] + 1, i + 1, xx[i]);
return 0;
}
Compilation message
minmaxtree.c: In function 'append':
minmaxtree.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
minmaxtree.c: In function 'main':
minmaxtree.c:108:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
minmaxtree.c:112:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
minmaxtree.c:116:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d", &m);
| ^~~~~~~~~~~~~~~
minmaxtree.c:120:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | scanf("%s%d%d%d", s, &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
14180 KB |
Output is correct |
2 |
Correct |
82 ms |
10868 KB |
Output is correct |
3 |
Correct |
79 ms |
12344 KB |
Output is correct |
4 |
Correct |
86 ms |
14848 KB |
Output is correct |
5 |
Correct |
79 ms |
11964 KB |
Output is correct |
6 |
Correct |
89 ms |
11476 KB |
Output is correct |
7 |
Correct |
80 ms |
10772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
8032 KB |
Output is correct |
2 |
Correct |
59 ms |
9560 KB |
Output is correct |
3 |
Correct |
55 ms |
11968 KB |
Output is correct |
4 |
Correct |
57 ms |
13484 KB |
Output is correct |
5 |
Correct |
60 ms |
10356 KB |
Output is correct |
6 |
Correct |
61 ms |
10836 KB |
Output is correct |
7 |
Correct |
61 ms |
9852 KB |
Output is correct |
8 |
Correct |
60 ms |
9692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
87 ms |
14180 KB |
Output is correct |
6 |
Correct |
82 ms |
10868 KB |
Output is correct |
7 |
Correct |
79 ms |
12344 KB |
Output is correct |
8 |
Correct |
86 ms |
14848 KB |
Output is correct |
9 |
Correct |
79 ms |
11964 KB |
Output is correct |
10 |
Correct |
89 ms |
11476 KB |
Output is correct |
11 |
Correct |
80 ms |
10772 KB |
Output is correct |
12 |
Correct |
53 ms |
8032 KB |
Output is correct |
13 |
Correct |
59 ms |
9560 KB |
Output is correct |
14 |
Correct |
55 ms |
11968 KB |
Output is correct |
15 |
Correct |
57 ms |
13484 KB |
Output is correct |
16 |
Correct |
60 ms |
10356 KB |
Output is correct |
17 |
Correct |
61 ms |
10836 KB |
Output is correct |
18 |
Correct |
61 ms |
9852 KB |
Output is correct |
19 |
Correct |
60 ms |
9692 KB |
Output is correct |
20 |
Correct |
87 ms |
13320 KB |
Output is correct |
21 |
Correct |
76 ms |
12168 KB |
Output is correct |
22 |
Correct |
75 ms |
12256 KB |
Output is correct |
23 |
Correct |
78 ms |
12268 KB |
Output is correct |
24 |
Correct |
101 ms |
17108 KB |
Output is correct |
25 |
Correct |
97 ms |
18516 KB |
Output is correct |
26 |
Correct |
87 ms |
17744 KB |
Output is correct |
27 |
Correct |
92 ms |
16428 KB |
Output is correct |
28 |
Correct |
95 ms |
13916 KB |
Output is correct |
29 |
Correct |
88 ms |
14028 KB |
Output is correct |
30 |
Correct |
83 ms |
12880 KB |
Output is correct |
31 |
Correct |
86 ms |
12880 KB |
Output is correct |
32 |
Correct |
88 ms |
13516 KB |
Output is correct |
33 |
Correct |
86 ms |
12816 KB |
Output is correct |
34 |
Correct |
84 ms |
12900 KB |
Output is correct |
35 |
Correct |
83 ms |
12444 KB |
Output is correct |