Submission #212679

# Submission time Handle Problem Language Result Execution time Memory
212679 2020-03-24T05:10:08 Z rama_pang Stray Cat (JOI20_stray) C++14
29 / 100
131 ms 22132 KB
#include "Anthony.h"
#include "bits/stdc++.h"
using namespace std;

namespace {

int N, M, A, B;

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() < 20) {
          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;
      }

      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 {
    GenerateMask();
    return SolveTree(N, M, A, B, U, V); // A = 3 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() < 20) {
          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;
      }

      bool can = true;

      for (auto &i : direction) {
        if (i.second == 3) {
          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 {
      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 {
      assert(false);
    }
  } 
  
  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 {
        return -1;
      }
    } else {
      assert(false);
    }
  }
}


}  // 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:72:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for (int i = 0; i + 4 < String.size(); i++) {
                       ~~~~~~^~~~~~~~~~~~~~~
Anthony.cpp: At global scope:
Anthony.cpp:7:14: warning: '{anonymous}::B' defined but not used [-Wunused-variable]
 int N, M, A, B;
              ^
Anthony.cpp:7:11: warning: '{anonymous}::A' defined but not used [-Wunused-variable]
 int N, M, A, B;
           ^
Anthony.cpp:7:8: warning: '{anonymous}::M' defined but not used [-Wunused-variable]
 int N, M, A, B;
        ^
Anthony.cpp:7:5: warning: '{anonymous}::N' defined but not used [-Wunused-variable]
 int N, M, A, B;
     ^

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:214:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Catherine.cpp: At global scope:
Catherine.cpp:67:5: warning: '{anonymous}::variable_example' defined but not used [-Wunused-variable]
 int variable_example = 0;
     ^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 91 ms 16408 KB Output is correct
