图算法和图模型(八)

这篇博文主要讲述一下状态图,隐式图建图的技巧
补充了一些最短路的拓展
$spfa()$ 求最长路算法等等

spfa求最长路

UVALive3310
UVALive3310
UVALive3310

$\textbf{algorithm}$
$\textbf{i) run }\quad \text{strongConnected?} \leftarrow \text{tarjan()}$
$\quad \quad \textbf{ if circle, } \text{ return } \infty$

$\textbf{ii) run } \text{spfa()}$
$\quad \quad \textbf{ relax }(x, y), \quad d(y) < d(x) + w(x, y)$
$\quad \quad \text{ get longest path}$

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const int maxn = 100000+5;
const int inf = 1e9;
int n, tot;
int ans;

// == Graph definition ==
int m = 0;

class Edge {
public:
int to, weight, s, t;
bool loop;
Edge *next;
Edge() {}
Edge(int v, int w, int s, int t, bool loop) : to(v), weight(w), s(s), t(t), loop(loop) {
next = NULL;
}
} edges[maxn << 1], *head[maxn];

void add(int x, int y, int w, int s, int t, bool loop) {
edges[++m] = Edge(y, w, s, t, loop);
edges[m].next = head[x];
head[x] = &edges[m];
}

int dfn[maxn], low[maxn];
int belong[maxn], ccnum[maxn];
int cc = 0;
int ins[maxn];
stack<int> stk;
int clk = 0;

void initG() {
memset(head, 0, sizeof(head));
m = clk = 0;
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(ins, 0, sizeof(ins));
while (stk.size()) stk.pop();

memset(ccnum, 0, sizeof(ccnum));
cc = 0;
memset(belong, 0, sizeof(belong));
}

void tarjan(int u) {
low[u] = dfn[u] = ++clk;
stk.push(u);
ins[u] = 1;
for(const Edge *e = head[u]; e; e = e->next) {
int v = e->to;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v]) {
low[u] = min(low[u], dfn[v]);
}
}

if(dfn[u] == low[u]) {
cc++;
while (true) {
int x = stk.top();
stk.pop();
ins[x] = false;
belong[x] = cc;
if(x == u) break;
}
}
}
// == Graph finished ==


// == build graph, important ==
const int maxl = 82;
char buf[maxl];
char Cmd[maxn][maxl];

ll nxt[maxn], len[maxn];
bool Die[maxn], vis[maxn];
int ID[maxn], pos[maxn];

void initbuild() {
memset(ID, -1, sizeof(ID));
memset(pos, -1, sizeof(pos));
memset(Die, 0, sizeof(Die));
memset(nxt, -1, sizeof(nxt));
memset(len, 0, sizeof(len));
}

bool judge(const int u, int &v, int &w) {
memset(vis, 0, sizeof(vis));
vis[u] = 1;
bool loop = false;

while (ID[v] == -1) {
vis[v] = 1;
w += len[v];
v = nxt[v];

if(vis[v]) {
loop = true;
break;
}
}
return loop;
}

