#include <bits/stdc++.h> #include "message.h" #include "sabotaz.h" #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int64 INF = SQ(1000LL * 1000 * 1000); int instance; int instance_count; int n, t; vector<PII> edges; int bridges; vector<int> out[200010]; int pre[200010]; vector<int> st; int dfs(int u, int p){ int q = p; pre[u] = ++t; int low = t; int block = SIZE(st); st.push_back(u); for(int v : out[u]){ if(pre[v]){ if(p == v) p = -1; else low = min(low, pre[v]); }else{ low = min(low, dfs(v, u)); } } if(low == pre[u]){ if(block + 1 < SIZE(st)){ edges.push_back(PII(st[block], st.back())); FWD(i,block,SIZE(st)-1) edges.push_back(PII(st[i], st[i+1])); } st.resize(block); if(q != -1){ edges.push_back(PII(u, q)); ++bridges; } } return low; } void reduce(){ bridges = 0; FWD(i,0,n) pre[i] = 0, out[i].clear(); for(PII e : edges){ out[e.st].push_back(e.nd); out[e.nd].push_back(e.st); } edges.clear(); t = 0; FWD(i,0,n) if(!pre[i]) dfs(i, -1); } void send_edges(int target){ PutInt(target, SIZE(edges)); Send(target); FWD(i,0,SIZE(edges)){ PutInt(target, edges[i].st); PutInt(target, edges[i].nd); if(i+1 == SIZE(edges) || (i+1)%30000 == 0) Send(target); } } void receive_edges(int source){ Receive(source); int k = GetInt(source); FWD(i,0,k){ if(i % 30000 == 0) Receive(source); edges.push_back(PII(GetInt(source), GetInt(source))); } } int main(){ instance = MyNodeId(); instance_count = NumberOfNodes(); n = NumberOfIsles(); int m = NumberOfBridges(); int a = (int)((int64)m * instance / instance_count); int b = (int)((int64)m * (instance+1) / instance_count); FWD(i,a,b) edges.push_back(PII(BridgeEndA(i), BridgeEndB(i))); reduce(); int s = sqrt(instance_count); if(instance < s){ FWD(source,s,instance_count) if(source % s == instance) receive_edges(source); }else{ send_edges(instance % s); return 0; } reduce(); if(instance < 1){ FWD(source,1,s) receive_edges(source); }else{ send_edges(0); return 0; } reduce(); printf("%d\n", bridges); 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 | #include <bits/stdc++.h> #include "message.h" #include "sabotaz.h" #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int64 INF = SQ(1000LL * 1000 * 1000); int instance; int instance_count; int n, t; vector<PII> edges; int bridges; vector<int> out[200010]; int pre[200010]; vector<int> st; int dfs(int u, int p){ int q = p; pre[u] = ++t; int low = t; int block = SIZE(st); st.push_back(u); for(int v : out[u]){ if(pre[v]){ if(p == v) p = -1; else low = min(low, pre[v]); }else{ low = min(low, dfs(v, u)); } } if(low == pre[u]){ if(block + 1 < SIZE(st)){ edges.push_back(PII(st[block], st.back())); FWD(i,block,SIZE(st)-1) edges.push_back(PII(st[i], st[i+1])); } st.resize(block); if(q != -1){ edges.push_back(PII(u, q)); ++bridges; } } return low; } void reduce(){ bridges = 0; FWD(i,0,n) pre[i] = 0, out[i].clear(); for(PII e : edges){ out[e.st].push_back(e.nd); out[e.nd].push_back(e.st); } edges.clear(); t = 0; FWD(i,0,n) if(!pre[i]) dfs(i, -1); } void send_edges(int target){ PutInt(target, SIZE(edges)); Send(target); FWD(i,0,SIZE(edges)){ PutInt(target, edges[i].st); PutInt(target, edges[i].nd); if(i+1 == SIZE(edges) || (i+1)%30000 == 0) Send(target); } } void receive_edges(int source){ Receive(source); int k = GetInt(source); FWD(i,0,k){ if(i % 30000 == 0) Receive(source); edges.push_back(PII(GetInt(source), GetInt(source))); } } int main(){ instance = MyNodeId(); instance_count = NumberOfNodes(); n = NumberOfIsles(); int m = NumberOfBridges(); int a = (int)((int64)m * instance / instance_count); int b = (int)((int64)m * (instance+1) / instance_count); FWD(i,a,b) edges.push_back(PII(BridgeEndA(i), BridgeEndB(i))); reduce(); int s = sqrt(instance_count); if(instance < s){ FWD(source,s,instance_count) if(source % s == instance) receive_edges(source); }else{ send_edges(instance % s); return 0; } reduce(); if(instance < 1){ FWD(source,1,s) receive_edges(source); }else{ send_edges(0); return 0; } reduce(); printf("%d\n", bridges); return 0; } |