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
394
395
396
397
398
399
400
401
402
#include <bits/stdc++.h>
using namespace std;

#include "message.h"
#include "skup.h"

// #define DBGMSG
#ifdef DBGMSG
  #define debugmsg(...) fprintf(stderr, __VA_ARGS__)
#else
  #define debugmsg(...)
#endif

// #define DBG
#ifdef DBG
  #define debug(...) fprintf(stderr, __VA_ARGS__)
#else
  #define debug(...)
#endif

#define massert(...) assert(__VA_ARGS__)

using ull = unsigned long long;

enum MESSAGE_TYPE {
  NOPE,
  PING,
  PONG,
  GRAPH,
  CYCLE,
  FINAL_CYCLE,
  DIE,
};

vector<int> shares[100], receivedShares[100];
vector<int> graph, lastSentGraph;

unordered_set<int> canSendTo;
unordered_set<int> canReceiveFrom;
bool receivedNope[100];
int parent;
int cycleLen;
bool finalCycleSent;
vector<int> lastCycle;

int id;
int totalNodes;

void sendPing(int s) {
  debugmsg("send ping to %d\n", s);
  PutInt(s, PING);
  Send(s);
}

void sendPong(int s) {
  debugmsg("send pong to %d\n", s);
  PutInt(s, PONG);
  Send(s);
}

void sendGraph(int s) {
  static vector<int> diff;
  const int INT_THRESHOLD = 15000;
  if (graph == lastSentGraph) {
    return;
  }
  diff.clear();
  debugmsg("send graph to %d\n", s);
  PutInt(s, GRAPH);
  for (int i = 0; i < totalNodes; i++) {
    if (graph[i] != lastSentGraph[i]) {
      massert(graph[i] != -1);
      massert(!shares[i].empty());
      diff.push_back(i);
    }
  }
  lastSentGraph = graph;
  PutInt(s, diff.size());
  int intsSent = totalNodes + 2;
  for (int i : diff) {
    PutInt(s, i);
    PutInt(s, graph[i]);
    PutInt(s, shares[i].size());
    massert(!shares[i].empty());
    intsSent += 3;
    for (auto j : shares[i]) {
      if (intsSent >= INT_THRESHOLD) {
        PutInt(s, -1);
        Send(s);
        intsSent = 0;
      }
      PutInt(s, j);
      intsSent++;
    }
  }
  Send(s);
}

bool receiveGraph(int s) {
  debugmsg("received graph from %d\n", s);
  bool changed = false;
  vector<int> g;
  // for (int i = 0; i < totalNodes; i++) {
  //   int p = GetInt(s);
  //   g.push_back(p);
  //   if (p != -1) {
  //     if (graph[i] != p) {
  //       changed = true;
  //     }
  //     graph[i] = p;
  //   }
  // }
  int diffLen = GetInt(s);
  vector<pair<int,vector<int>>> diff;
  while (diffLen--) {
    int i = GetInt(s);
    if (i == -1) {
      Receive(s);
      i = GetInt(s);
    }
    int gr = GetInt(s);
    if (gr == -1) {
      Receive(s);
      gr = GetInt(s);
    }
    graph[i] = gr;
    diff.push_back({i, {}});
    massert(0 <= i && i < totalNodes);
    receivedShares[i].clear();
    int len = GetInt(s);
    if (len == -1) {
      Receive(s);
      len = GetInt(s);
    }
    massert(len > 0);
    while (len--) {
      int v = GetInt(s);
      if (v == -1) {
        Receive(s);
        v = GetInt(s);
      }
      diff.back().second.push_back(v);
      receivedShares[i].push_back(v);
    }
    if (shares[i].empty()) {
      shares[i].swap(receivedShares[i]);
    }
  }
  // for (int i = 0; i < totalNodes; i++) {
  //   debugmsg("%d:", i);
  //   for (int j : shares[i]) {
  //     debugmsg(" %d", j);
  //   }
  //   debugmsg("\n");
  // }
  return changed;
}

void sendDie(int s) {
  PutInt(s, DIE);
  Send(s);
}

bool fullReach() {
  return find(graph.begin(), graph.end(), -1) == graph.end();
}

int minOnCycle() {
  int minc = id;
  for (int i = graph[id]; i != id; i = graph[i]) {
    minc = min(minc, i);
  }
  return minc;
}

void sendFinalCycle(int s, int minc) {
  PutInt(s, FINAL_CYCLE);
  PutInt(s, minc);
  Send(s);
}

int receiveFinalCycle(int s) {
  return GetInt(s);
}

void sendCycle(int s, int v, int cand, int q) {
  PutInt(s, CYCLE);
  PutInt(s, v);
  PutInt(s, cand);
  PutInt(s, q);
  Send(s);
}

pair<pair<int,int>,int> receiveCycle(int s) {
  int v = GetInt(s);
  int cand = GetInt(s);
  int q = GetInt(s);
  return {{v, cand}, q};
}

