#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #include "sabotaz.h" #include "message.h" #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf("%d: ", id); dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; const int NMAX = 1000*1000+9; int id, nd, nn; vvi scanEdgesLists() { vvi out(nn); for(int i = id; i < NumberOfBridges(); i += nd) { int x = BridgeEndA(i), y = BridgeEndB(i); out[x].pb(y); out[y].pb(x); } return out; } vvi receiveEdgesLists(int sender, vvi &out) { prd("receives msg from %d: \n", sender); Receive(sender); int msgSize = GetInt(sender); REP(i, msgSize) { int x = GetInt(sender), y = GetInt(sender); out[x].pb(y); out[y].pb(x); } return out; } void sendMsg(int node, vi & msg) { prd("sending msg to %d: \n", node); PutInt(node, msg.size() / 2); FORIT(it, msg, vi) { PutInt(node, *it); dbg printf("%d ", *it); } dbg printf("\n"); Send(node); } class Forest { public: vvpi tree; vi low, level, msg; vvi blocks; void dfs(int v, int forb, vvi & edges) { prd("dfs %d level = %d\n", v, level[v]); FORIT(it, edges[v], vi) { int w = *it; if(w == forb) { forb = -1; continue; } if(level[w]) { prd("dfs %d see %d\n", v, level[w]); low[v] = min(low[v], level[w]); } else { low[w] = level[w] = level[v] + 1; dfs(w, v, edges); low[v] = min(low[v], low[w]); tree[v].pb(mp(w, (low[w] == level[w]))); } } } void walkTree(int v, int head) { prd("walkTree %d head = %d\n", v, head); blocks[head].pb(v); FORIT(it, tree[v], vpi) { if(it->yy) { msg.pb(v); msg.pb(it->xx); walkTree(it->xx, it->xx); } else walkTree(it->xx, head); } } Forest(vvi & edges) { prd("Creating Forest\n"); tree = vvpi(nn); low = vi(nn); level = vi(nn); blocks = vvi(nn); REP(i, nn) if(!level[i]) { low[i] = level[i] = 1; dfs(i, -1, edges); } REP(i, nn) if(level[i] == 1) walkTree(i, i); REP(i, nn) { dbg { prd("block: "); FORIT(it, blocks[i], vi) printf("%d ", *it); printf("\n"); } if(blocks[i].size() > 1) { msg.pb(blocks[i][0]); msg.pb(blocks[i].back()); REP(j, blocks[i].size() - 1) { msg.pb(blocks[i][j]); msg.pb(blocks[i][j+1]); } } } } vvi retrieveEdges() { vvi out(nn); for(int i = 0; i < msg.size(); i += 2) { int x = msg[i], y = msg[i+1]; out[x].pb(y); out[y].pb(x); } return out; } int getNumberOfBridges() { int ans = 0; REP(i, nn) FORIT(it, tree[i], vpi) ans += it->yy; return ans; } }; int main() { id = MyNodeId(); nn = NumberOfIsles(); nd = NumberOfNodes(); vvi edges = scanEdgesLists(); Forest forest(edges); int turn = 1; while(1) { if(id % (2*turn)) { sendMsg(id - turn, forest.msg); break; } else { int next = id + turn; turn *= 2; if(next >= nd) { if(id == 0) break; else continue; } edges = forest.retrieveEdges(); receiveEdgesLists(next, edges); forest = Forest(edges); } } if(id == 0) printf("%d", forest.getNumberOfBridges()); return 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 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #include "sabotaz.h" #include "message.h" #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf("%d: ", id); dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; const int NMAX = 1000*1000+9; int id, nd, nn; vvi scanEdgesLists() { vvi out(nn); for(int i = id; i < NumberOfBridges(); i += nd) { int x = BridgeEndA(i), y = BridgeEndB(i); out[x].pb(y); out[y].pb(x); } return out; } vvi receiveEdgesLists(int sender, vvi &out) { prd("receives msg from %d: \n", sender); Receive(sender); int msgSize = GetInt(sender); REP(i, msgSize) { int x = GetInt(sender), y = GetInt(sender); out[x].pb(y); out[y].pb(x); } return out; } void sendMsg(int node, vi & msg) { prd("sending msg to %d: \n", node); PutInt(node, msg.size() / 2); FORIT(it, msg, vi) { PutInt(node, *it); dbg printf("%d ", *it); } dbg printf("\n"); Send(node); } class Forest { public: vvpi tree; vi low, level, msg; vvi blocks; void dfs(int v, int forb, vvi & edges) { prd("dfs %d level = %d\n", v, level[v]); FORIT(it, edges[v], vi) { int w = *it; if(w == forb) { forb = -1; continue; } if(level[w]) { prd("dfs %d see %d\n", v, level[w]); low[v] = min(low[v], level[w]); } else { low[w] = level[w] = level[v] + 1; dfs(w, v, edges); low[v] = min(low[v], low[w]); tree[v].pb(mp(w, (low[w] == level[w]))); } } } void walkTree(int v, int head) { prd("walkTree %d head = %d\n", v, head); blocks[head].pb(v); FORIT(it, tree[v], vpi) { if(it->yy) { msg.pb(v); msg.pb(it->xx); walkTree(it->xx, it->xx); } else walkTree(it->xx, head); } } Forest(vvi & edges) { prd("Creating Forest\n"); tree = vvpi(nn); low = vi(nn); level = vi(nn); blocks = vvi(nn); REP(i, nn) if(!level[i]) { low[i] = level[i] = 1; dfs(i, -1, edges); } REP(i, nn) if(level[i] == 1) walkTree(i, i); REP(i, nn) { dbg { prd("block: "); FORIT(it, blocks[i], vi) printf("%d ", *it); printf("\n"); } if(blocks[i].size() > 1) { msg.pb(blocks[i][0]); msg.pb(blocks[i].back()); REP(j, blocks[i].size() - 1) { msg.pb(blocks[i][j]); msg.pb(blocks[i][j+1]); } } } } vvi retrieveEdges() { vvi out(nn); for(int i = 0; i < msg.size(); i += 2) { int x = msg[i], y = msg[i+1]; out[x].pb(y); out[y].pb(x); } return out; } int getNumberOfBridges() { int ans = 0; REP(i, nn) FORIT(it, tree[i], vpi) ans += it->yy; return ans; } }; int main() { id = MyNodeId(); nn = NumberOfIsles(); nd = NumberOfNodes(); vvi edges = scanEdgesLists(); Forest forest(edges); int turn = 1; while(1) { if(id % (2*turn)) { sendMsg(id - turn, forest.msg); break; } else { int next = id + turn; turn *= 2; if(next >= nd) { if(id == 0) break; else continue; } edges = forest.retrieveEdges(); receiveEdgesLists(next, edges); forest = Forest(edges); } } if(id == 0) printf("%d", forest.getNumberOfBridges()); return 0; } |