Pagini recente » Istoria paginii runda/aicluj_cls11 | Cod sursa (job #1438564) | Cod sursa (job #1801943) | Cod sursa (job #2002466) | Cod sursa (job #163679)
Cod sursa(job #163679)
#include <stdio.h>
#define nm 5010
int n, last[nm], crt[nm], next[nm], sol[nm];
int main()
{
freopen("sortare.in", "r", stdin);
freopen("sortare.out", "w", stdout);
scanf("%d", &n);
crt[0] = crt[1] = 1;
sol[0] = sol[1] = 1;
for (int i = 1; i < n; ++i) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if (x > y)
x += y, y = x - y, x -= y;
if (y > z)
y += z, z = y - z, y -= z;
if (x > y)
x += y, y = x - y, x -= y;
if (x == y || y == z) {
if (x == y)
next[x] = 1;
else
next[z] = 1;
for (int tmp = 1, j = 1; j <= i + 1; ++j)
if (!next[j])
next[j] = crt[tmp++] + 1;
sol[i + 1] = sol[i] + 1;
} else {
next[x] = 1;
next[y] = 2;
for (int tmp = 1, j = 1; j <= i + 1; ++j)
if (!next[j])
next[j] = last[tmp++] + 2;
sol[i + 1] = sol[i - 1] + 1;
}
for (int j = 1; j <= i + 1; ++j)
last[j] = crt[j], crt[j] = next[j], next[j] = 0;
}
printf("%d\n", sol[n]);
for (int i = 1; i <= n; ++i)
printf("%d ", crt[i]);
printf("\n");
return 0;
}