Submission #565369

# Submission time Handle Problem Language Result Execution time Memory
565369 2022-05-20T18:59:17 Z shrimb Saveit (IOI10_saveit) C++17
100 / 100
348 ms 14024 KB
#include "grader.h"
#include "encoder.h"
#include"bits/stdc++.h"
using namespace std;

#define BSZ 1665

bitset<BSZ> operator + (const bitset<BSZ> &a, const bitset<BSZ> &b) {
    if (b.count() == 0) return a;
    return (a^b) + ((a&b)<<1);
}

vector<int> b2t(const vector<bool>& in);// {
//   vector<int> out;
//   for (bool carry : in) {
//     for (int& trit : out) {
//       int new_trit = 2 * trit + carry;
//       carry = new_trit / 3;
//       trit = new_trit % 3;
//     }
//     if (carry) {
//       out.push_back(1);
//     }
//   }
//   out.reserve(out.size());
//   return out;
// }

void encode(int n, int h, int p, int *v1, int *v2){
    vector<int> adj[n];
    for (int i = 0 ; i < p ; i++) {
        adj[v1[i]].push_back(v2[i]);
        adj[v2[i]].push_back(v1[i]);
    }

    int par[n];
    {
        queue<int> q;
        vector<bool> vis(n);
        q.push(0);
        while (q.size()) {
            auto cur = q.front();
            q.pop();
            for (int j : adj[cur]) {
                if (!vis[j]) {
                    vis[j] = 1;
                    par[j] = cur;
                    q.push(j);
                }
            }
        }
    }
    auto sendnum = [&](int x) {
        for (int k = 0 ; k < 10 ; k++) {
            encode_bit(!!(x & (1 << k)));
        }
    };
    for (int i = 1 ; i < n ; i++) sendnum(par[i]);
    for (int i = 1 ; i < h ; i++) {
        queue<int> q;
        vector<int> dis(n, -1);
        dis[i] = 0;
        q.push(i);
        while (q.size()) {
            auto cur = q.front();
            q.pop();
            for (int j : adj[cur]) {
                if (dis[j] == -1) {
                    dis[j] = dis[cur] + 1;
                    q.push(j);
                }
            }
        }
        bitset<BSZ> b = 0;
        for (int i = 1 ; i < n ; i++) {
            int x = dis[i] - dis[par[i]];
            // cerr << x << " ";
            b = b + b + b;
            if (x == 1) b = b + bitset<BSZ>(1);
            else if (x == -1) b = b + bitset<BSZ>(2);
        }
        // vector<bool> bin(BSZ);
        // int id = 0;
        for (int i = BSZ - 1 ; i >=0 ; i--) {
            encode_bit(b[i]);
            // bin[id++] = b[i];
        }
        // auto t = b2t(bin);
        // cerr << "T(encode): ";
        // for (int j : t) cerr << j << " " ;
        // cerr << endl;
    }
}
#include "grader.h"
#include "decoder.h"
#include "bits/stdc++.h"
using namespace std;
vector<int> b2t(const vector<bool>& in) {
  vector<int> out;
  for (bool carry : in) {
    for (int& trit : out) {
      int new_trit = 2 * trit + carry;
      carry = new_trit / 3;
      trit = new_trit % 3;
    }
    if (carry) {
      out.push_back(1);
    }
  }
  out.reserve(out.size());
  return out;
}
void decode(int n, int h) {
    vector<int> adj[n];
    int par[n];
    auto getnum = [&]() {
        int ret = 0;
        for (int k = 0 ; k < 10 ; k++) {
            ret |= (1 << k)*decode_bit();
        }
        return ret;
    };
    for (int i = 1 ; i < n ; i++) {
        int x = getnum();
        par[i] = x;
        adj[i].push_back(x);
        adj[x].push_back(i);
    }
    {
        queue<int> q;
        vector<int> dis(n, -1);
        dis[0] = 0;
        q.push(0);
        while (q.size()) {
            auto cur = q.front();
            q.pop();
            for (int j : adj[cur]) {
                if (dis[j] == -1) {
                    dis[j] = dis[cur] + 1;
                    q.push(j);
                }
            }
        }
        for (int i = 0 ; i < n ; i++) {
            hops(0, i, dis[i]);
        }
    }
    int w[n][n];
    for (int i = 1 ; i < h ; i++) {
        vector<bool> b;
        for (int j = 0 ; j < 1665 ; j++) b.push_back(decode_bit());
        vector<int> t = b2t(b);
        while (t.size() < n - 1) t.push_back(0);
        reverse(t.begin(), t.end());
        // cerr << "t(" << t.size() << "): ";
        // for (auto k : t) cerr << k << " ";
        // cerr << endl;
        for (int j = 1 ; j < n ; j++) {
            int v = t[j-1];
            if (v == 2) v = -1;
            w[par[j]][j] = v;
            w[j][par[j]] = -v;
        }
        queue<int> q;
        vector<int> dis(n, -1e9);
        dis[i] = 0;
        q.push(i);
        while (q.size()) {
            auto cur = q.front();
            q.pop();
            for (int j : adj[cur]) {
                if (dis[j] == -1e9) {
                    dis[j] = dis[cur] + w[cur][j];
                    q.push(j);
                }
            }
        }
        for (int j = 0 ; j < n ; j++) {
            hops(i, j, dis[j]);
        }
    }
}