bool done() {
  for (int i = 0; i < totalNodes; i++) {
    if (shares[i].empty()) {
      massert(graph[i] == -1);
      return false;
    }
    // massert(graph[i] != -1);
  }
  vector<int> all;
  for (int i = 0; i < totalNodes; i++) {
    all.insert(all.end(), shares[i].begin(), shares[i].end());
  }
  sort(all.begin(), all.end(), greater<int>());
  ull res = 0;
  for (int i = 0; i < (int)all.size(); i++) {
    res += (ull)all[i] * (i+1);
  }
  printf("%llu\n", res);
  for (int i = 0; i < totalNodes; i++) {
    if (i != id) {
      sendDie(i);
      sendDie(i);
    }
  }
  return true;
}

vector<int> getCycle() {
  vector<int> tmpCycle;
  // tmpCycle.push_back(id);
  int s, len;
  for (len = 0, s = graph[id]; s != -1 && s != id && len < totalNodes; len++) {
    tmpCycle.push_back(s);
    s = graph[s];
  }
  if (s != id) {
    return {};
  }
  return tmpCycle;
}

void checkCycle() {
  auto cycle = getCycle();
  if (!cycle.empty()) {
    debug("cycle %d %d\n", fullReach(), minOnCycle());
    if (fullReach()) {
      if (!finalCycleSent) {
        finalCycleSent = true;
        sendFinalCycle(graph[id], id);
      }
    } else {
      if (cycle != lastCycle) {
        int cand = -1;
        int q;
        for (auto v : canSendTo) {
          if (graph[v] == -1) {
            cand = v;
            q = 0;
            break;
          }
        }
        if (cand == -1) {
          for (auto v : canSendTo) {
            if (find(cycle.begin(), cycle.end(), v) == cycle.end()) {
              cand = v;
              q = 1;
              break;
            }
          }
        }
        if (cand != -1) {
          // fprintf(stderr, "init cycle %d %d %d\n", id, cand, q);
          sendCycle(graph[id], id, cand, q);
          lastCycle = cycle;
        }
      }
    }
  }
}

int main() {
  id = MyNodeId();
  totalNodes = NumberOfNodes();
  lastSentGraph.assign(totalNodes, -1);
  graph.assign(totalNodes, -1);
  for (int i = NumberOfCompanies()-1; i >= 0; i--) {
    shares[id].push_back(GetShareCost(i));
  }
  for (int i = 0; i < totalNodes; i++) {
    if (i != id) {
      sendPing(i);
    }
  }
  for (int i = 0; i < totalNodes; i++) {
    if (i == id) {
      continue;
    }
    int t = Receive(i);
    int type = GetInt(t);
    if (type == PING) {
      canReceiveFrom.insert(t);
      sendPong(t);
    }
  }


  while (1) {
    int t = Receive(-1);
    int type = GetInt(t);
    debugmsg("receive %d from %d\n", type, t);
    switch (type) {
      case NOPE:
        if (receivedNope[t]) {
          return 0;
        }
        receivedNope[t] = true;
      // FALLTHROUGH
      case PONG:
        if (graph[id] == -1) {
          graph[id] = t;
          debug("set parent to %d\n", t);
          sendGraph(graph[id]);
        } else {
          canSendTo.insert(t);
          checkCycle();
        }
        break;

      case GRAPH:
        {
          receiveGraph(t);
          if (graph[id] != -1) {
            sendGraph(graph[id]);
          } else {
            if (done()) {
              return 0;
            }
          }
          checkCycle();
        }
        break;

      case CYCLE:
        {
          auto pi = receiveCycle(t);
          if (finalCycleSent) {
            break;
          }
          int v = pi.first.first;
          int cand = pi.first.second;
          int q = pi.second;
          // fprintf(stderr, "receive cycle %d %d %d\n", v, cand, q);
          if (v == id) {
            if (graph[id] == cand) {
              break;
            }
            // fprintf(stderr, "graph change from %d to %d\n", graph[id], cand);
            massert(graph[id] != cand);
            graph[id] = cand;
            lastSentGraph.assign(totalNodes, -1);
            sendGraph(graph[id]);
          } else {
            if (id < v || q == 1) {
              for (auto newcand : canSendTo) {
                if (graph[newcand] == -1) {
                  cand = newcand;
                  v = id;
                  q = 0;
                  // fprintf(stderr, "override cycle %d %d\n", v, cand);
                  break;
                }
              }
            }
            if (graph[id] != -1) {
              sendCycle(graph[id], v, cand, q);
            }
          }
        }
        break;

      case FINAL_CYCLE:
        {
          int cand = receiveFinalCycle(t);
          debug("final cycle %d\n", cand);
          if (cand == id) {
            bool d = done();
            // massert(d);
            return 0;
          } else {
            finalCycleSent = true;
            sendGraph(graph[id]);
            sendFinalCycle(graph[id], min(id, cand));
          }
        }
        break;

      case DIE:
        return 0;
    }
  }
  return 0;
}