void build() {
initbuild();
ID[0] = ++tot;
pos[tot] = 0;

_for(i, 0, n) {
if(Cmd[i][0] == 'l') {
int s = -1, t = -1;
sscanf(Cmd[i], "%*s%d%d", &s, &t);
s--;

if(ID[s] == -1) {
ID[s] = ++tot;
pos[tot] = s;
}
if(ID[i] == -1) {
ID[i] = ++tot;
pos[tot] = i;
}

nxt[i] = i;
}
else if(Cmd[i][0] == 'i' || Cmd[i][0] == 'j') {
int v;
sscanf(Cmd[i], "%*s%d", &v);
v--;

if(Cmd[i][0] == 'i' && ID[i] == -1) {
ID[i] = ++tot;
pos[tot] = i;
}

nxt[i] = v;
}
else if(Cmd[i][0] == 'd') {
if(ID[i] == -1) {
ID[i] = ++tot;
pos[tot] = i;
}
Die[ID[i]] = true;
nxt[i] = i;
}
else {
nxt[i] = (i + 1) % n;
}

len[i] = nxt[i] == i ? 0 : 1;
}

_forDown(i, tot, 1) {
if(Die[i]) continue;

int u = pos[i];
int v = nxt[u];
int w = len[u];

bool lp = judge(u, v, w);
if(lp) {
add(ID[u], ID[u], inf, -1, -1, false);
continue;
}

len[u] = w;
nxt[u] = v;
if(v != u) {
add(ID[u], ID[v], w, -1, -1, false);
}
if(Cmd[u][0] == 'j') continue;

// then deal with pass, loop, ifgo
// just one step
v = (u + 1) % n;
w = 1;
lp = judge(u, v, w);
if(lp) {
add(ID[u], ID[u], inf, -1, -1, false);
continue;
}

int s = -1, t = -1;
if(Cmd[u][0] == 'l') {
sscanf(Cmd[u], "%*s%d%d", &s, &t);
s--;
}
add(ID[u], ID[v], w, ID[s], t, Cmd[u][0] == 'l');
}
}
// == build finished ==

// == spfa get longest path ==
ll cnt[maxn], D[maxn];
int inq[maxn];

void initspfa() {
memset(cnt, 0, sizeof(cnt));
memset(inq, 0, sizeof(inq));
memset(D, 0, sizeof(D));
}

void spfa() {
initspfa();

queue<int> que;
int st = ID[0];
inq[st] = 1;
D[st] = 1;
que.push(st);

while (que.size()) {
int x = que.front();
que.pop();
inq[x] = false;

if(Die[x] && ans < D[x]) ans = D[x];
for(const Edge *e = head[x]; e; e = e->next) {
int y = e->to;
int w = e->weight;
if(e->loop) w += (D[x] - D[e->s] + 1) * (e->t - 1);

if(D[y] < D[x] + w) {
D[y] = D[x] + w;
if(!inq[y]) {
inq[y] = true;
que.push(y);

if(++cnt[y] > tot) {
printf("infinity\n");
return;
}
}
}
}
}
if(ans > inf) printf("infinity\n");
else cout << ans << endl;
}
// == spfa finished ==

// == get cmd ==
void getCmd() {
_for(i, 0, strlen(buf)) buf[i] = tolower(buf[i]);
char *p = strtok(buf, " ");
while (p) {
strcat(Cmd[n], p);
strcat(Cmd[n], " ");
p = strtok(NULL, " ");
}
n++;
}

bool cmdModify() {
bool die = false;
_for(i, 0, n) {
if(Cmd[i][0] == 'd') die = true;
if(Cmd[i][0] == 'i' || Cmd[i][0] == 'j') {
int v;
sscanf(Cmd[i], "%*s%d", &v);
v--;

if(v == (i + 1) % n) Cmd[i][0] = 'p';
if(i == v && !die) return false;
}
}
return die;
}
// == get cmd finished ==

void init() {
memset(Cmd, 0, sizeof(Cmd));
n = tot = 0;
}

void test() {
_for(i, 0, n) debug(Cmd[i]);
}

int main() {
freopen("input.txt", "r", stdin);
while (fgets(buf, sizeof(buf) / sizeof(buf[0]), stdin)) {
if(buf[0] == '\n') break;
init();
getCmd();

while (fgets(buf, sizeof(buf) / sizeof(buf[0]), stdin)) {
if(buf[0] == '\n') break;
_for(i, 0, strlen(buf)) if(isspace(buf[i])) buf[i] = ' ';
getCmd();
}
// one test case finished
//test();
if(!cmdModify()) {
printf("infinity\n");
continue;
}

// then build graph
initG();
build();

bool circle = false;
tarjan(1);
_rep(i, 1, tot) ccnum[belong[i]]++;
_rep(i, 1, cc) if(ccnum[i] > 1) {
circle = true;
break;
}

if(circle) {
printf("infinity\n");
continue;
}

ans = -1;
spfa();
}
}

