-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbliss2dre.c
161 lines (143 loc) · 3.11 KB
/
bliss2dre.c
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
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
/* Reads a graph in Bliss format from stdin and writes it in dreadnaut
* format to stdout. If there is an argument, it is written to the
* output before the graph. If there are two arguments, they are
* written to the output before and after the graph.
*/
typedef struct
{
int v,w;
} vpair;
int comparedge(const void *p1, const void *p2)
{
vpair *e1,*e2;
e1 = (vpair*)p1;
e2 = (vpair*)p2;
if (e1->v < e2->v) return -1;
else if (e1->v > e2->v) return 1;
else if (e1->w < e2->w) return -1;
else if (e1->w > e2->w) return 1;
else return 0;
}
static int
nextchar(void)
{
char s[2];
if (scanf("%1s",s) != 1) return EOF;
else return s[0];
}
int
main(int argc, char *argv[])
{
int n,c;
unsigned long ne,j;
vpair *elist,*vlist;
int haven;
int i,v,w;
int haveptn;
haven = 0;
j = 0;
while ((c = nextchar()) >= 0)
{
switch (c)
{
case 'c':
putchar('"');
while ((c = getchar()) != '\n' && c != EOF) putchar(c);
printf("\\n\"\n");
break;
case 'p':
if (haven)
{
fprintf(stderr,"Duplicate p line\n");
exit(1);
}
if (scanf(" edge %d %lu",&n,&ne) != 2)
{
fprintf(stderr,"Bad p line\n");
exit(1);
}
if ((elist = (vpair*)malloc(ne*sizeof(vpair))) == NULL
|| (vlist = (vpair*)malloc(n*sizeof(vpair))) == NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
haven = 1;
for (i = 0; i < n; ++i)
{
vlist[i].v = 0; /* default colour */
vlist[i].w = i+1;
}
break;
case 'n':
if (!haven)
{
fprintf(stderr,"Missing p line\n");
exit(1);
}
if (scanf("%d%d",&w,&v) != 2 || w < 1 || w > n)
{
fprintf(stderr,"Bad n line\n");
exit(1);
}
vlist[w-1].v = v;
break;
case 'e':
if (!haven || j == ne)
{
fprintf(stderr,"Missing p line or too many e lines\n");
exit(1);
}
if (scanf("%d%d",&v,&w) != 2 || v < 1 || w < 1 || v > n || w > n)
{
fprintf(stderr,"Bad e line\n");
exit(1);
}
elist[j].v = v; elist[j].w = w;
++j;
break;
default:
fprintf(stderr,"Unknown line %c\n",c);
exit(1);
}
}
if (j != ne)
{
fprintf(stderr,"Wrong number of e lines\n");
exit(1);
}
if (argc > 1) printf("%s\n",argv[1]);
printf("$=1 n=%d g\n",n);
qsort(elist,ne,sizeof(vpair),comparedge);
v = -1;
for (j = 0; j < ne; ++j)
{
if (elist[j].v != v)
{
v = elist[j].v;
if (j > 0) printf("\n");
printf("%d:",v);
}
printf(" %d",elist[j].w);
}
printf(".\n");
qsort(vlist,n,sizeof(vpair),comparedge);
for (i = 1; i < n; ++i)
if (vlist[i].v != vlist[i-1].v) break;
if (i < n)
{
printf("f=[");
for (i = 0; i < n; ++i)
{
if (i > 0 && vlist[i].v != vlist[i-1].v)
printf("\n |");
printf(" %d",vlist[i].w);
}
printf("]\n");
}
printf("$$\n");
if (argc > 2) printf("%s\n",argv[2]);
return 0;
}