-
Notifications
You must be signed in to change notification settings - Fork 0
/
antichain.cpp
151 lines (146 loc) · 3.54 KB
/
antichain.cpp
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
#include "naive.h"
#include "graph.h"
#include "antichain.h"
#include <vector>
#include <stack>
// TODO fix - ei toimi
antichain maxantichain_from_mpc(Graph &g, path_cover &mpc) {
// mpc -> minflow
// topo order . . => linear minflow graph
// reverse all 1way flow topo order
int source = g.n*2+1;
int sink = g.n*2+2;
auto fgo = Flowgraph<Edge::Minflow>(g.n*2+2, source, sink);
auto v_in = [](int v){return v*2-1;};
auto v_out = [](int v){return v*2;};
std::vector<int> visited(g.n+1);
std::vector<int> ree(g.n+1);
std::vector<std::vector<std::pair<int, std::stack<int>>>> flowcount(g.n+1);
// todo stack copy
for(auto &path:mpc) {
flowcount[path[0]].push_back({0, {}});
for(auto it=path.rbegin(); it!=path.rend()-1; it++) {
flowcount[path[0]][flowcount[path[0]].size()-1].second.push(*it);
}
}
for(int i=1; i<=g.n; i++) {
Edge::Minflow *e = fgo.add_edge(source, v_in(i));
e->demand = 0;
e->flow = flowcount[i].size();
}
auto dfs = [&ree, &v_in, &v_out, &fgo, &g, &flowcount, &visited](auto dfs, int s) {
if(visited[s])
return;
visited[s] = 1;
for(auto &u:g.edge_in[s]) {
dfs(dfs, u);
}
auto *e = fgo.add_edge(v_in(s), v_out(s));
e->demand = 1;
e->flow = flowcount[s].size();
assert(e->flow>0);
int pv = 0;
int cnt = 0;
for(auto &u:flowcount[s]) {
if(cnt && pv && pv != u.first) {
auto *e = fgo.add_edge(v_out(pv), v_in(s));
e->demand = 0;
e->flow = cnt;
cnt = 0;
}
cnt++;
pv = u.first;
if(u.second.size() >= 1) {
flowcount[u.second.top()].push_back({s, u.second});
assert(visited[u.second.top()]<2);
flowcount[u.second.top()][flowcount[u.second.top()].size()-1].second.pop();
} else {
ree[s]++;
}
}
if(cnt && pv) {
auto *e = fgo.add_edge(v_out(pv), v_in(s));
e->demand = 0;
e->flow = cnt;
cnt = 0;
}
for(auto &u:g.edge_out[s])
if(flowcount[u].size() == 0)
fgo.add_edge(v_out(s), v_in(u));
visited[s] = 2;
};
for(int i=1; i<=g.n; i++)
dfs(dfs, i);
for(int i=1; i<=g.n; i++) {
auto *e = fgo.add_edge(v_out(i), sink);
e->flow = ree[i];
}
assert(is_valid_minflow(fgo));
return maxantichain_from_minflow(fgo);
}
antichain maxantichain_from_minflow(Flowgraph<Edge::Minflow> &mf) {
std::vector<int> visited(mf.n+1);
auto v_r = [](int v){return (v+1)/2;}; // fg -> original graph
antichain mac;
auto dfs = [&mac, &v_r, &mf, &visited](auto &dfs, int s) {
if(visited[s])
return;
assert(s != mf.sink);
visited[s] = 1;
for(auto &[u,e] : mf.edge_out[s]) {
if(e->flow > e->demand) {
dfs(dfs, u);
}
}
for(auto &[u, e] : mf.edge_in[s]) {
dfs(dfs, u);
}
};
dfs(dfs, mf.source);
auto dfs2 = [&mac, &v_r, &mf, &visited](auto &dfs, int s) {
if(visited[s] != 1)
return;
visited[s] = 2;
for(auto &[u,e] : mf.edge_out[s]) {
if(e->flow > e->demand) {
dfs(dfs, u);
}
if(e->flow == e->demand && e->demand >= 1 && !visited[u]) {
mac.push_back(v_r(s));
visited[u] = 3;
}
}
for(auto &[u, e] : mf.edge_in[s]) {
dfs(dfs, u);
}
};
dfs2(dfs2, mf.source);
return mac;
}
bool is_antichain(antichain &ac, Graph &g) {
std::vector<bool> visited(g.n+1), antichain(g.n+1);
for(auto u:ac)
antichain[u] = 1;
auto dfs = [&g, &visited, &antichain](auto &dfs, int s)->bool {
if(visited[s])
return false;
if(antichain[s]) {
return true;
}
visited[s] = 1;
for(auto u:g.edge_out[s]) {
if(dfs(dfs, u))
return true;
}
return false;
};
for(auto u:ac) {
antichain[u] = 0;
if(dfs(dfs, u))
return false;
antichain[u] = 1;
for(int i=1; i<=g.n; i++)
visited[i] = 0;
}
return true;
}