#include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second #include "message.h" #include "sabotaz.h" int ME, NODES, N, M; int _treeroot[300000]; int _treesize[300000]; int _component[300000]; int _parent[300000]; int treeroot(int v) { if (_treeroot[v] != v) _treeroot[v] = treeroot(_treeroot[v]); return _treeroot[v]; } int component(int v) { if (_component[v] != v) _component[v] = component(_component[v]); return _component[v]; } int parent(int v) { return _parent[v] == -1 ? -1 : component(_parent[v]); } void reroot(int v, int old_root) { _treeroot[v] = _treeroot[old_root] = v; _treesize[v] = _treesize[old_root]; int new_parent = -1; while (v != -1) { int p = parent(v); _parent[v] = new_parent; new_parent = v; v = p; } } int T = 0; int last_visit[300000]; void merge(int a, int b) { ++T; int a2 = a, b2 = b, p; while (true) { if (a2 != -1) { if (last_visit[a2] == T) { p = a2; break;} last_visit[a2] = T; a2 = parent(a2); } if (b2 != -1) { if (last_visit[b2] == T) { p = b2; break;} last_visit[b2] = T; b2 = parent(b2); } } while (a != p) { int tmp = parent(a); _component[a] = p; a = tmp; } while (b != p) { int tmp = parent(b); _component[b] = p; b = tmp; } } bool is_interesting[300000]; vector<int> interesting_nodes; void mark_as_interesting(int v) { if (is_interesting[v]) return; is_interesting[v] = true; interesting_nodes.pb(v); } void add_edge(int a, int b) { mark_as_interesting(a); mark_as_interesting(b); a = component(a), b = component(b); if (a == b) return; int r1 = treeroot(a), r2 = treeroot(b); if (r1 != r2) { if (_treesize[r1] > _treesize[r2]) { swap(a, b); swap(r1, r2); } reroot(a, r1); _parent[a] = b; _treeroot[a] = r2; _treesize[r2] += _treesize[r1]; } else { merge(a, b); } } list<int> child_nodes[1000]; int parent_node[1000]; vector<PII> double_edges; vector<PII> single_edges; void do_send(int p) { FOREACH(it, interesting_nodes) { int c = component(*it); if (c != *it) double_edges.pb(mp(*it, c)); else { int p = parent(*it); if (p != *it && p != -1) { single_edges.pb(mp(*it, p)); } } } PutInt(p, single_edges.size()); FOREACH(it, single_edges) { PutInt(p, it->first); PutInt(p, it->second); } PutInt(p, double_edges.size()); FOREACH(it, double_edges) { PutInt(p, it->first); PutInt(p, it->second); } Send(p); } void do_receive(int c) { Receive(c); FOR(m,1,3) { int D = GetInt(c); REP(i, D) { int a = GetInt(c), b = GetInt(c); REP(j, m) add_edge(a, b); } } } int main() { ME = MyNodeId(); NODES = NumberOfNodes(); FOR(i, 1, NODES) { int k = 1; while ((k&i) == 0) k <<= 1; child_nodes[i - k].pb(i); parent_node[i] = i - k; } N = NumberOfIsles(); M = NumberOfBridges(); REP(i,N) { _treeroot[i] = _component[i] = i; _parent[i] = -1; _treesize[i] = 1; } int M0 = M / NODES * ME; int M1 = (ME == NODES - 1) ? M : M / NODES * (ME + 1); FOR(i,M0,M1) { int a = BridgeEndA(i); int b = BridgeEndB(i); add_edge(a, b); } FOREACH (it, child_nodes[ME]) { do_receive(*it); } if (ME != 0) { do_send(parent_node[ME]); return 0; } int result = 0; REP(i, N) if (component(i) == i && treeroot(i) != i) ++result; printf("%d\n", result); }
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 | #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second #include "message.h" #include "sabotaz.h" int ME, NODES, N, M; int _treeroot[300000]; int _treesize[300000]; int _component[300000]; int _parent[300000]; int treeroot(int v) { if (_treeroot[v] != v) _treeroot[v] = treeroot(_treeroot[v]); return _treeroot[v]; } int component(int v) { if (_component[v] != v) _component[v] = component(_component[v]); return _component[v]; } int parent(int v) { return _parent[v] == -1 ? -1 : component(_parent[v]); } void reroot(int v, int old_root) { _treeroot[v] = _treeroot[old_root] = v; _treesize[v] = _treesize[old_root]; int new_parent = -1; while (v != -1) { int p = parent(v); _parent[v] = new_parent; new_parent = v; v = p; } } int T = 0; int last_visit[300000]; void merge(int a, int b) { ++T; int a2 = a, b2 = b, p; while (true) { if (a2 != -1) { if (last_visit[a2] == T) { p = a2; break;} last_visit[a2] = T; a2 = parent(a2); } if (b2 != -1) { if (last_visit[b2] == T) { p = b2; break;} last_visit[b2] = T; b2 = parent(b2); } } while (a != p) { int tmp = parent(a); _component[a] = p; a = tmp; } while (b != p) { int tmp = parent(b); _component[b] = p; b = tmp; } } bool is_interesting[300000]; vector<int> interesting_nodes; void mark_as_interesting(int v) { if (is_interesting[v]) return; is_interesting[v] = true; interesting_nodes.pb(v); } void add_edge(int a, int b) { mark_as_interesting(a); mark_as_interesting(b); a = component(a), b = component(b); if (a == b) return; int r1 = treeroot(a), r2 = treeroot(b); if (r1 != r2) { if (_treesize[r1] > _treesize[r2]) { swap(a, b); swap(r1, r2); } reroot(a, r1); _parent[a] = b; _treeroot[a] = r2; _treesize[r2] += _treesize[r1]; } else { merge(a, b); } } list<int> child_nodes[1000]; int parent_node[1000]; vector<PII> double_edges; vector<PII> single_edges; void do_send(int p) { FOREACH(it, interesting_nodes) { int c = component(*it); if (c != *it) double_edges.pb(mp(*it, c)); else { int p = parent(*it); if (p != *it && p != -1) { single_edges.pb(mp(*it, p)); } } } PutInt(p, single_edges.size()); FOREACH(it, single_edges) { PutInt(p, it->first); PutInt(p, it->second); } PutInt(p, double_edges.size()); FOREACH(it, double_edges) { PutInt(p, it->first); PutInt(p, it->second); } Send(p); } void do_receive(int c) { Receive(c); FOR(m,1,3) { int D = GetInt(c); REP(i, D) { int a = GetInt(c), b = GetInt(c); REP(j, m) add_edge(a, b); } } } int main() { ME = MyNodeId(); NODES = NumberOfNodes(); FOR(i, 1, NODES) { int k = 1; while ((k&i) == 0) k <<= 1; child_nodes[i - k].pb(i); parent_node[i] = i - k; } N = NumberOfIsles(); M = NumberOfBridges(); REP(i,N) { _treeroot[i] = _component[i] = i; _parent[i] = -1; _treesize[i] = 1; } int M0 = M / NODES * ME; int M1 = (ME == NODES - 1) ? M : M / NODES * (ME + 1); FOR(i,M0,M1) { int a = BridgeEndA(i); int b = BridgeEndB(i); add_edge(a, b); } FOREACH (it, child_nodes[ME]) { do_receive(*it); } if (ME != 0) { do_send(parent_node[ME]); return 0; } int result = 0; REP(i, N) if (component(i) == i && treeroot(i) != i) ++result; printf("%d\n", result); } |