#include <bits/stdc++.h> #include "skup.h" #include "message.h" #define FOR(i,b,e) for(int i=(b); i <= (e); ++i) #define FORD(i,b,e) for(int i=(b); i >= (e); --i) #define SIZE(c) (int) (c).size() #define FORE(i,c) FOR(i,0,SIZE(c)-1) #define FORDE(i,c) FORD(i,SIZE(c)-1,0) #define pb push_back #define mp make_pair #define st first #define nd second using namespace std; typedef long long ll; typedef pair <int,int> pii; typedef pair <ll,ll> pll; typedef vector <int> VI; typedef vector <bool> VB; typedef vector <pii> VP; typedef vector <ll> VL; typedef vector <pll> VPL; typedef vector <VI> VVI; typedef vector <VL> VVL; typedef vector <VB> VVB; typedef vector <VP> VVP; const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000000000000001LL; /*************************************************************************/ void broadcastVec(int nodes, int myId, const VI &vec) { FOR(i,0,nodes-1) if (i != myId) { PutInt(i, vec.size()); for (int x : vec) { PutInt(i, x); } Send(i); } } void broadcastVecEmpty(int nodes, int myId) { broadcastVec(nodes, myId, {}); } VI receivePacket(int from) { Receive(from); int len = GetInt(from); VI packet; FOR(i,0,len-1) { packet.pb(GetInt(from)); } return packet; } /*************************************************************************/ int main() { ios_base::sync_with_stdio(0); int nodes = NumberOfNodes(); int myId = MyNodeId(); FOR(i,0,nodes-1) if (i != myId) { PutInt(i, 1); Send(i); } VI in(nodes,0); FOR(i,0,nodes-1) if (i != myId) { Receive(i); in[i] = GetInt(i); } VI packet = {myId}; copy(in.begin(), in.end(), back_inserter(packet)); int m = NumberOfCompanies(); FOR(i,0,m-1) { packet.pb(GetShareCost(i)); } VVI gotPacket(nodes); gotPacket[myId] = packet; broadcastVec(nodes, myId, packet); FOR(phase,1,nodes) { bool gotSomething = false; FOR(i,0,nodes-1) if (in[i]) { VI recv = receivePacket(i); gotSomething = true; if (!recv.empty()) { int id = recv[0]; if (gotPacket[id].empty()) { gotPacket[id] = recv; broadcastVec(nodes, myId, recv); } } else { in[i] = false; } } if (!gotSomething) { broadcastVecEmpty(nodes, myId); break; } } FOR(i,0,nodes-1) if (gotPacket[i].empty()) { return 0; } VVI reach(nodes, VI(nodes, 0)); FOR(i,0,nodes-1) { FOR(j,0,nodes-1) { reach[i][j] = gotPacket[j][i+1]; } reach[i][i] = 1; } FOR(j,0,nodes-1) FOR(i,0,nodes-1) FOR(k,0,nodes-1) { if (reach[i][j] && reach[j][k]) { reach[i][k] = 1; } } FOR(i,0,nodes-1) { bool sink = true; FOR(j,0,nodes-1) if (!reach[j][i]) { sink = false; } if (sink && i < myId) { return 0; } } VI data; FOR(i,0,nodes-1) { copy(gotPacket[i].begin() + nodes + 1, gotPacket[i].end(), back_inserter(data)); } sort(data.begin(), data.end(), greater <int> ()); ll ans = 0; FORE(i,data) { ans += (ll) (i + 1) * data[i]; } cout << ans; 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 | #include <bits/stdc++.h> #include "skup.h" #include "message.h" #define FOR(i,b,e) for(int i=(b); i <= (e); ++i) #define FORD(i,b,e) for(int i=(b); i >= (e); --i) #define SIZE(c) (int) (c).size() #define FORE(i,c) FOR(i,0,SIZE(c)-1) #define FORDE(i,c) FORD(i,SIZE(c)-1,0) #define pb push_back #define mp make_pair #define st first #define nd second using namespace std; typedef long long ll; typedef pair <int,int> pii; typedef pair <ll,ll> pll; typedef vector <int> VI; typedef vector <bool> VB; typedef vector <pii> VP; typedef vector <ll> VL; typedef vector <pll> VPL; typedef vector <VI> VVI; typedef vector <VL> VVL; typedef vector <VB> VVB; typedef vector <VP> VVP; const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000000000000001LL; /*************************************************************************/ void broadcastVec(int nodes, int myId, const VI &vec) { FOR(i,0,nodes-1) if (i != myId) { PutInt(i, vec.size()); for (int x : vec) { PutInt(i, x); } Send(i); } } void broadcastVecEmpty(int nodes, int myId) { broadcastVec(nodes, myId, {}); } VI receivePacket(int from) { Receive(from); int len = GetInt(from); VI packet; FOR(i,0,len-1) { packet.pb(GetInt(from)); } return packet; } /*************************************************************************/ int main() { ios_base::sync_with_stdio(0); int nodes = NumberOfNodes(); int myId = MyNodeId(); FOR(i,0,nodes-1) if (i != myId) { PutInt(i, 1); Send(i); } VI in(nodes,0); FOR(i,0,nodes-1) if (i != myId) { Receive(i); in[i] = GetInt(i); } VI packet = {myId}; copy(in.begin(), in.end(), back_inserter(packet)); int m = NumberOfCompanies(); FOR(i,0,m-1) { packet.pb(GetShareCost(i)); } VVI gotPacket(nodes); gotPacket[myId] = packet; broadcastVec(nodes, myId, packet); FOR(phase,1,nodes) { bool gotSomething = false; FOR(i,0,nodes-1) if (in[i]) { VI recv = receivePacket(i); gotSomething = true; if (!recv.empty()) { int id = recv[0]; if (gotPacket[id].empty()) { gotPacket[id] = recv; broadcastVec(nodes, myId, recv); } } else { in[i] = false; } } if (!gotSomething) { broadcastVecEmpty(nodes, myId); break; } } FOR(i,0,nodes-1) if (gotPacket[i].empty()) { return 0; } VVI reach(nodes, VI(nodes, 0)); FOR(i,0,nodes-1) { FOR(j,0,nodes-1) { reach[i][j] = gotPacket[j][i+1]; } reach[i][i] = 1; } FOR(j,0,nodes-1) FOR(i,0,nodes-1) FOR(k,0,nodes-1) { if (reach[i][j] && reach[j][k]) { reach[i][k] = 1; } } FOR(i,0,nodes-1) { bool sink = true; FOR(j,0,nodes-1) if (!reach[j][i]) { sink = false; } if (sink && i < myId) { return 0; } } VI data; FOR(i,0,nodes-1) { copy(gotPacket[i].begin() + nodes + 1, gotPacket[i].end(), back_inserter(data)); } sort(data.begin(), data.end(), greater <int> ()); ll ans = 0; FORE(i,data) { ans += (ll) (i + 1) * data[i]; } cout << ans; return 0; } /*************************************************************************/ |