#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; /*#include <atcoder/modint> using namespace atcoder; using mint = modint998244353; vector <mint> fac, inv; mint binom(int n, int k) { if (n < k || n < 0) return 0; return fac[n] * inv[k] * inv[n-k]; } void prep(int N) { fac.resize(N+1, 1); inv.resize(N+1, 1); for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i; inv[N] = fac[N].inv(); for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1); }*/ mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 1000100; const int inf = 1e9; typedef vector<pii> vpi; //Did you REAALLY consider sample tests? #include <algorithm> #include <vector> namespace atcoder { namespace internal { template <class T> struct simple_queue { std::vector<T> payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T& t) { payload.push_back(t); } T& front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace internal } // namespace atcoder #include <cassert> #include <limits> #include <queue> #include <vector> namespace atcoder { template <class Cap> struct mf_graph { public: mf_graph() : _n(0) {} mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap}); g[to].push_back(_edge{from, from_id, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto& _e = g[pos[i].first][pos[i].second]; auto& _re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector<int> level(_n), iter(_n); internal::simple_queue<int> que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int& i = iter[v]; i < int(g[v].size()); i++) { _edge& e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) break; } return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); while (flow < flow_limit) { Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } } return flow; } std::vector<bool> min_cut(int s) { std::vector<bool> visited(_n); internal::simple_queue<int> que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace atcoder using namespace atcoder; bool TEST = 0; void solve(int tc) { int n, m, k; cin >> n >> m >> k; vector<pii> edges(m); auto solve_flow = [&](int l, int r) -> int { int left_part = k; int right_part = r - l + 1; int middle = n - left_part - right_part; int nodes = 2 + left_part + right_part + 2 * middle; vi node_in(n, -1); vi node_out(n, -1); int source = 0; int sink = nodes - 1; auto part = [&](int node) { if (node < k) return 0; //left if (node >= l && node <= r) return 2; //right return 1; //middle }; int curr = 1; rep(i, 0, n) { if (part(i) == 0) { node_in[i] = curr++; node_out[i] = node_in[i]; } } curr = left_part + 1; rep(i, 0, n) { if (part(i) == 1) { node_in[i] = curr++; node_out[i] = node_in[i] + middle; } } curr = left_part + 2 * middle + 1; rep(i, 0, n) { if (part(i) == 2) { node_in[i] = curr++; node_out[i] = node_in[i]; } } mf_graph<int> solver(nodes); //from source to left part rep(i, 0, n) { int tp = part(i); if (tp == 0) solver.add_edge(source, node_in[i], 1); if (tp == 1) solver.add_edge(node_in[i], node_out[i], 1); if (tp == 2) solver.add_edge(node_out[i], sink, 1); } for (auto &[a, b] : edges) { if (part(a) != 2 && part(b) != 0) solver.add_edge(node_out[a], node_in[b], 1); } return solver.flow(source, sink); }; rep(i, 0 ,m) { int a, b; cin >> a >> b; --a; --b; edges[i] = {a, b}; } vi res(k + 1, 0); function<void(int,int,int,int,int)> solve_divconq = [&](int init, int a, int b, int val1, int val2) { if (val1 == val2) { res[val1] += b - a; return; } if (a == b) return; if (b == a + 1) { res[val1]++; return; } int mid = (a + b) / 2; int fmid = solve_flow(init, mid); solve_divconq(init, a, mid, val1, fmid); solve_divconq(init, mid, b, fmid, val2); }; for (int a = k; a < n; ++a) { int f0 = solve_flow(a, a); solve_divconq(a, a, n, f0, k); } FOR(i, 0, k) cout << res[i] << "\n"; } int main() { boost; solve(0); }
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; /*#include <atcoder/modint> using namespace atcoder; using mint = modint998244353; vector <mint> fac, inv; mint binom(int n, int k) { if (n < k || n < 0) return 0; return fac[n] * inv[k] * inv[n-k]; } void prep(int N) { fac.resize(N+1, 1); inv.resize(N+1, 1); for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i; inv[N] = fac[N].inv(); for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1); }*/ mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 1000100; const int inf = 1e9; typedef vector<pii> vpi; //Did you REAALLY consider sample tests? #include <algorithm> #include <vector> namespace atcoder { namespace internal { template <class T> struct simple_queue { std::vector<T> payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T& t) { payload.push_back(t); } T& front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace internal } // namespace atcoder #include <cassert> #include <limits> #include <queue> #include <vector> namespace atcoder { template <class Cap> struct mf_graph { public: mf_graph() : _n(0) {} mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap}); g[to].push_back(_edge{from, from_id, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto& _e = g[pos[i].first][pos[i].second]; auto& _re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector<int> level(_n), iter(_n); internal::simple_queue<int> que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int& i = iter[v]; i < int(g[v].size()); i++) { _edge& e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) break; } return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); while (flow < flow_limit) { Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } } return flow; } std::vector<bool> min_cut(int s) { std::vector<bool> visited(_n); internal::simple_queue<int> que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; } // namespace atcoder using namespace atcoder; bool TEST = 0; void solve(int tc) { int n, m, k; cin >> n >> m >> k; vector<pii> edges(m); auto solve_flow = [&](int l, int r) -> int { int left_part = k; int right_part = r - l + 1; int middle = n - left_part - right_part; int nodes = 2 + left_part + right_part + 2 * middle; vi node_in(n, -1); vi node_out(n, -1); int source = 0; int sink = nodes - 1; auto part = [&](int node) { if (node < k) return 0; //left if (node >= l && node <= r) return 2; //right return 1; //middle }; int curr = 1; rep(i, 0, n) { if (part(i) == 0) { node_in[i] = curr++; node_out[i] = node_in[i]; } } curr = left_part + 1; rep(i, 0, n) { if (part(i) == 1) { node_in[i] = curr++; node_out[i] = node_in[i] + middle; } } curr = left_part + 2 * middle + 1; rep(i, 0, n) { if (part(i) == 2) { node_in[i] = curr++; node_out[i] = node_in[i]; } } mf_graph<int> solver(nodes); //from source to left part rep(i, 0, n) { int tp = part(i); if (tp == 0) solver.add_edge(source, node_in[i], 1); if (tp == 1) solver.add_edge(node_in[i], node_out[i], 1); if (tp == 2) solver.add_edge(node_out[i], sink, 1); } for (auto &[a, b] : edges) { if (part(a) != 2 && part(b) != 0) solver.add_edge(node_out[a], node_in[b], 1); } return solver.flow(source, sink); }; rep(i, 0 ,m) { int a, b; cin >> a >> b; --a; --b; edges[i] = {a, b}; } vi res(k + 1, 0); function<void(int,int,int,int,int)> solve_divconq = [&](int init, int a, int b, int val1, int val2) { if (val1 == val2) { res[val1] += b - a; return; } if (a == b) return; if (b == a + 1) { res[val1]++; return; } int mid = (a + b) / 2; int fmid = solve_flow(init, mid); solve_divconq(init, a, mid, val1, fmid); solve_divconq(init, mid, b, fmid, val2); }; for (int a = k; a < n; ++a) { int f0 = solve_flow(a, a); solve_divconq(a, a, n, f0, k); } FOR(i, 0, k) cout << res[i] << "\n"; } int main() { boost; solve(0); } |