Submission #515945

# Submission time Handle Problem Language Result Execution time Memory
515945 2022-01-20T07:29:22 Z 600Mihnea Saveit (IOI10_saveit) C++17
100 / 100
279 ms 10728 KB
#include "grader.h"
#include "encoder.h"
#include <bits/stdc++.h>

using namespace std;

void encode(int nv, int nh, int ne, int *v1, int *v2) {
  const int STORING_BITS = 10;

  vector<vector<int>> g(nv);
  vector<int> parrent(nv, -2);
  vector<bool> vis(nv, 0);
  for (int i = 0; i < ne; i++) {
    g[v1[i]].push_back(v2[i]);
    g[v2[i]].push_back(v1[i]);
  }
  function<void(int)> dfs = [&] (int a) {
    vis[a] = 1;
    for (auto &b : g[a]) {
      if (!vis[b]) {
        parrent[b] = a;
        dfs(b);
      }
    }
  };
  parrent[0] = -1;
  dfs(0);
  int cnt = 0;
  for (int i = 1; i < nv; i++) {
    for (int bit = 0; bit < STORING_BITS; bit++) {
      if (parrent[i] & (1 << bit)) {
        encode_bit(1);
        cnt++;
      } else {
        encode_bit(0);
        cnt++;
      }
    }
  }
  vector<int> stored;
  for (int i = 0; i < nh; i++) {
    /// calculate the dist from vertex i
    vector<int> d(nv, -1);
    queue<int> q;
    q.push(i);
    d[i] = 0;
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      for (auto &b : g[a]) {
        if (d[b] == -1) {
          d[b] = 1 + d[a];
          q.push(b);
        }
      }
    }
    for (int i = 0; i < nv; i++) {
      assert(d[i] != -1);
    }
    for (int i = 1; i < nv; i++) {
      int dif = d[i] - d[parrent[i]];
      assert(-1 <= dif && dif <= +1);
      stored.push_back(dif + 1);
    }
    continue;
    cout << " : ";
    for (int i = 0; i < nv; i++) {
      cout << d[i] << " ";
    }
    cout << "\n";
  }

  for (int l = 0; l < (int) stored.size(); l += 3) {
    int r = min((int) stored.size(), l + 2);
    int num = 0;
    for (int j = l; j <= r; j++) {
      num = num * 3 + stored[j];
    }
    for (int bit = 0; bit < 5; bit++) {
      encode_bit(num % 2);
      num /= 2;
    }
  }
  return;
  for (auto &send : stored) {
    assert(0 <= send && send <= 2);
    if (send == 0) {
      encode_bit(0);
      encode_bit(0);
    }
    if (send == 1) {
      encode_bit(0);
      encode_bit(1);
    }
    if (send == 2) {
      encode_bit(1);
      encode_bit(0);
    }
  }
  return;
  assert((int) stored.size() == nh * (nv - 1));
  for (int i = 0; i < nv; i++) {
    cout << i << " : " << parrent[i] << "\n";
  }
  cout << " = ";
  for (auto &x : stored) {
    cout << x << " ";
  }
  cout << "\n";
  return;
  exit(0);
  encode_bit(1);
  encode_bit(0);
  return;
}
#include "grader.h"
#include "decoder.h"
#include <bits/stdc++.h>

using namespace std;

void decode(int nv, int nh) {
  const int STORING_BITS = 10;
  vector<int> parrent(nv, 0);
  parrent[0] = -1;
  for (int i = 1; i < nv; i++) {
    for (int bit = 0; bit < STORING_BITS; bit++) {
      int x = decode_bit();
      parrent[i] += (1 << bit) * x;
    }
  }
 // cout << "hey\n";

  vector<int> stored(nh * (nv - 1));
  for (int l = 0; l < (int) stored.size(); l += 3) {
    int r = min((int) stored.size(), l + 2);
    int num = 0;
    for (int bit = 0; bit < 5; bit++) {
      num += (1 << bit) * decode_bit();
    }
    for (int j = r; j >= l; j--) {
      stored[j] = num % 3;
      num /= 3;
    }
  }
  int po = 0;
  for (int r = 0; r < nh; r++) {
    /// calculate the dist from vertex i
    vector<vector<pair<int, int>>> g(nv);
    for (int i = 1; i < nv; i++) {
      int dif = stored[po++] - 1;
      int j = parrent[i];
      g[i].push_back({j, dif});
      g[j].push_back({i, -dif});
    }
    const int TOKEN = -(int) 1e9;
    vector<int> sol(nv, TOKEN);
    sol[r] = 0;
    function<void(int)> explore = [&] (int a) {
      for (auto &b : g[a]) {
        if (sol[b.first] == TOKEN) {
          sol[b.first] = sol[a] - b.second;
          explore(b.first);
        }
      }
    };
    explore(r);
    for (int i = 0; i < nv; i++) {
      hops(r, i, sol[i]);
    }
  }
}

