#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); } // floor log depth <= floor log 200,000 = 17 constexpr int max_log_depth = 17; struct Node { // 500,000 edges: 2 MB total len std::vector<int> out_nodes; // 500,000 nodes ~= 12 MB total used std::set<int> out_trees; // only non-empty for not enabled yet nodes // contains values already enabled std::vector<int> nodes_to_link_when_enabled; // tree linkage Node *child = nullptr; Node *sibling = nullptr; // ancestors[0] = parent // ancestors[i] = up 2^i steps (lazily evaluated for i > 0) // root iff num_ancestors == 0 int num_ancestors = 0; Node *ancestors[max_log_depth + 1] = {}; // only relevant for roots int tree_id = -1; int tree_size = 0; // nodes with edges to this tree // 500,000 nodes ~= 12 MB std::set<int> into_tree; // only non-zero for cycle roots int cycle_len = 0; }; // 200,000 * 320 = 64 MB std::vector<Node> nodes; // 100,000 * 320 = 32 MB std::vector<Node> cycle_nodes; // number of enabled arenas int num_enabled = 0; // for cycles, add cycle_len-2 for each node, (except cycle root) int64_t result = 0; // nodes containing a single out_trees, with both sides enabled std::vector<int> nodes_to_link; void read_input() { int n; std::cin >> n; nodes.resize(n); cycle_nodes.reserve(n / 2); REP(node_id, n) { Node *node = &nodes[node_id]; node->tree_id = node_id; node->tree_size = 1; int num_edges; std::cin >> num_edges; node->out_nodes.reserve(num_edges); bool self_cycle = false; REP(edge_idx, num_edges) { int dest; std::cin >> dest; --dest; node->out_nodes.push_back(dest); if (dest == node_id) self_cycle = true; } if (self_cycle) { // self cycle is equivalent to having no edges node->out_nodes.clear(); } else { for (int x : node->out_nodes) { node->out_trees.insert(x); nodes[x].into_tree.insert(node_id); } } } } void fill_ancestors(Node *node) { if (node->num_ancestors == 0) return; for (;;) { Node *a = node->ancestors[node->num_ancestors - 1]; fill_ancestors(a); if (node->num_ancestors > a->num_ancestors) break; assert(node->num_ancestors <= max_log_depth); node->ancestors[node->num_ancestors] = a->ancestors[node->num_ancestors - 1]; ++node->num_ancestors; } } std::pair<int, Node*> find_depth_and_root(Node *node) { // fills the whole jump path to root fill_ancestors(node); int depth = 0; while (node->num_ancestors != 0) { depth += 1 << (node->num_ancestors - 1); node = node->ancestors[node->num_ancestors - 1]; } return {depth, node}; } Node *go_up(Node *a, int dist) { // a already has filled ancestors FORD(i, max_log_depth, 0) { if (dist >= (1 << i)) { assert(a->num_ancestors > i); a = a->ancestors[i]; fill_ancestors(a); dist -= (1 << i); } } return a; } std::pair<int, Node*> least_common_ancestor(Node *a, int depth_a, Node *b, int depth_b) { // a and b already have filled ancestors // they must be in the same tree if (depth_a > depth_b) { a = go_up(a, depth_a - depth_b); depth_a = depth_b; fill_ancestors(a); } else if (depth_b > depth_a) { b = go_up(b, depth_b - depth_a); depth_b = depth_a; fill_ancestors(b); } if (a==b) return {depth_a, a}; FORD(i, max_log_depth, 0) { if (depth_a >= (1<<i)) { assert(a->num_ancestors > i); assert(b->num_ancestors > i); if (a->ancestors[i] != b->ancestors[i]) { a = a->ancestors[i]; b = b->ancestors[i]; fill_ancestors(a); fill_ancestors(b); depth_a -= 1 << i; depth_b -= 1 << i; } } } assert(a->num_ancestors > 0); assert(b->num_ancestors > 0); assert(a->ancestors[0] == b->ancestors[0]); a = a->ancestors[0]; depth_a -= 1; return {depth_a, a}; } void rename_tree(Node *root, int new_tree_id) { for (const int node_id : root->into_tree) { Node *node = &nodes[node_id]; const auto size_before = node->out_trees.size(); auto it = node->out_trees.find(root->tree_id); assert(it != node->out_trees.end()); node->out_trees.erase(it); node->out_trees.insert(new_tree_id); if (node_id < num_enabled && node->out_trees.size() == 1 && size_before != 1) { // it is pointing at an enabled tree because we wouldn't be renaming to it otherwise nodes_to_link.push_back(node_id); } } root->tree_id = new_tree_id; } void link_to_parent(Node *p, Node *parent) { p->num_ancestors = 1; p->ancestors[0] = parent; p->sibling = parent->child; parent->child = p; } void unlink_from_parent(Node *p, Node *parent) { Node **q = &parent->child; while (*q != p) { assert(*q); q = &(*q)->sibling; } *q = p->sibling; p->sibling = nullptr; p->num_ancestors = 0; } void link_trees(Node *root, Node *parent, int depth, Node *parent_root) { assert(root != parent_root); assert(root->num_ancestors == 0); // if we are linking to a cycle, instead link to an arbitrary point on the cycle if (parent == parent_root && parent_root->cycle_len != 0) { parent = parent_root->child; ++depth; } // merge tree ids if (root->into_tree.size() < parent_root->into_tree.size()) { rename_tree(root, parent_root->tree_id); for (const int node_id : root->into_tree) { parent_root->into_tree.insert(node_id); } root->into_tree.clear(); } else { rename_tree(parent_root, root->tree_id); for (const int node_id : parent_root->into_tree) { root->into_tree.insert(node_id); } parent_root->into_tree = std::move(root->into_tree); } link_to_parent(root, parent); int new_nodes_seen = depth + 1; if (parent_root->cycle_len != 0) { new_nodes_seen += parent_root->cycle_len - 2; } result += int64_t(root->tree_size) * int64_t(new_nodes_seen); parent_root->tree_size += root->tree_size; } // (node, depth) // 1.6 MB std::vector<std::pair<Node*, int>> walk_tree_stack; // return tree size int walk_tree_limit_ancestors(Node *root) { walk_tree_stack.clear(); walk_tree_stack.emplace_back(root, 0); int tree_size = 0; while (!walk_tree_stack.empty()) { auto [node, depth] = walk_tree_stack.back(); walk_tree_stack.pop_back(); ++tree_size; while (node->num_ancestors != 0 && (1 << (node->num_ancestors - 1)) > depth) { --node->num_ancestors; } for (Node *p = node->child; p; p = p->sibling) { walk_tree_stack.emplace_back(p, depth + 1); } } return tree_size; } void create_cycle(Node *cycle_start, Node *root, int depth) { assert(depth >= 1); cycle_nodes.emplace_back(); Node *cycle_root = &cycle_nodes.back(); cycle_root->tree_size = root->tree_size + 1; cycle_root->tree_id = root->tree_id; cycle_root->into_tree = std::move(root->into_tree); cycle_root->cycle_len = depth + 1; Node *p = cycle_start; while (p) { Node *parent = p->num_ancestors == 0 ? nullptr : p->ancestors[0]; if (parent) { unlink_from_parent(p, parent); } int subtree_size = walk_tree_limit_ancestors(p); result += int64_t(subtree_size) * int64_t(cycle_root->cycle_len - 1 - depth); link_to_parent(p, cycle_root); p = parent; --depth; } assert(depth == -1); } void link_root(const int root_id) { assert(root_id < num_enabled); Node *root = &nodes[root_id]; assert(root->num_ancestors == 0); assert(root->out_trees.size() == 1); const int parent_tree_id = *root->out_trees.begin(); auto out_nodes_iter = root->out_nodes.begin(); assert(out_nodes_iter != root->out_nodes.end()); Node *parent = &nodes[*out_nodes_iter++]; auto [depth, parent_root] = find_depth_and_root(parent); assert(parent_root->tree_id == parent_tree_id); while (out_nodes_iter != root->out_nodes.end()) { Node *parent2 = &nodes[*out_nodes_iter++]; const auto [depth2, parent_root2] = find_depth_and_root(parent2); assert(parent_root == parent_root2); const auto [depth_lca, lca] = least_common_ancestor(parent, depth, parent2, depth2); parent = lca; depth = depth_lca; } root->out_nodes.clear(); root->out_trees.clear(); auto into_tree_iter = parent_root->into_tree.find(root_id); assert(into_tree_iter != parent_root->into_tree.end()); parent_root->into_tree.erase(into_tree_iter); if (parent == root) { // nothing, cycle of length 1 } else if (parent_root == root) { create_cycle(parent, root, depth); } else { link_trees(root, parent, depth, parent_root); } } void debug_tree(Node *node, int indent); void debug_children(Node *node, int indent) { for (Node *child = node->child; child; child = child->sibling) { debug_tree(child, indent); } } void debug_tree(Node *node, int indent) { REP(i, indent) std::cerr << ' '; std::cerr << (node - &nodes[0] + 1) << '\n'; debug_children(node, indent+1); } void debug_trees() { std::cerr << "--- trees begin, num_enabled = " << num_enabled << "\n"; for (Node &cycle_node : cycle_nodes) { std::cerr << "C\n"; debug_children(&cycle_node, 1); } for (Node &node : nodes) { if (node.num_ancestors == 0) { debug_tree(&node, 0); } } std::cerr << "--- trees end\n"; } void enable_one() { ++num_enabled; { Node *node = &nodes[num_enabled - 1]; nodes_to_link.insert( nodes_to_link.end(), node->nodes_to_link_when_enabled.begin(), node->nodes_to_link_when_enabled.end()); node->nodes_to_link_when_enabled.clear(); if (node->out_trees.size() == 1) { int node_id2 = *node->out_trees.begin(); if (node_id2 < num_enabled) { nodes_to_link.push_back(num_enabled - 1); } else { nodes[node_id2].nodes_to_link_when_enabled.push_back(num_enabled - 1); } } } while (!nodes_to_link.empty()) { const int node_id = nodes_to_link.back(); nodes_to_link.pop_back(); assert(node_id < num_enabled); link_root(node_id); } } int main() { init_io(); read_input(); walk_tree_stack.reserve(nodes.size()); while (num_enabled < int(nodes.size())) { enable_one(); if (num_enabled > 1) std::cout << ' '; std::cout << result; } std::cout << '\n'; }
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | #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); } // floor log depth <= floor log 200,000 = 17 constexpr int max_log_depth = 17; struct Node { // 500,000 edges: 2 MB total len std::vector<int> out_nodes; // 500,000 nodes ~= 12 MB total used std::set<int> out_trees; // only non-empty for not enabled yet nodes // contains values already enabled std::vector<int> nodes_to_link_when_enabled; // tree linkage Node *child = nullptr; Node *sibling = nullptr; // ancestors[0] = parent // ancestors[i] = up 2^i steps (lazily evaluated for i > 0) // root iff num_ancestors == 0 int num_ancestors = 0; Node *ancestors[max_log_depth + 1] = {}; // only relevant for roots int tree_id = -1; int tree_size = 0; // nodes with edges to this tree // 500,000 nodes ~= 12 MB std::set<int> into_tree; // only non-zero for cycle roots int cycle_len = 0; }; // 200,000 * 320 = 64 MB std::vector<Node> nodes; // 100,000 * 320 = 32 MB std::vector<Node> cycle_nodes; // number of enabled arenas int num_enabled = 0; // for cycles, add cycle_len-2 for each node, (except cycle root) int64_t result = 0; // nodes containing a single out_trees, with both sides enabled std::vector<int> nodes_to_link; void read_input() { int n; std::cin >> n; nodes.resize(n); cycle_nodes.reserve(n / 2); REP(node_id, n) { Node *node = &nodes[node_id]; node->tree_id = node_id; node->tree_size = 1; int num_edges; std::cin >> num_edges; node->out_nodes.reserve(num_edges); bool self_cycle = false; REP(edge_idx, num_edges) { int dest; std::cin >> dest; --dest; node->out_nodes.push_back(dest); if (dest == node_id) self_cycle = true; } if (self_cycle) { // self cycle is equivalent to having no edges node->out_nodes.clear(); } else { for (int x : node->out_nodes) { node->out_trees.insert(x); nodes[x].into_tree.insert(node_id); } } } } void fill_ancestors(Node *node) { if (node->num_ancestors == 0) return; for (;;) { Node *a = node->ancestors[node->num_ancestors - 1]; fill_ancestors(a); if (node->num_ancestors > a->num_ancestors) break; assert(node->num_ancestors <= max_log_depth); node->ancestors[node->num_ancestors] = a->ancestors[node->num_ancestors - 1]; ++node->num_ancestors; } } std::pair<int, Node*> find_depth_and_root(Node *node) { // fills the whole jump path to root fill_ancestors(node); int depth = 0; while (node->num_ancestors != 0) { depth += 1 << (node->num_ancestors - 1); node = node->ancestors[node->num_ancestors - 1]; } return {depth, node}; } Node *go_up(Node *a, int dist) { // a already has filled ancestors FORD(i, max_log_depth, 0) { if (dist >= (1 << i)) { assert(a->num_ancestors > i); a = a->ancestors[i]; fill_ancestors(a); dist -= (1 << i); } } return a; } std::pair<int, Node*> least_common_ancestor(Node *a, int depth_a, Node *b, int depth_b) { // a and b already have filled ancestors // they must be in the same tree if (depth_a > depth_b) { a = go_up(a, depth_a - depth_b); depth_a = depth_b; fill_ancestors(a); } else if (depth_b > depth_a) { b = go_up(b, depth_b - depth_a); depth_b = depth_a; fill_ancestors(b); } if (a==b) return {depth_a, a}; FORD(i, max_log_depth, 0) { if (depth_a >= (1<<i)) { assert(a->num_ancestors > i); assert(b->num_ancestors > i); if (a->ancestors[i] != b->ancestors[i]) { a = a->ancestors[i]; b = b->ancestors[i]; fill_ancestors(a); fill_ancestors(b); depth_a -= 1 << i; depth_b -= 1 << i; } } } assert(a->num_ancestors > 0); assert(b->num_ancestors > 0); assert(a->ancestors[0] == b->ancestors[0]); a = a->ancestors[0]; depth_a -= 1; return {depth_a, a}; } void rename_tree(Node *root, int new_tree_id) { for (const int node_id : root->into_tree) { Node *node = &nodes[node_id]; const auto size_before = node->out_trees.size(); auto it = node->out_trees.find(root->tree_id); assert(it != node->out_trees.end()); node->out_trees.erase(it); node->out_trees.insert(new_tree_id); if (node_id < num_enabled && node->out_trees.size() == 1 && size_before != 1) { // it is pointing at an enabled tree because we wouldn't be renaming to it otherwise nodes_to_link.push_back(node_id); } } root->tree_id = new_tree_id; } void link_to_parent(Node *p, Node *parent) { p->num_ancestors = 1; p->ancestors[0] = parent; p->sibling = parent->child; parent->child = p; } void unlink_from_parent(Node *p, Node *parent) { Node **q = &parent->child; while (*q != p) { assert(*q); q = &(*q)->sibling; } *q = p->sibling; p->sibling = nullptr; p->num_ancestors = 0; } void link_trees(Node *root, Node *parent, int depth, Node *parent_root) { assert(root != parent_root); assert(root->num_ancestors == 0); // if we are linking to a cycle, instead link to an arbitrary point on the cycle if (parent == parent_root && parent_root->cycle_len != 0) { parent = parent_root->child; ++depth; } // merge tree ids if (root->into_tree.size() < parent_root->into_tree.size()) { rename_tree(root, parent_root->tree_id); for (const int node_id : root->into_tree) { parent_root->into_tree.insert(node_id); } root->into_tree.clear(); } else { rename_tree(parent_root, root->tree_id); for (const int node_id : parent_root->into_tree) { root->into_tree.insert(node_id); } parent_root->into_tree = std::move(root->into_tree); } link_to_parent(root, parent); int new_nodes_seen = depth + 1; if (parent_root->cycle_len != 0) { new_nodes_seen += parent_root->cycle_len - 2; } result += int64_t(root->tree_size) * int64_t(new_nodes_seen); parent_root->tree_size += root->tree_size; } // (node, depth) // 1.6 MB std::vector<std::pair<Node*, int>> walk_tree_stack; // return tree size int walk_tree_limit_ancestors(Node *root) { walk_tree_stack.clear(); walk_tree_stack.emplace_back(root, 0); int tree_size = 0; while (!walk_tree_stack.empty()) { auto [node, depth] = walk_tree_stack.back(); walk_tree_stack.pop_back(); ++tree_size; while (node->num_ancestors != 0 && (1 << (node->num_ancestors - 1)) > depth) { --node->num_ancestors; } for (Node *p = node->child; p; p = p->sibling) { walk_tree_stack.emplace_back(p, depth + 1); } } return tree_size; } void create_cycle(Node *cycle_start, Node *root, int depth) { assert(depth >= 1); cycle_nodes.emplace_back(); Node *cycle_root = &cycle_nodes.back(); cycle_root->tree_size = root->tree_size + 1; cycle_root->tree_id = root->tree_id; cycle_root->into_tree = std::move(root->into_tree); cycle_root->cycle_len = depth + 1; Node *p = cycle_start; while (p) { Node *parent = p->num_ancestors == 0 ? nullptr : p->ancestors[0]; if (parent) { unlink_from_parent(p, parent); } int subtree_size = walk_tree_limit_ancestors(p); result += int64_t(subtree_size) * int64_t(cycle_root->cycle_len - 1 - depth); link_to_parent(p, cycle_root); p = parent; --depth; } assert(depth == -1); } void link_root(const int root_id) { assert(root_id < num_enabled); Node *root = &nodes[root_id]; assert(root->num_ancestors == 0); assert(root->out_trees.size() == 1); const int parent_tree_id = *root->out_trees.begin(); auto out_nodes_iter = root->out_nodes.begin(); assert(out_nodes_iter != root->out_nodes.end()); Node *parent = &nodes[*out_nodes_iter++]; auto [depth, parent_root] = find_depth_and_root(parent); assert(parent_root->tree_id == parent_tree_id); while (out_nodes_iter != root->out_nodes.end()) { Node *parent2 = &nodes[*out_nodes_iter++]; const auto [depth2, parent_root2] = find_depth_and_root(parent2); assert(parent_root == parent_root2); const auto [depth_lca, lca] = least_common_ancestor(parent, depth, parent2, depth2); parent = lca; depth = depth_lca; } root->out_nodes.clear(); root->out_trees.clear(); auto into_tree_iter = parent_root->into_tree.find(root_id); assert(into_tree_iter != parent_root->into_tree.end()); parent_root->into_tree.erase(into_tree_iter); if (parent == root) { // nothing, cycle of length 1 } else if (parent_root == root) { create_cycle(parent, root, depth); } else { link_trees(root, parent, depth, parent_root); } } void debug_tree(Node *node, int indent); void debug_children(Node *node, int indent) { for (Node *child = node->child; child; child = child->sibling) { debug_tree(child, indent); } } void debug_tree(Node *node, int indent) { REP(i, indent) std::cerr << ' '; std::cerr << (node - &nodes[0] + 1) << '\n'; debug_children(node, indent+1); } void debug_trees() { std::cerr << "--- trees begin, num_enabled = " << num_enabled << "\n"; for (Node &cycle_node : cycle_nodes) { std::cerr << "C\n"; debug_children(&cycle_node, 1); } for (Node &node : nodes) { if (node.num_ancestors == 0) { debug_tree(&node, 0); } } std::cerr << "--- trees end\n"; } void enable_one() { ++num_enabled; { Node *node = &nodes[num_enabled - 1]; nodes_to_link.insert( nodes_to_link.end(), node->nodes_to_link_when_enabled.begin(), node->nodes_to_link_when_enabled.end()); node->nodes_to_link_when_enabled.clear(); if (node->out_trees.size() == 1) { int node_id2 = *node->out_trees.begin(); if (node_id2 < num_enabled) { nodes_to_link.push_back(num_enabled - 1); } else { nodes[node_id2].nodes_to_link_when_enabled.push_back(num_enabled - 1); } } } while (!nodes_to_link.empty()) { const int node_id = nodes_to_link.back(); nodes_to_link.pop_back(); assert(node_id < num_enabled); link_root(node_id); } } int main() { init_io(); read_input(); walk_tree_stack.reserve(nodes.size()); while (num_enabled < int(nodes.size())) { enable_one(); if (num_enabled > 1) std::cout << ' '; std::cout << result; } std::cout << '\n'; } |