Submission #212959

# Submission time Handle Problem Language Result Execution time Memory
212959 2020-03-24T15:49:25 Z rama_pang Stray Cat (JOI20_stray) C++14
100 / 100
122 ms 17524 KB
#include "Anthony.h"
#include "bits/stdc++.h"
using namespace std;

namespace {

vector<int> Bfs(int N, vector<vector<int>> adj) {
  queue<int> q;
  q.emplace(0);

  vector<int> dist(N, -1);
  dist[0] = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto &v : adj[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        q.emplace(v);
      }
    }
  }

  return dist;
}

vector<int> SolveGeneral(int N, int M, int A, int B, vector<int> U, vector<int> V) { // solve for A = 3 with B = 0 for any graph
  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    adj[U[i]].emplace_back(V[i]);
    adj[V[i]].emplace_back(U[i]);
  }

  vector<int> dist = Bfs(N, adj);

  vector<int> res;
  for (int i = 0; i < M; i++) {
    if (dist[U[i]] > dist[V[i]]) swap(U[i], V[i]);
    res.emplace_back(dist[U[i]] % 3);
  }

  return res;

}

vector<int> Mask;

void GenerateMask() {
  for (int len = 1; true; len++) {
    for (int mask = 0; mask < (1 << len); mask++) {
      vector<int> String;
      for (int i = 0; i < len; i++) {
        if (mask & (1 << i)) {
          String.emplace_back(1);
        } else {
          String.emplace_back(0);
        }
      }

      {
        int cnt = 0;
        while (String.size() < 100) {
          String.emplace_back(String[cnt]);
          cnt++;
        }
      }

      map<vector<int>, int> direction;
      for (int i = 0; i + 4 < String.size(); i++) {
        vector<int> cur;
        for (int j = i; j < i + 5; j++) {
          cur.emplace_back(String[j]);
        }
        direction[cur] |= 1;
        reverse(begin(cur), end(cur));
        direction[cur] |= 2;
      }
      
      { // when starting the sequence, adj[n].size() == 2 && adj[p].size() != 2, edge (n, p) can either be 0 or 1 (4 true bits from Mask)
        vector<int> cur;
        cur.emplace_back(0);
        for (int i = 0; i < 4; i++) {
          cur.emplace_back(String[i]);
        }
        // wrong direction
        direction[cur] |= 1;
        cur.front() ^= 1;
        direction[cur] |= 1;

        // right direction
        reverse(begin(cur), end(cur));
        direction[cur] |= 2;
        cur.back() ^= 1;
        direction[cur] |= 2;
      }

      bool can = true;

      for (auto &i : direction) {
        if (i.second == 3) {
          can = false;
        }
      }

      if (can) {
        for (int i = 0; i < len; i++) {
          Mask.emplace_back(String[i]);
        }
        return;
      }
    }
  }
}

vector<int> ans;
map<pair<int, int>, int> UV_id;

void Dfs(int n, int p, int cnt, const vector<vector<int>> &adj, vector<int> &color) {
  if (cnt != -1) {
    cnt %= Mask.size();
  }

  if (p != -1) {
    if (cnt == -1) {
      color[n] = color[p] ^ 1;
    } else {
      color[n] = Mask[cnt];
    }
    ans[UV_id[{n, p}]] = color[n];
  } else {
    color[n] = 1;
  }

  for (auto &i : adj[n]) if (i != p) {
    if (n != 0 && adj[n].size() == 2) {
      Dfs(i, n, cnt + 1, adj, color);
    } else {
      Dfs(i, n, -1, adj, color);
    }
  }
}

vector<int> SolveTree(int N, int M, int A, int B, vector<int> U, vector<int> V) { // solve for A = 2 with B = 6 for a tree
  vector<vector<int>> adj(N);
  ans.resize(M);

  for (int i = 0; i < M; i++) {
    adj[U[i]].emplace_back(V[i]);
    adj[V[i]].emplace_back(U[i]);
    UV_id[{U[i], V[i]}] = UV_id[{V[i], U[i]}] = i;
  }

  GenerateMask();
  vector<int> parent_edge_color(N, -1);
  Dfs(0, -1, -1, adj, parent_edge_color);

  for (int i = 0; i < M; i++) {
    assert(0 <= ans[i] && ans[i] < A);
  }

  return ans;
}

}  // namespace

vector<int> Mark(int N, int M, int A, int B, vector<int> U, vector<int> V) {
  if (A >= 3) {
    return SolveGeneral(N, M, A, B, U, V); // A = 3 and B = 0
  } else {
    return SolveTree(N, M, A, B, U, V); // A = 2 and B >= 6
  }
}
#include "Catherine.h"
#include "bits/stdc++.h"
using namespace std;

