-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgraph.cpp
More file actions
92 lines (77 loc) · 1.59 KB
/
Copy pathgraph.cpp
File metadata and controls
92 lines (77 loc) · 1.59 KB
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
#include "graph.h"
#include "gadget.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
Graph::Graph() {
n=m=0;
degree=start=elist=NULL;
}
Graph::Graph(char *fname) {
if (n>0) {
fprintf(stderr, "graph already loaded\n");
exit(1);
}
build(fname);
}
Graph::~Graph() {
if (start != NULL) {
free(start);
free(degree);
free(elist);
}
}
int Graph::V() {
return n;
}
int Graph::E() {
return m;
}
void Graph::build(char *fname) {
FILE *fg = fopen(fname, "r");
fscanf(fg, "%d%d", &n, &m);
start = (int*)alloc(n);
degree = (int*)alloc(n);
elist = (int*)alloc(m);
gvec.clear();
gvec.resize(n);
int ecur=0;
for (int i=0; i<n; ++i) {
int u, d, newd=0;
fscanf(fg, "%d%d", &u, &d);
assert(u<n && u>=0);
start[u] = ecur;
for (int j=0; j<d; ++j) {
int v;
fscanf(fg, "%d", &v);
if (v==u) continue;
assert(v<n && v>=0);
elist[ecur++] = v;
gvec[u].push_back(v);
++newd;
}
degree[u] = newd;
sort(elist+start[u], elist+start[u]+newd);
}
fclose(fg);
}
int Graph::dest(int v, int k) {
return elist[start[v]+k];
}
int Graph::deg(int v) {
return degree[v];
}
void Graph::print() {
for (int i=0; i<n; ++i) {
printf("%d:", i+1);
for (int j=0; j<deg(i); ++j) {
int v = dest(i,j)+1;
printf(" %d", v);
}
printf("\n");
}
}