2 Correct 11 ms 740 KB Output is correct
3 Correct 72 ms 15728 KB Output is correct
4 Correct 117 ms 17524 KB Output is correct
5 Correct 83 ms 17592 KB Output is correct
6 Correct 68 ms 16352 KB Output is correct
7 Correct 73 ms 16128 KB Output is correct
8 Correct 79 ms 16820 KB Output is correct
9 Correct 91 ms 17144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 16408 KB Output is correct
2 Correct 11 ms 740 KB Output is correct
3 Correct 72 ms 15728 KB Output is correct
4 Correct 117 ms 17524 KB Output is correct
5 Correct 83 ms 17592 KB Output is correct
6 Correct 68 ms 16352 KB Output is correct
7 Correct 73 ms 16128 KB Output is correct
8 Correct 79 ms 16820 KB Output is correct
9 Correct 91 ms 17144 KB Output is correct
10 Correct 69 ms 14024 KB Output is correct
11 Correct 57 ms 14116 KB Output is correct
12 Correct 57 ms 14072 KB Output is correct
13 Correct 57 ms 14080 KB Output is correct
14 Correct 64 ms 14192 KB Output is correct
15 Correct 73 ms 14608 KB Output is correct
16 Correct 69 ms 17012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 14036 KB Output is correct
2 Correct 11 ms 908 KB Output is correct
3 Correct 50 ms 13564 KB Output is correct
4 Correct 112 ms 15220 KB Output is correct
5 Correct 71 ms 15348 KB Output is correct
6 Correct 66 ms 13948 KB Output is correct
7 Correct 62 ms 13820 KB Output is correct
8 Correct 70 ms 14740 KB Output is correct
9 Correct 72 ms 14700 KB Output is correct
10 Correct 61 ms 14404 KB Output is correct
11 Correct 63 ms 14456 KB Output is correct
12 Correct 62 ms 14436 KB Output is correct
13 Correct 79 ms 14436 KB Output is correct
14 Correct 78 ms 14708 KB Output is correct
15 Correct 72 ms 14700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 14036 KB Output is correct
2 Correct 11 ms 908 KB Output is correct
3 Correct 50 ms 13564 KB Output is correct
4 Correct 112 ms 15220 KB Output is correct
5 Correct 71 ms 15348 KB Output is correct
6 Correct 66 ms 13948 KB Output is correct
7 Correct 62 ms 13820 KB Output is correct
8 Correct 70 ms 14740 KB Output is correct
9 Correct 72 ms 14700 KB Output is correct
10 Correct 61 ms 14404 KB Output is correct
11 Correct 63 ms 14456 KB Output is correct
12 Correct 62 ms 14436 KB Output is correct
13 Correct 79 ms 14436 KB Output is correct
14 Correct 78 ms 14708 KB Output is correct
15 Correct 72 ms 14700 KB Output is correct
16 Correct 53 ms 12272 KB Output is correct
17 Correct 89 ms 12272 KB Output is correct
18 Correct 60 ms 12272 KB Output is correct
19 Correct 65 ms 12152 KB Output is correct
20 Correct 59 ms 12928 KB Output is correct
21 Correct 63 ms 12400 KB Output is correct
22 Correct 73 ms 14820 KB Output is correct
23 Correct 56 ms 12232 KB Output is correct
24 Correct 56 ms 12272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1172 KB Output is correct
2 Correct 12 ms 644 KB Output is correct
3 Correct 12 ms 1300 KB Output is correct
4 Correct 12 ms 1164 KB Output is correct
5 Correct 12 ms 1172 KB Output is correct
6 Correct 14 ms 1164 KB Output is correct
7 Correct 13 ms 1292 KB Output is correct
8 Correct 12 ms 1164 KB Output is correct
9 Correct 12 ms 1164 KB Output is correct
10 Correct 12 ms 1244 KB Output is correct
11 Correct 13 ms 1184 KB Output is correct
12 Correct 12 ms 1164 KB Output is correct
13 Correct 12 ms 1164 KB Output is correct
14 Correct 12 ms 1164 KB Output is correct
15 Correct 13 ms 1172 KB Output is correct
16 Correct 12 ms 1148 KB Output is correct
17 Correct 12 ms 1028 KB Output is correct
18 Correct 12 ms 1036 KB Output is correct
19 Correct 13 ms 1036 KB Output is correct
20 Correct 12 ms 1036 KB Output is correct
21 Correct 12 ms 1284 KB Output is correct
22 Correct 13 ms 1172 KB Output is correct
23 Correct 11 ms 1036 KB Output is correct
24 Correct 13 ms 1244 KB Output is correct
25 Correct 12 ms 1036 KB Output is correct
26 Correct 12 ms 1036 KB Output is correct
27 Correct 12 ms 1172 KB Output is correct
28 Correct 12 ms 1036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 13612 KB Output is correct
2 Correct 86 ms 15084 KB Output is correct
3 Correct 10 ms 908 KB Output is correct
4 Correct 73 ms 13560 KB Output is correct
5 Correct 108 ms 16988 KB Output is correct
6 Correct 95 ms 17044 KB Output is correct
7 Correct 89 ms 16244 KB Output is correct
8 Correct 84 ms 16236 KB Output is correct
9 Correct 104 ms 17100 KB Output is correct
10 Correct 98 ms 17104 KB Output is correct
11 Correct 104 ms 17004 KB Output is correct
12 Correct 106 ms 17072 KB Output is correct
13 Correct 125 ms 17052 KB Output is correct
14 Correct 103 ms 16996 KB Output is correct
15 Correct 102 ms 17252 KB Output is correct
16 Correct 100 ms 17004 KB Output is correct
17 Correct 109 ms 16716 KB Output is correct
18 Correct 103 ms 16860 KB Output is correct
19 Correct 98 ms 16840 KB Output is correct
20 Correct 110 ms 16740 KB Output is correct
21 Correct 95 ms 16752 KB Output is correct
22 Correct 103 ms 16808 KB Output is correct
23 Runtime error 84 ms 22132 KB Execution killed with signal 11 (could be triggered by violating memory limits)
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 13556 KB Output is correct
2 Correct 84 ms 15100 KB Output is correct
3 Correct 11 ms 1028 KB Output is correct
4 Correct 74 ms 13544 KB Output is correct
5 Correct 102 ms 17116 KB Output is correct
6 Correct 98 ms 17004 KB Output is correct
7 Correct 87 ms 16128 KB Output is correct
8 Correct 83 ms 16244 KB Output is correct
9 Correct 103 ms 17080 KB Output is correct
10 Correct 109 ms 17004 KB Output is correct
11 Correct 105 ms 17104 KB Output is correct
12 Correct 96 ms 17064 KB Output is correct
13 Correct 109 ms 17180 KB Output is correct
14 Correct 101 ms 17068 KB Output is correct
15 Correct 101 ms 17004 KB Output is correct
16 Correct 115 ms 17076 KB Output is correct
17 Correct 104 ms 16856 KB Output is correct
18 Correct 100 ms 16712 KB Output is correct
19 Correct 96 ms 16748 KB Output is correct
20 Correct 101 ms 16856 KB Output is correct
21 Correct 100 ms 16788 KB Output is correct
22 Correct 112 ms 16848 KB Output is correct
23 Correct 76 ms 13436 KB Output is correct
24 Correct 80 ms 13556 KB Output is correct
25 Correct 82 ms 14228 KB Output is correct
26 Correct 107 ms 14280 KB Output is correct
27 Correct 96 ms 15284 KB Output is correct
28 Correct 119 ms 15332 KB Output is correct
29 Correct 90 ms 15380 KB Output is correct
30 Correct 124 ms 15300 KB Output is correct
31 Correct 77 ms 13704 KB Output is correct
32 Correct 96 ms 13676 KB Output is correct
33 Correct 99 ms 14316 KB Output is correct
34 Correct 87 ms 14324 KB Output is correct
35 Correct 96 ms 14988 KB Output is correct
36 Correct 118 ms 15076 KB Output is correct
37 Correct 113 ms 15196 KB Output is correct
38 Correct 100 ms 15196 KB Output is correct
39 Correct 96 ms 15084 KB Output is correct
40 Correct 103 ms 15192 KB Output is correct
41 Correct 113 ms 15988 KB Output is correct
42 Correct 95 ms 15964 KB Output is correct
43 Correct 102 ms 15964 KB Output is correct
44 Correct 93 ms 16128 KB Output is correct
45 Correct 119 ms 15852 KB Output is correct
46 Correct 107 ms 15968 KB Output is correct
47 Correct 102 ms 14956 KB Output is correct
48 Correct 103 ms 14976 KB Output is correct
49 Correct 107 ms 14828 KB Output is correct
50 Correct 131 ms 14964 KB Output is correct
51 Correct 102 ms 13916 KB Output is correct
52 Correct 96 ms 13916 KB Output is correct
53 Correct 103 ms 13920 KB Output is correct
54 Correct 84 ms 13980 KB Output is correct
55 Correct 82 ms 13824 KB Output is correct
56 Correct 79 ms 13928 KB Output is correct
57 Correct 89 ms 13836 KB Output is correct
58 Correct 96 ms 13724 KB Output is correct
59 Correct 77 ms 13980 KB Output is correct
60 Correct 88 ms 13952 KB Output is correct