# Verdict Execution time Memory Grader output
1 Correct 279 ms 10728 KB Output is correct - 69930 call(s) of encode_bit()
2 Correct 2 ms 4452 KB Output is correct - 60 call(s) of encode_bit()
3 Correct 24 ms 5864 KB Output is correct - 62930 call(s) of encode_bit()
4 Correct 3 ms 4580 KB Output is correct - 75 call(s) of encode_bit()
5 Correct 28 ms 5992 KB Output is correct - 62930 call(s) of encode_bit()
6 Correct 36 ms 6012 KB Output is correct - 69930 call(s) of encode_bit()
7 Correct 44 ms 6468 KB Output is correct - 69930 call(s) of encode_bit()
8 Correct 22 ms 5848 KB Output is correct - 67200 call(s) of encode_bit()
9 Correct 25 ms 5828 KB Output is correct - 69930 call(s) of encode_bit()
10 Correct 34 ms 5824 KB Output is correct - 69930 call(s) of encode_bit()
11 Correct 31 ms 5936 KB Output is correct - 69930 call(s) of encode_bit()
12 Correct 24 ms 5868 KB Output is correct - 69930 call(s) of encode_bit()
13 Correct 57 ms 6440 KB Output is correct - 69930 call(s) of encode_bit()
14 Correct 28 ms 5852 KB Output is correct - 69930 call(s) of encode_bit()
15 Correct 27 ms 6024 KB Output is correct - 69930 call(s) of encode_bit()
16 Correct 63 ms 6400 KB Output is correct - 69930 call(s) of encode_bit()
17 Correct 55 ms 6336 KB Output is correct - 69930 call(s) of encode_bit()
18 Correct 56 ms 6644 KB Output is correct - 69930 call(s) of encode_bit()
19 Correct 50 ms 6296 KB Output is correct - 69930 call(s) of encode_bit()
20 Correct 81 ms 6876 KB Output is correct - 69930 call(s) of encode_bit()
21 Correct 100 ms 7012 KB Output is correct - 69930 call(s) of encode_bit()
22 Correct 55 ms 6380 KB Output is correct - 69930 call(s) of encode_bit()
23 Correct 74 ms 7240 KB Output is correct - 69930 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 279 ms 10728 KB Output is correct - 69930 call(s) of encode_bit()
2 Correct 2 ms 4452 KB Output is correct - 60 call(s) of encode_bit()
3 Correct 24 ms 5864 KB Output is correct - 62930 call(s) of encode_bit()
4 Correct 3 ms 4580 KB Output is correct - 75 call(s) of encode_bit()
5 Correct 28 ms 5992 KB Output is correct - 62930 call(s) of encode_bit()
6 Correct 36 ms 6012 KB Output is correct - 69930 call(s) of encode_bit()
7 Correct 44 ms 6468 KB Output is correct - 69930 call(s) of encode_bit()
8 Correct 22 ms 5848 KB Output is correct - 67200 call(s) of encode_bit()
9 Correct 25 ms 5828 KB Output is correct - 69930 call(s) of encode_bit()
10 Correct 34 ms 5824 KB Output is correct - 69930 call(s) of encode_bit()
11 Correct 31 ms 5936 KB Output is correct - 69930 call(s) of encode_bit()
12 Correct 24 ms 5868 KB Output is correct - 69930 call(s) of encode_bit()
13 Correct 57 ms 6440 KB Output is correct - 69930 call(s) of encode_bit()
14 Correct 28 ms 5852 KB Output is correct - 69930 call(s) of encode_bit()
15 Correct 27 ms 6024 KB Output is correct - 69930 call(s) of encode_bit()
16 Correct 63 ms 6400 KB Output is correct - 69930 call(s) of encode_bit()
17 Correct 55 ms 6336 KB Output is correct - 69930 call(s) of encode_bit()
18 Correct 56 ms 6644 KB Output is correct - 69930 call(s) of encode_bit()
19 Correct 50 ms 6296 KB Output is correct - 69930 call(s) of encode_bit()
20 Correct 81 ms 6876 KB Output is correct - 69930 call(s) of encode_bit()
21 Correct 100 ms 7012 KB Output is correct - 69930 call(s) of encode_bit()
22 Correct 55 ms 6380 KB Output is correct - 69930 call(s) of encode_bit()
23 Correct 74 ms 7240 KB Output is correct - 69930 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 279 ms 10728 KB Output is correct - 69930 call(s) of encode_bit()
2 Correct 2 ms 4452 KB Output is correct - 60 call(s) of encode_bit()
3 Correct 24 ms 5864 KB Output is correct - 62930 call(s) of encode_bit()
4 Correct 3 ms 4580 KB Output is correct - 75 call(s) of encode_bit()
5 Correct 28 ms 5992 KB Output is correct - 62930 call(s) of encode_bit()
6 Correct 36 ms 6012 KB Output is correct - 69930 call(s) of encode_bit()
7 Correct 44 ms 6468 KB Output is correct - 69930 call(s) of encode_bit()
8 Correct 22 ms 5848 KB Output is correct - 67200 call(s) of encode_bit()
9 Correct 25 ms 5828 KB Output is correct - 69930 call(s) of encode_bit()
10 Correct 34 ms 5824 KB Output is correct - 69930 call(s) of encode_bit()
11 Correct 31 ms 5936 KB Output is correct - 69930 call(s) of encode_bit()
12 Correct 24 ms 5868 KB Output is correct - 69930 call(s) of encode_bit()
13 Correct 57 ms 6440 KB Output is correct - 69930 call(s) of encode_bit()
14 Correct 28 ms 5852 KB Output is correct - 69930 call(s) of encode_bit()
15 Correct 27 ms 6024 KB Output is correct - 69930 call(s) of encode_bit()
16 Correct 63 ms 6400 KB Output is correct - 69930 call(s) of encode_bit()
17 Correct 55 ms 6336 KB Output is correct - 69930 call(s) of encode_bit()
18 Correct 56 ms 6644 KB Output is correct - 69930 call(s) of encode_bit()
19 Correct 50 ms 6296 KB Output is correct - 69930 call(s) of encode_bit()
20 Correct 81 ms 6876 KB Output is correct - 69930 call(s) of encode_bit()
21 Correct 100 ms 7012 KB Output is correct - 69930 call(s) of encode_bit()
22 Correct 55 ms 6380 KB Output is correct - 69930 call(s) of encode_bit()
23 Correct 74 ms 7240 KB Output is correct - 69930 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 279 ms 10728 KB Output is correct - 69930 call(s) of encode_bit()
2 Correct 2 ms 4452 KB Output is correct - 60 call(s) of encode_bit()
3 Correct 24 ms 5864 KB Output is correct - 62930 call(s) of encode_bit()
4 Correct 3 ms 4580 KB Output is correct - 75 call(s) of encode_bit()
5 Correct 28 ms 5992 KB Output is correct - 62930 call(s) of encode_bit()
6 Correct 36 ms 6012 KB Output is correct - 69930 call(s) of encode_bit()
7 Correct 44 ms 6468 KB Output is correct - 69930 call(s) of encode_bit()
8 Correct 22 ms 5848 KB Output is correct - 67200 call(s) of encode_bit()
9 Correct 25 ms 5828 KB Output is correct - 69930 call(s) of encode_bit()
10 Correct 34 ms 5824 KB Output is correct - 69930 call(s) of encode_bit()
11 Correct 31 ms 5936 KB Output is correct - 69930 call(s) of encode_bit()
12 Correct 24 ms 5868 KB Output is correct - 69930 call(s) of encode_bit()
13 Correct 57 ms 6440 KB Output is correct - 69930 call(s) of encode_bit()
14 Correct 28 ms 5852 KB Output is correct - 69930 call(s) of encode_bit()
15 Correct 27 ms 6024 KB Output is correct - 69930 call(s) of encode_bit()
16 Correct 63 ms 6400 KB Output is correct - 69930 call(s) of encode_bit()
17 Correct 55 ms 6336 KB Output is correct - 69930 call(s) of encode_bit()
18 Correct 56 ms 6644 KB Output is correct - 69930 call(s) of encode_bit()
19 Correct 50 ms 6296 KB Output is correct - 69930 call(s) of encode_bit()
20 Correct 81 ms 6876 KB Output is correct - 69930 call(s) of encode_bit()
21 Correct 100 ms 7012 KB Output is correct - 69930 call(s) of encode_bit()
22 Correct 55 ms 6380 KB Output is correct - 69930 call(s) of encode_bit()
23 Correct 74 ms 7240 KB Output is correct - 69930 call(s) of encode_bit()