namespace {

set<vector<int>> RIGHT_DIRECTION;
set<vector<int>> WRONG_DIRECTION;

void GenerateMask() {
  for (int len = 1; true; len++) {
    for (int mask = 0; mask < (1 << len); mask++) {
      vector<int> String;
      for (int i = 0; i < len; i++) {
        if (mask & (1 << i)) {
          String.emplace_back(1);
        } else {
          String.emplace_back(0);
        }
      }

      {
        int cnt = 0;
        while (String.size() < 100) {
          String.emplace_back(String[cnt]);
          cnt++;
        }
      }

      map<vector<int>, int> direction;
      for (int i = 0; i + 4 < String.size(); i++) {
        vector<int> cur;
        for (int j = i; j < i + 5; j++) {
          cur.emplace_back(String[j]);
        }
        direction[cur] |= 1;
        reverse(begin(cur), end(cur));
        direction[cur] |= 2;
      }

      { // when starting the sequence, adj[n].size() == 2 && adj[p].size() != 2, edge (n, p) can either be 0 or 1 (4 true bits from Mask)
        vector<int> cur;
        cur.emplace_back(0);
        for (int i = 0; i < 4; i++) {
          cur.emplace_back(String[i]);
        }
        // wrong direction
        direction[cur] |= 1;
        cur.front() ^= 1;
        direction[cur] |= 1;

        // right direction
        reverse(begin(cur), end(cur));
        direction[cur] |= 2;
        cur.back() ^= 1;
        direction[cur] |= 2;
      }

      bool can = true;

      for (auto &i : direction) {
        if (i.second == 1 || i.second == 2) {
          continue;
        }
        can = false;
      }

      if (can) {
        for (auto &i : direction) {
          if (i.second == 1) { // wrong direction
            WRONG_DIRECTION.emplace(i.first);
          } else if (i.second == 2) {
            RIGHT_DIRECTION.emplace(i.first);
          } else {
            assert(false);
          }
        }
        return;
      }
    }
  }
}

int A, B;
int variable_example = 0;

int SolveGeneral(vector<int> y) { // solve for A = 3 with B = 0 for any graph
  // case: there is only one possible edge to go
  if (y[0] == 0 && y[1] == 0) {
    return 2;
  } else if (y[0] == 0 && y[2] == 0) {
    return 1;
  } else if (y[1] == 0 && y[2] == 0) {
    return 0;
  }

  // general case
  if (y[0] == 0) { // case 12, go to 1
    return 1;
  } else if (y[1] == 0) { // case 20, go to 2
    return 2;
  } else if (y[2] == 0) { // case 01, go to 1
    return 0;
  }

  // this should never be triggered
  return -1;
}

int SolveTree(vector<int> y) { // solve for A = 2 with B = 12 for a tree
  static int determined = 0; // determined the correct way
  static int last = -1;

  // if not yet determined
  static vector<int> read;

  if (last == -1) {
    if (y[0] + y[1] == 2) {
      if (y[0] == 1 && y[1] == 1) {
        read.emplace_back(1);
        read.emplace_back(0);
        return last = 0;
      } else if (y[0] == 2 && y[1] == 0) {
        read.emplace_back(0);
        read.emplace_back(0);
        return last = 0;
      } else if (y[0] == 0 && y[1] == 2) {
        read.emplace_back(1);
        read.emplace_back(1);
        return last = 1;
      }
    } else {
      determined = 1;
      if (y[0] == 1) {
        return last = 0;
      } else if (y[1] == 1) {
        return last = 1;
      }
    }
  }

  if (y[0] == 0 && y[1] == 0) {
    determined = 1;
    return -1;
  }

  y[last]++;
  if (determined) {
    if (y[0] + y[1] == 2) {
      y[last]--;
      if (y[0] == 1) {
        return last = 0;
      } else if (y[1] == 1) {
        return last = 1;
      } else {
        return -1;
      }
    } else if (y[0] == 1) {
      if (last == 0) {
        return -1;
      } else {
        return last = 0;
      }
    } else if (y[1] == 1) {
      if (last == 1) {
        return -1;
      } else {
        return last = 1;
      }
    } else {
      return -1;
    }
  }

  if (y[0] + y[1] != 2) {
    determined = 1;
    if (y[0] == 1) {
      if (last == 0) {
        return -1;
      } else {
        return last = 0;
      }
    } else if (y[1] == 1) {
      if (last == 1) {
        return -1;
      } else {
        return last = 1;
      }
    } else {
      return -1;
    }
  }

  y[last]--;

  if (read.size() < 4) {
    if (y[0] == 1) {
      read.emplace_back(0);
      return last = 0;
    } else if (y[1] == 1) {
      read.emplace_back(1);
      return last = 1;
    } else {
      determined = 1;
      return -1;
    }
  } else if (read.size() == 4) {
    if (y[0] == 1) {
      read.emplace_back(0);
    } else if (y[1] == 1) {
      read.emplace_back(1);
    } else {
      determined = 1;
      return -1;
    }
  } 
  
  if (read.size() == 5) {
    determined = 1;
    if (WRONG_DIRECTION.count(read) == 1) {
      return -1;
    } else if (RIGHT_DIRECTION.count(read) == 1) {
      if (y[0] == 1) {
        return last = 0;
      } else if (y[1] == 1) {
        return last = 1;
      } else {
        assert(false);
      }
    } else {
      return -1;
    }
  }
}

}  // namespace