Compilation message

decoder.cpp: In function 'void decode(int, int)':
decoder.cpp:60:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         while (t.size() < n - 1) t.push_back(0);
      |                ~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 348 ms 14024 KB Output is correct - 68265 call(s) of encode_bit()
2 Correct 3 ms 4604 KB Output is correct - 3370 call(s) of encode_bit()
3 Correct 143 ms 8516 KB Output is correct - 67265 call(s) of encode_bit()
4 Correct 3 ms 4588 KB Output is correct - 6700 call(s) of encode_bit()
5 Correct 142 ms 8852 KB Output is correct - 67265 call(s) of encode_bit()
6 Correct 165 ms 9480 KB Output is correct - 68265 call(s) of encode_bit()
7 Correct 174 ms 9728 KB Output is correct - 68265 call(s) of encode_bit()
8 Correct 158 ms 8900 KB Output is correct - 67875 call(s) of encode_bit()
9 Correct 165 ms 9444 KB Output is correct - 68265 call(s) of encode_bit()
10 Correct 173 ms 9192 KB Output is correct - 68265 call(s) of encode_bit()
11 Correct 174 ms 9368 KB Output is correct - 68265 call(s) of encode_bit()
12 Correct 165 ms 9132 KB Output is correct - 68265 call(s) of encode_bit()
13 Correct 179 ms 9968 KB Output is correct - 68265 call(s) of encode_bit()
14 Correct 187 ms 9268 KB Output is correct - 68265 call(s) of encode_bit()
15 Correct 169 ms 9304 KB Output is correct - 68265 call(s) of encode_bit()
16 Correct 191 ms 9732 KB Output is correct - 68265 call(s) of encode_bit()
17 Correct 212 ms 9704 KB Output is correct - 68265 call(s) of encode_bit()
18 Correct 187 ms 10136 KB Output is correct - 68265 call(s) of encode_bit()
19 Correct 174 ms 9632 KB Output is correct - 68265 call(s) of encode_bit()
20 Correct 227 ms 10392 KB Output is correct - 68265 call(s) of encode_bit()
21 Correct 204 ms 10648 KB Output is correct - 68265 call(s) of encode_bit()
22 Correct 179 ms 9952 KB Output is correct - 68265 call(s) of encode_bit()
23 Correct 212 ms 10620 KB Output is correct - 68265 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 348 ms 14024 KB Output is correct - 68265 call(s) of encode_bit()
2 Correct 3 ms 4604 KB Output is correct - 3370 call(s) of encode_bit()
3 Correct 143 ms 8516 KB Output is correct - 67265 call(s) of encode_bit()
4 Correct 3 ms 4588 KB Output is correct - 6700 call(s) of encode_bit()
5 Correct 142 ms 8852 KB Output is correct - 67265 call(s) of encode_bit()
6 Correct 165 ms 9480 KB Output is correct - 68265 call(s) of encode_bit()
7 Correct 174 ms 9728 KB Output is correct - 68265 call(s) of encode_bit()
8 Correct 158 ms 8900 KB Output is correct - 67875 call(s) of encode_bit()
9 Correct 165 ms 9444 KB Output is correct - 68265 call(s) of encode_bit()
10 Correct 173 ms 9192 KB Output is correct - 68265 call(s) of encode_bit()
11 Correct 174 ms 9368 KB Output is correct - 68265 call(s) of encode_bit()
12 Correct 165 ms 9132 KB Output is correct - 68265 call(s) of encode_bit()
13 Correct 179 ms 9968 KB Output is correct - 68265 call(s) of encode_bit()
14 Correct 187 ms 9268 KB Output is correct - 68265 call(s) of encode_bit()
15 Correct 169 ms 9304 KB Output is correct - 68265 call(s) of encode_bit()
16 Correct 191 ms 9732 KB Output is correct - 68265 call(s) of encode_bit()
17 Correct 212 ms 9704 KB Output is correct - 68265 call(s) of encode_bit()
18 Correct 187 ms 10136 KB Output is correct - 68265 call(s) of encode_bit()
19 Correct 174 ms 9632 KB Output is correct - 68265 call(s) of encode_bit()
20 Correct 227 ms 10392 KB Output is correct - 68265 call(s) of encode_bit()
21 Correct 204 ms 10648 KB Output is correct - 68265 call(s) of encode_bit()
22 Correct 179 ms 9952 KB Output is correct - 68265 call(s) of encode_bit()
23 Correct 212 ms 10620 KB Output is correct - 68265 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 348 ms 14024 KB Output is correct - 68265 call(s) of encode_bit()
2 Correct 3 ms 4604 KB Output is correct - 3370 call(s) of encode_bit()
3 Correct 143 ms 8516 KB Output is correct - 67265 call(s) of encode_bit()
4 Correct 3 ms 4588 KB Output is correct - 6700 call(s) of encode_bit()
5 Correct 142 ms 8852 KB Output is correct - 67265 call(s) of encode_bit()
6 Correct 165 ms 9480 KB Output is correct - 68265 call(s) of encode_bit()
7 Correct 174 ms 9728 KB Output is correct - 68265 call(s) of encode_bit()
8 Correct 158 ms 8900 KB Output is correct - 67875 call(s) of encode_bit()
9 Correct 165 ms 9444 KB Output is correct - 68265 call(s) of encode_bit()
10 Correct 173 ms 9192 KB Output is correct - 68265 call(s) of encode_bit()
11 Correct 174 ms 9368 KB Output is correct - 68265 call(s) of encode_bit()
12 Correct 165 ms 9132 KB Output is correct - 68265 call(s) of encode_bit()
13 Correct 179 ms 9968 KB Output is correct - 68265 call(s) of encode_bit()
14 Correct 187 ms 9268 KB Output is correct - 68265 call(s) of encode_bit()
15 Correct 169 ms 9304 KB Output is correct - 68265 call(s) of encode_bit()
16 Correct 191 ms 9732 KB Output is correct - 68265 call(s) of encode_bit()
17 Correct 212 ms 9704 KB Output is correct - 68265 call(s) of encode_bit()
18 Correct 187 ms 10136 KB Output is correct - 68265 call(s) of encode_bit()
19 Correct 174 ms 9632 KB Output is correct - 68265 call(s) of encode_bit()
20 Correct 227 ms 10392 KB Output is correct - 68265 call(s) of encode_bit()
21 Correct 204 ms 10648 KB Output is correct - 68265 call(s) of encode_bit()
22 Correct 179 ms 9952 KB Output is correct - 68265 call(s) of encode_bit()
23 Correct 212 ms 10620 KB Output is correct - 68265 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 348 ms 14024 KB Output is correct - 68265 call(s) of encode_bit()
2 Correct 3 ms 4604 KB Output is correct - 3370 call(s) of encode_bit()
3 Correct 143 ms 8516 KB Output is correct - 67265 call(s) of encode_bit()
4 Correct 3 ms 4588 KB Output is correct - 6700 call(s) of encode_bit()
5 Correct 142 ms 8852 KB Output is correct - 67265 call(s) of encode_bit()
6 Correct 165 ms 9480 KB Output is correct - 68265 call(s) of encode_bit()
7 Correct 174 ms 9728 KB Output is correct - 68265 call(s) of encode_bit()
8 Correct 158 ms 8900 KB Output is correct - 67875 call(s) of encode_bit()
9 Correct 165 ms 9444 KB Output is correct - 68265 call(s) of encode_bit()
10 Correct 173 ms 9192 KB Output is correct - 68265 call(s) of encode_bit()
11 Correct 174 ms 9368 KB Output is correct - 68265 call(s) of encode_bit()
12 Correct 165 ms 9132 KB Output is correct - 68265 call(s) of encode_bit()
13 Correct 179 ms 9968 KB Output is correct - 68265 call(s) of encode_bit()
14 Correct 187 ms 9268 KB Output is correct - 68265 call(s) of encode_bit()
15 Correct 169 ms 9304 KB Output is correct - 68265 call(s) of encode_bit()
16 Correct 191 ms 9732 KB Output is correct - 68265 call(s) of encode_bit()
17 Correct 212 ms 9704 KB Output is correct - 68265 call(s) of encode_bit()
18 Correct 187 ms 10136 KB Output is correct - 68265 call(s) of encode_bit()
19 Correct 174 ms 9632 KB Output is correct - 68265 call(s) of encode_bit()
20 Correct 227 ms 10392 KB Output is correct - 68265 call(s) of encode_bit()
21 Correct 204 ms 10648 KB Output is correct - 68265 call(s) of encode_bit()
22 Correct 179 ms 9952 KB Output is correct - 68265 call(s) of encode_bit()
23 Correct 212 ms 10620 KB Output is correct - 68265 call(s) of encode_bit()