#include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Edge; struct Node { std::vector<Edge*> edges; bool visited = false; Edge *visited_by_edge = nullptr; }; struct Edge { Node *from = nullptr; Node *to = nullptr; Edge *rev = nullptr; int capacity = 0; int flow = 0; }; std::vector<Node> nodes; std::vector<Edge> edges; int num_vials, num_initial_vials; inline Node *source() { return &nodes[0]; } inline Node *vial_entry(int x) { return &nodes[2*x+1]; } inline Node *vial_exit(int x) { return &nodes[2*x+2]; } Edge *allocate_edge() { edges.emplace_back(); return &edges.back(); } void add_edge(Node *a, Node *b) { Edge *edge_forward = allocate_edge(); Edge *edge_back = allocate_edge(); edge_forward->from = a; edge_forward->to = b; edge_forward->rev = edge_back; edge_forward->capacity = 1; edge_back->from = b; edge_back->to = a; edge_back->rev = edge_forward; edge_back->capacity = 0; a->edges.push_back(edge_forward); b->edges.push_back(edge_back); } void read_input() { int num_connections; std::cin >> num_vials >> num_connections >> num_initial_vials; nodes.resize(2*num_vials+1); edges.reserve(2*(num_initial_vials + num_vials + num_connections)); REP(i, num_initial_vials) add_edge(source(), vial_entry(i)); REP(i, num_vials) add_edge(vial_entry(i), vial_exit(i)); REP(i, num_connections) { int a, b; std::cin >> a >> b; --a; --b; add_edge(vial_exit(a), vial_entry(b)); } } std::vector<Node*> augmenting_stack; int find_augmenting_path(const int minimum_endpoint) { for (Node &node : nodes) { node.visited = false; node.visited_by_edge = nullptr; } augmenting_stack.clear(); { Node *s = source(); s->visited = true; augmenting_stack.push_back(s); } while (!augmenting_stack.empty()) { Node *node = augmenting_stack.back(); augmenting_stack.pop_back(); for (Edge *edge : node->edges) { if (edge->flow >= edge->capacity) continue; Node *dest = edge->to; if (dest->visited) continue; dest->visited = true; dest->visited_by_edge = edge; augmenting_stack.push_back(dest); } } FOR(endpoint, minimum_endpoint, num_vials-1) { if (vial_exit(endpoint)->visited) { return endpoint; } } return -1; } void clear_flow() { for (Node &node : nodes) { for (Edge *edge : node.edges) { edge->flow = 0; } } } void augment_path(const int endpoint) { Node *node = vial_exit(endpoint); for (;;) { Edge *edge = node->visited_by_edge; if (!edge) break; edge->flow += 1; edge->rev->flow -= 1; node = edge->from; } } std::vector<int64_t> num_segments_flow; void calc_segments_flow_from(const int start) { clear_flow(); int flow = 0; int max_endpoint = start; for (;;) { num_segments_flow[flow] += num_vials - max_endpoint; const int endpoint = find_augmenting_path(start); if (endpoint == -1) break; augment_path(endpoint); ++flow; max_endpoint = std::max(max_endpoint, endpoint); } } void solve() { num_segments_flow.assign(num_initial_vials+1, 0); FOR(start, num_initial_vials, num_vials-1) { calc_segments_flow_from(start); } FOR(i, 0, num_initial_vials-1) num_segments_flow[i] -= num_segments_flow[i+1]; } void print() { for (int64_t res : num_segments_flow) { std::cout << res << '\n'; } } int main() { init_io(); read_input(); augmenting_stack.reserve(nodes.size()); solve(); print(); }
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 | #include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Edge; struct Node { std::vector<Edge*> edges; bool visited = false; Edge *visited_by_edge = nullptr; }; struct Edge { Node *from = nullptr; Node *to = nullptr; Edge *rev = nullptr; int capacity = 0; int flow = 0; }; std::vector<Node> nodes; std::vector<Edge> edges; int num_vials, num_initial_vials; inline Node *source() { return &nodes[0]; } inline Node *vial_entry(int x) { return &nodes[2*x+1]; } inline Node *vial_exit(int x) { return &nodes[2*x+2]; } Edge *allocate_edge() { edges.emplace_back(); return &edges.back(); } void add_edge(Node *a, Node *b) { Edge *edge_forward = allocate_edge(); Edge *edge_back = allocate_edge(); edge_forward->from = a; edge_forward->to = b; edge_forward->rev = edge_back; edge_forward->capacity = 1; edge_back->from = b; edge_back->to = a; edge_back->rev = edge_forward; edge_back->capacity = 0; a->edges.push_back(edge_forward); b->edges.push_back(edge_back); } void read_input() { int num_connections; std::cin >> num_vials >> num_connections >> num_initial_vials; nodes.resize(2*num_vials+1); edges.reserve(2*(num_initial_vials + num_vials + num_connections)); REP(i, num_initial_vials) add_edge(source(), vial_entry(i)); REP(i, num_vials) add_edge(vial_entry(i), vial_exit(i)); REP(i, num_connections) { int a, b; std::cin >> a >> b; --a; --b; add_edge(vial_exit(a), vial_entry(b)); } } std::vector<Node*> augmenting_stack; int find_augmenting_path(const int minimum_endpoint) { for (Node &node : nodes) { node.visited = false; node.visited_by_edge = nullptr; } augmenting_stack.clear(); { Node *s = source(); s->visited = true; augmenting_stack.push_back(s); } while (!augmenting_stack.empty()) { Node *node = augmenting_stack.back(); augmenting_stack.pop_back(); for (Edge *edge : node->edges) { if (edge->flow >= edge->capacity) continue; Node *dest = edge->to; if (dest->visited) continue; dest->visited = true; dest->visited_by_edge = edge; augmenting_stack.push_back(dest); } } FOR(endpoint, minimum_endpoint, num_vials-1) { if (vial_exit(endpoint)->visited) { return endpoint; } } return -1; } void clear_flow() { for (Node &node : nodes) { for (Edge *edge : node.edges) { edge->flow = 0; } } } void augment_path(const int endpoint) { Node *node = vial_exit(endpoint); for (;;) { Edge *edge = node->visited_by_edge; if (!edge) break; edge->flow += 1; edge->rev->flow -= 1; node = edge->from; } } std::vector<int64_t> num_segments_flow; void calc_segments_flow_from(const int start) { clear_flow(); int flow = 0; int max_endpoint = start; for (;;) { num_segments_flow[flow] += num_vials - max_endpoint; const int endpoint = find_augmenting_path(start); if (endpoint == -1) break; augment_path(endpoint); ++flow; max_endpoint = std::max(max_endpoint, endpoint); } } void solve() { num_segments_flow.assign(num_initial_vials+1, 0); FOR(start, num_initial_vials, num_vials-1) { calc_segments_flow_from(start); } FOR(i, 0, num_initial_vials-1) num_segments_flow[i] -= num_segments_flow[i+1]; } void print() { for (int64_t res : num_segments_flow) { std::cout << res << '\n'; } } int main() { init_io(); read_input(); augmenting_stack.reserve(nodes.size()); solve(); print(); } |