void Init(int A, int B) {
  ::A = A;
  ::B = B;
  GenerateMask();
}

int Move(vector<int> y) {
  if (A >= 3) {
    return SolveGeneral(y);
  } else {
    return SolveTree(y);
  }
}

Compilation message

Anthony.cpp: In function 'void {anonymous}::GenerateMask()':
Anthony.cpp:70:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int i = 0; i + 4 < String.size(); i++) {
                       ~~~~~~^~~~~~~~~~~~~~~

Catherine.cpp: In function 'void {anonymous}::GenerateMask()':
Catherine.cpp:31:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int i = 0; i + 4 < String.size(); i++) {
                       ~~~~~~^~~~~~~~~~~~~~~
Catherine.cpp: In function 'int {anonymous}::SolveTree(std::vector<int>)':
Catherine.cpp:234:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Catherine.cpp: At global scope:
Catherine.cpp:85:5: warning: '{anonymous}::variable_example' defined but not used [-Wunused-variable]
 int variable_example = 0;
     ^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 65 ms 16228 KB Output is correct
2 Correct 13 ms 772 KB Output is correct
3 Correct 56 ms 15944 KB Output is correct
4 Correct 89 ms 17524 KB Output is correct
5 Correct 84 ms 17396 KB Output is correct
6 Correct 67 ms 16124 KB Output is correct
7 Correct 64 ms 16236 KB Output is correct
8 Correct 82 ms 16980 KB Output is correct
9 Correct 76 ms 17056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 16228 KB Output is correct
2 Correct 13 ms 772 KB Output is correct
3 Correct 56 ms 15944 KB Output is correct
4 Correct 89 ms 17524 KB Output is correct
5 Correct 84 ms 17396 KB Output is correct
6 Correct 67 ms 16124 KB Output is correct
7 Correct 64 ms 16236 KB Output is correct
8 Correct 82 ms 16980 KB Output is correct
9 Correct 76 ms 17056 KB Output is correct
10 Correct 57 ms 14072 KB Output is correct
11 Correct 70 ms 13976 KB Output is correct
12 Correct 82 ms 14072 KB Output is correct
13 Correct 75 ms 13964 KB Output is correct
14 Correct 67 ms 14468 KB Output is correct
15 Correct 99 ms 14584 KB Output is correct
16 Correct 68 ms 17020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 14000 KB Output is correct
2 Correct 13 ms 1072 KB Output is correct
3 Correct 48 ms 13684 KB Output is correct
4 Correct 80 ms 15348 KB Output is correct
5 Correct 73 ms 15228 KB Output is correct
6 Correct 59 ms 14204 KB Output is correct
7 Correct 63 ms 14068 KB Output is correct
8 Correct 82 ms 14716 KB Output is correct
9 Correct 68 ms 14716 KB Output is correct
10 Correct 62 ms 14324 KB Output is correct
11 Correct 59 ms 14448 KB Output is correct
12 Correct 64 ms 14368 KB Output is correct
13 Correct 60 ms 14488 KB Output is correct
14 Correct 77 ms 14708 KB Output is correct
15 Correct 66 ms 14780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 14000 KB Output is correct
2 Correct 13 ms 1072 KB Output is correct
3 Correct 48 ms 13684 KB Output is correct
4 Correct 80 ms 15348 KB Output is correct
5 Correct 73 ms 15228 KB Output is correct
6 Correct 59 ms 14204 KB Output is correct
7 Correct 63 ms 14068 KB Output is correct
8 Correct 82 ms 14716 KB Output is correct
9 Correct 68 ms 14716 KB Output is correct
10 Correct 62 ms 14324 KB Output is correct
11 Correct 59 ms 14448 KB Output is correct
12 Correct 64 ms 14368 KB Output is correct
13 Correct 60 ms 14488 KB Output is correct
14 Correct 77 ms 14708 KB Output is correct
15 Correct 66 ms 14780 KB Output is correct
16 Correct 52 ms 12152 KB Output is correct
17 Correct 55 ms 12288 KB Output is correct
18 Correct 54 ms 12208 KB Output is correct
19 Correct 59 ms 12100 KB Output is correct
20 Correct 71 ms 12752 KB Output is correct
21 Correct 66 ms 12528 KB Output is correct
22 Correct 67 ms 14844 KB Output is correct
23 Correct 56 ms 12056 KB Output is correct
24 Correct 55 ms 12284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1488 KB Output is correct
2 Correct 14 ms 780 KB Output is correct
3 Correct 14 ms 1036 KB Output is correct
4 Correct 14 ms 1276 KB Output is correct
5 Correct 14 ms 1172 KB Output is correct
6 Correct 14 ms 1172 KB Output is correct
7 Correct 16 ms 1172 KB Output is correct
8 Correct 16 ms 1184 KB Output is correct
9 Correct 15 ms 1164 KB Output is correct
10 Correct 15 ms 1276 KB Output is correct
11 Correct 15 ms 1164 KB Output is correct
12 Correct 15 ms 1048 KB Output is correct
13 Correct 15 ms 1164 KB Output is correct
14 Correct 16 ms 1300 KB Output is correct
15 Correct 14 ms 1172 KB Output is correct
16 Correct 16 ms 1036 KB Output is correct
17 Correct 14 ms 1172 KB Output is correct
18 Correct 17 ms 1036 KB Output is correct
19 Correct 14 ms 1204 KB Output is correct
20 Correct 15 ms 1156 KB Output is correct
21 Correct 15 ms 1408 KB Output is correct
22 Correct 16 ms 1172 KB Output is correct
23 Correct 14 ms 1164 KB Output is correct
24 Correct 14 ms 1048 KB Output is correct
25 Correct 14 ms 1172 KB Output is correct
26 Correct 15 ms 1300 KB Output is correct
27 Correct 14 ms 1156 KB Output is correct
28 Correct 15 ms 1164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 13532 KB Output is correct
2 Correct 97 ms 15044 KB Output is correct
3 Correct 15 ms 772 KB Output is correct
4 Correct 83 ms 13540 KB Output is correct
5 Correct 112 ms 17092 KB Output is correct
6 Correct 113 ms 17200 KB Output is correct
7 Correct 120 ms 16160 KB Output is correct
8 Correct 86 ms 16140 KB Output is correct
9 Correct 122 ms 17116 KB Output is correct
10 Correct 102 ms 17120 KB Output is correct
11 Correct 111 ms 17268 KB Output is correct
12 Correct 108 ms 17004 KB Output is correct
13 Correct 97 ms 17132 KB Output is correct
14 Correct 104 ms 17164 KB Output is correct
15 Correct 112 ms 17148 KB Output is correct
16 Correct 102 ms 17136 KB Output is correct
17 Correct 112 ms 16836 KB Output is correct
18 Correct 109 ms 16748 KB Output is correct
19 Correct 92 ms 16620 KB Output is correct
20 Correct 89 ms 16620 KB Output is correct
21 Correct 108 ms 16756 KB Output is correct
22 Correct 95 ms 16756 KB Output is correct
23 Correct 83 ms 13668 KB Output is correct
24 Correct 84 ms 13660 KB Output is correct
25 Correct 103 ms 14328 KB Output is correct
26 Correct 92 ms 14188 KB Output is correct
27 Correct 99 ms 15340 KB Output is correct
28 Correct 96 ms 15324 KB Output is correct
29 Correct 94 ms 15240 KB Output is correct
30 Correct 99 ms 15292 KB Output is correct
31 Correct 88 ms 13616 KB Output is correct
32 Correct 89 ms 13556 KB Output is correct
33 Correct 82 ms 14176 KB Output is correct
34 Correct 84 ms 14196 KB Output is correct
35 Correct 93 ms 15008 KB Output is correct
36 Correct 99 ms 15160 KB Output is correct
37 Correct 114 ms 15040 KB Output is correct
38 Correct 84 ms 15072 KB Output is correct
39 Correct 89 ms 15084 KB Output is correct
40 Correct 102 ms 15172 KB Output is correct
41 Correct 88 ms 15944 KB Output is correct
42 Correct 84 ms 15972 KB Output is correct
43 Correct 94 ms 15904 KB Output is correct
44 Correct 110 ms 15980 KB Output is correct
45 Correct 95 ms 15932 KB Output is correct
46 Correct 93 ms 15940 KB Output is correct
47 Correct 96 ms 14956 KB Output is correct
48 Correct 95 ms 14924 KB Output is correct
49 Correct 94 ms 14828 KB Output is correct
50 Correct 97 ms 14956 KB Output is correct
51 Correct 88 ms 13940 KB Output is correct
52 Correct 83 ms 13960 KB Output is correct
53 Correct 77 ms 13984 KB Output is correct
54 Correct 78 ms 13860 KB Output is correct
55 Correct 90 ms 13904 KB Output is correct
56 Correct 90 ms 14088 KB Output is correct
57 Correct 83 ms 13684 KB Output is correct
58 Correct 93 ms 13664 KB Output is correct
59 Correct 81 ms 13948 KB Output is correct
60 Correct 80 ms 13948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 13652 KB Output is correct
2 Correct 102 ms 15044 KB Output is correct
3 Correct 13 ms 908 KB Output is correct
4 Correct 73 ms 13504 KB Output is correct
5 Correct 104 ms 17184 KB Output is correct
6 Correct 113 ms 17196 KB Output is correct
7 Correct 84 ms 16244 KB Output is correct
8 Correct 86 ms 16248 KB Output is correct
9 Correct 90 ms 17132 KB Output is correct
10 Correct 95 ms 17116 KB Output is correct
11 Correct 98 ms 17132 KB Output is correct
12 Correct 98 ms 17008 KB Output is correct
13 Correct 97 ms 17216 KB Output is correct
14 Correct 100 ms 17104 KB Output is correct
15 Correct 121 ms 17076 KB Output is correct
16 Correct 98 ms 16996 KB Output is correct
17 Correct 99 ms 16624 KB Output is correct
18 Correct 102 ms 16756 KB Output is correct
19 Correct 101 ms 16732 KB Output is correct
20 Correct 88 ms 16732 KB Output is correct
21 Correct 105 ms 16588 KB Output is correct
22 Correct 97 ms 16760 KB Output is correct
23 Correct 78 ms 13744 KB Output is correct
24 Correct 80 ms 13636 KB Output is correct
25 Correct 88 ms 14152 KB Output is correct
26 Correct 83 ms 14204 KB Output is correct
27 Correct 89 ms 15620 KB Output is correct
28 Correct 100 ms 15352 KB Output is correct
29 Correct 91 ms 15356 KB Output is correct
30 Correct 100 ms 15344 KB Output is correct
31 Correct 77 ms 13648 KB Output is correct
32 Correct 93 ms 13688 KB Output is correct
33 Correct 80 ms 14316 KB Output is correct
34 Correct 91 ms 14284 KB Output is correct
35 Correct 93 ms 15084 KB Output is correct
36 Correct 99 ms 15212 KB Output is correct
37 Correct 99 ms 15076 KB Output is correct
38 Correct 99 ms 15084 KB Output is correct
39 Correct 88 ms 15072 KB Output is correct
40 Correct 106 ms 15036 KB Output is correct
41 Correct 107 ms 16128 KB Output is correct
42 Correct 96 ms 15836 KB Output is correct
43 Correct 93 ms 15892 KB Output is correct
44 Correct 108 ms 15988 KB Output is correct
45 Correct 110 ms 16076 KB Output is correct
46 Correct 90 ms 15996 KB Output is correct
47 Correct 96 ms 14920 KB Output is correct
48 Correct 84 ms 14964 KB Output is correct
49 Correct 97 ms 14828 KB Output is correct
50 Correct 91 ms 14956 KB Output is correct
51 Correct 96 ms 14012 KB Output is correct
52 Correct 111 ms 13912 KB Output is correct
53 Correct 94 ms 13952 KB Output is correct
54 Correct 86 ms 13940 KB Output is correct
55 Correct 80 ms 14380 KB Output is correct
56 Correct 83 ms 14412 KB Output is correct
57 Correct 93 ms 14192 KB Output is correct
58 Correct 89 ms 14140 KB Output is correct
59 Correct 83 ms 14404 KB Output is correct
60 Correct 80 ms 14408 KB Output is correct