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
|
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char ** argv) {
char * buf = malloc(500);
fgets(buf, 500, stdin);
char * cp;
int n = strtoll(buf, &cp, 10);
cp++;
int k = strtoll(cp, NULL, 10);
int * a = calloc(n+1, sizeof(int));
int * t = calloc(n+1, sizeof(int)); /* t[x] = ekipa št <od 1 do k> */
realloc(buf, (n+1)*6);
fgets(buf, (n+1)*6, stdin);
cp = buf;
int i = 0; /* i bo število zadnjega in hkrati število vseh, kajti prvi je 1 */
do {
int ai = strtoll(cp, &cp, 10);
if (ai != 0)
a[++i] = ai;
cp++;
} while (cp[-1] != 0 && cp[-1] != '\n' && cp[-1] != '\r');
if (i != n)
fprintf(stderr, "error %d ni %d\n", n, i);
int ze = 0; /* referencaj novo ekipo po (++ze % k)+1 */
k1:
for (int j = 1; j <= n; j++) {
if (!t[(i = j)])
goto k2;
}
goto konec;
k2:
/*
e = 0;
while (!t[zn]) {
if (!e++)
fprintf(stdout, "%d ", zn);
if (t[a[zn]]) { // če je naš a že v ekipi, i damo v to ekipo
t[zn] = t[a[zn]];
zn++;
}
t[zn] = (ze % k)+1;
zn = a[zn];
}
ze++;
*/
if (ze)
fprintf(stdout, " ");
fprintf(stdout, "%d", i);
t[i] = ++ze;
k3:
if (t[a[i]])
goto k1;
k4:
i = a[i];
k5:
t[i] = ze;
goto k3;
konec:
fprintf(stdout, "\n");
free(buf);
return 0;
}
|