有向无环图最长路径

$\textbf{dp + topoSort} \text{可解决有向无环图最长路径问题}$

2017Urmuqi

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
const int maxn = 100000 + 10;
int ans = 0;

// == Graph definition ==
vector<int> G[maxn];

class Edge {
public:
int to, weight;
Edge() {}
Edge(int v, int w) : to(v), weight(w) {}
};

vector<Edge> edges;
int deg[maxn];

void initG() {
Set(deg, 0);
_for(i, 0, maxn) G[i].clear();
edges.clear();
}

void addEdge(int u, int v, int w) {
edges.push_back(Edge(v, w));
G[u].push_back(edges.size() - 1);
deg[v]++;
}

int n, m;
// == Graph finished ==

int D[maxn];
void initdp() {
ans = 0;
Set(D, 0);
}

void dp() {
queue<int> que;
_rep(i, 1, n) if(deg[i] == 0) que.push(i);

while (que.size()) {
int x = que.front();
que.pop();

if(G[x].size() == 0) ans = max(ans, D[x]);

_for(i, 0, G[x].size()) {
const Edge& e = edges[G[x][i]];
int y = e.to;

if(D[y] < D[x] + e.weight) D[y] = D[x] + e.weight;
if(--deg[y] == 0) que.push(y);
}
}
}

int main() {
freopen("input.txt", "r", stdin);
int kase;
scanf("%d", &kase);

while (kase--) {
initG();
scanf("%d%d", &n, &m);
_rep(i, 1, m) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
}


initdp();
dp();
printf("%d\n", ans);
}
}

二分图初步

codeforces

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

const int maxn = 1e5 + 10;
int n, m, k;

// == Graph ==
vector<int> G[maxn];

class Edge {
public:
int to, weight;
Edge() {}
Edge(int v, int w) : to(v), weight(w) {}
};

vector<Edge> edges;

void initG() {
_for(i, 0, maxn) G[i].clear();
edges.clear();
}

void addEdge(int u, int v, int w) {
edges.push_back(Edge(v, w));
G[u].push_back(edges.size() - 1);
}
// == Graph finished ==


// == solve ==
int D[maxn];
int pre[maxn];
int vis[maxn];
int bin[maxn];

void dfs(int u, int pa) {
vis[u] = 1;
_for(i, 0, G[u].size()) {
const Edge& e = edges[G[u][i]];
int v = e.to;

if(vis[v] && v != pa) {
vector<int> path;

int cu = u;
while (cu != v) {
path.push_back(cu);
cu = pre[cu];
}
path.push_back(v);

cout << 2 << "\n";
printf("%d\n", (int)path.size());
for(auto x : path) printf("%d ", x);
exit(0);
}
else if(!vis[v]) {
bin[v] = bin[u] ^ 1;
pre[v] = u;
dfs(v, u);
}
}
}
// == solve finished ==

void init() {
Set(D, 0);
Set(pre, 0);
Set(vis, 0);
Set(bin, 0);
}

int main() {
freopen("input.txt", "r", stdin);
initG();
init();
scanf("%d%d%d", &n, &m, &k);
n = k;

_for(i, 0, m) {
int u, v;
scanf("%d%d", &u, &v);
if(u > n || v > n) continue;
addEdge(u, v, 1);
addEdge(v, u, 1);
}

// solve
_rep(i, 1, n) if(!vis[i]) {
dfs(i, -1);
}

vector<int> ans[2];
_rep(i, 1, n) {
ans[bin[i]].push_back(i);
}

if(ans[0].size() < ans[1].size()) swap(ans[0], ans[1]);
ans[0].resize((k + 1) / 2);

cout << 1 << "\n";
for(auto x : ans[0]) printf("%d ", x);
}