aboutsummaryrefslogtreecommitdiff
path: root/2023/20/graph.c
blob: bef0496186206702eae805efd26c7f82296eddc3 (plain)
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
/* Picture generated with https://csacademy.com/app/graph_editor/ */

#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100
#define ischar(c) (c >= 'a' && c <= 'z')

typedef struct {
	int nin, nout, in[N], out[N];
	char name[20], outc[N][20];
	bool isff, ison, reg[N];
} node_t;

char *buf, line[N][N];
bool rx;
int b, r, n;
int64_t npush;
node_t m[N];

int findm(char *name) {
	for (int k = 0; k < n; k++)
		if (!strcmp(m[k].name, name))
			return k;
	return n;
}

int main() {
	for (n = 0; (buf = fgets(line[n], N, stdin)) != NULL; n++) {
		if (ischar(*buf)) b = n;
		m[n].isff = *buf == '%';
		if (!ischar(*buf)) buf++;
		for (int i = 0; ischar(*buf); m[n].name[i++] = *(buf++)) ;
		buf += 4;
		for (int i = 0; *buf != '\n'; i++) {
			while (!ischar(*buf)) buf++;
			for (int j = 0; ischar(*buf); m[n].outc[i][j++] = *(buf++)) ;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; m[i].outc[j][0]; j++) {
			int k = findm(m[i].outc[j]);
			m[i].out[m[i].nout++] = k;
			m[k].in[m[k].nin++] = i;
			if (k == n) {
				for (int k = 0; m[i].outc[j][k]; k++)
					m[n].name[k] = m[i].outc[j][k];
				n++;
			}
		}
	}

	for (int i = 0; i < n; i++)
		if (!strcmp(m[i].name, "rx"))
			r = i;

/* Print adjacency list
	printf("%d\n", n);
	for (int i = 0; i < n; i++) {
		printf("%d", m[i].nout);
		for (int j = 0; j < m[i].nout; j++)
			printf(" %d", m[i].out[j]);
		printf("\n");
	}
	printf("broadcaster = %d, r = %d\n", b, r);
*/

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m[i].nout; j++)
			printf("%d %d\n", i, m[i].out[j]);

	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net