Submission #1633033


Source Code Expand

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

int N;
int H , W;
vector<int> A;
vector<vector<int>> MAP;
int G[20][20];
int visited[20];
vector<int> g;

int dfs(vector<int> &g, int v) {
  if(visited[v] == false) {
    visited[v] = true;
    for(int i = 0; i < N; i++) {
      if(G[v][i] < 0) dfs(g, i);
    }
    g.push_back(v);
  }
}

map<pair<int, int>,bool> dp[20][20];
int solve(int no, int bits, int same, int pos) {
  auto p = make_pair(no, bits);
  if(dp[no][pos].count(p)) return dp[no][pos][p];
  bool same_color = no < N - 1 && A[no] == A[no + 1];
  if(no == N) return 1;
  if(pos == N) return 0; // 失敗
  int ret = solve(no, bits, same, pos + 1);
  if(bits >> pos & 1) return dp[no][pos][p] = ret;
  for(int i = 0; i < N; i++) {
    if(G[pos][i] < 0 && bits >> i & 1) return dp[no][pos][p]= 0; // 先が塗られているなら無理
    if(G[i][pos] < 0 && same >> i & 1) return dp[no][pos][p]= ret; // 同じ色で塗られていたらもう塗れない
  }
  ret |= solve(no + 1, bits | (1 << pos), same_color ? (same | (1 << pos)) : 0, same_color ? pos + 1: 0);
  return dp[no][pos][p]=ret;
}
int main() {
  cin >> N;
  A=vector<int>(N);
  for(int&a:A)cin>>a;
  sort(A.begin(), A.end());
  cin >> H >> W;
  MAP=vector<vector<int>>(H, vector<int>(W));
  for(int y = 0; y < H; y++) {
    for(int x = 0; x < W; x++) {
      cin>>MAP[y][x];
    }
  }
  for(int i = 0; i < N; i++) {
    fill(G[i], G[i]+N, 99999999);
    G[i][i] = 0;
  }
  for(int i = 0; i < N; i++) {
    int min_x = 2000, max_x = -1;
    int min_y = 2000, max_y = -1;
    for(int y = 0; y < H; y++) {
      for(int x = 0; x < W; x++) {
        if(MAP[y][x] == i) {
          min_x = min(min_x, x);
          min_y = min(min_y, y);
          max_x = max(max_x, x);
          max_y = max(max_y, y);
        }
      }
    }
    for(int y = min_y; y <= max_y; y++) {
      for(int x = min_x; x <= max_x; x++) {
        if(MAP[y][x] != i) {
          G[i][MAP[y][x]] = -1;
        }
      }
    }
  }
  for(int k = 0; k < N; k++)
    for(int i = 0; i < N; i++)
      for(int j = 0;j < N; j++){
        G[i][j] = min(G[i][j], G[i][k]+ G[k][j]);
      }
  for(int i = 0; i < N; i++) {
    if(G[i][i] < 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for(int i = 0; i < N; i++) {
    dfs(g, i);
  }
  reverse(g.begin(), g.end());
  cout << solve(0, 0, 0, 0) << endl;
}

Submission Info

Submission Time
Task D - 天下一芸術
User staka
Language C++14 (GCC 5.4.1)
Score 110
Code Size 2496 Byte
Status AC
Exec Time 1690 ms
Memory 139648 KB

Judge Result

Set Name small medium All
Score / Max Score 35 / 35 40 / 40 35 / 35
Status
AC × 34
AC × 66
AC × 135
Set Name Test Cases
small 00_sample0.txt, 00_sample1.txt, 00_sample2.txt, 00_sample3.txt, 01_manual1.txt, 01_manual2.txt, 01_manual3.txt, 01_manual4.txt, 01_manual5.txt, 01_manual6.txt, 02_small100.txt, 02_small101.txt, 02_small102.txt, 02_small103.txt, 02_small104.txt, 02_small105.txt, 02_small106.txt, 02_small107.txt, 02_small108.txt, 02_small109.txt, 02_small110.txt, 02_small111.txt, 02_small112.txt, 02_small113.txt, 02_small114.txt, 02_small115.txt, 02_small116.txt, 02_small117.txt, 02_small118.txt, 02_small119.txt, 02_small120.txt, 03_small_b1.txt, 03_small_b2.txt, 03_small_b3.txt
medium 00_sample0.txt, 00_sample1.txt, 00_sample2.txt, 00_sample3.txt, 01_manual1.txt, 01_manual2.txt, 01_manual3.txt, 01_manual4.txt, 01_manual5.txt, 01_manual6.txt, 02_small100.txt, 02_small101.txt, 02_small102.txt, 02_small103.txt, 02_small104.txt, 02_small105.txt, 02_small106.txt, 02_small107.txt, 02_small108.txt, 02_small109.txt, 02_small110.txt, 02_small111.txt, 02_small112.txt, 02_small113.txt, 02_small114.txt, 02_small115.txt, 02_small116.txt, 02_small117.txt, 02_small118.txt, 02_small119.txt, 02_small120.txt, 03_small_b1.txt, 03_small_b2.txt, 03_small_b3.txt, 11_manual0.txt, 11_manual1.txt, 12_medium100.txt, 12_medium101.txt, 12_medium102.txt, 12_medium103.txt, 12_medium104.txt, 12_medium105.txt, 12_medium106.txt, 12_medium107.txt, 12_medium108.txt, 12_medium109.txt, 12_medium110.txt, 12_medium111.txt, 12_medium112.txt, 12_medium113.txt, 12_medium114.txt, 12_medium115.txt, 12_medium116.txt, 12_medium117.txt, 12_medium118.txt, 12_medium119.txt, 12_medium120.txt, 12_medium121.txt, 12_medium122.txt, 12_medium123.txt, 12_medium124.txt, 12_medium125.txt, 12_medium126.txt, 12_medium127.txt, 12_medium128.txt, 12_medium129.txt
All 00_sample0.txt, 00_sample1.txt, 00_sample2.txt, 00_sample3.txt, 01_manual1.txt, 01_manual2.txt, 01_manual3.txt, 01_manual4.txt, 01_manual5.txt, 01_manual6.txt, 02_small100.txt, 02_small101.txt, 02_small102.txt, 02_small103.txt, 02_small104.txt, 02_small105.txt, 02_small106.txt, 02_small107.txt, 02_small108.txt, 02_small109.txt, 02_small110.txt, 02_small111.txt, 02_small112.txt, 02_small113.txt, 02_small114.txt, 02_small115.txt, 02_small116.txt, 02_small117.txt, 02_small118.txt, 02_small119.txt, 02_small120.txt, 03_small_b1.txt, 03_small_b2.txt, 03_small_b3.txt, 11_manual0.txt, 11_manual1.txt, 12_medium100.txt, 12_medium101.txt, 12_medium102.txt, 12_medium103.txt, 12_medium104.txt, 12_medium105.txt, 12_medium106.txt, 12_medium107.txt, 12_medium108.txt, 12_medium109.txt, 12_medium110.txt, 12_medium111.txt, 12_medium112.txt, 12_medium113.txt, 12_medium114.txt, 12_medium115.txt, 12_medium116.txt, 12_medium117.txt, 12_medium118.txt, 12_medium119.txt, 12_medium120.txt, 12_medium121.txt, 12_medium122.txt, 12_medium123.txt, 12_medium124.txt, 12_medium125.txt, 12_medium126.txt, 12_medium127.txt, 12_medium128.txt, 12_medium129.txt, 21_manual0.txt, 21_manual1.txt, 21_manual10.txt, 21_manual11.txt, 21_manual12.txt, 21_manual13.txt, 21_manual2.txt, 21_manual3.txt, 21_manual4.txt, 21_manual5.txt, 21_manual6.txt, 21_manual7.txt, 21_manual8.txt, 21_manual9.txt, 21_manual_L0.txt, 21_manual_L1.txt, 21_manual_L2.txt, 21_manual_L3.txt, 21_manual_L4.txt, 21_manual_L5.txt, 21_manual_L6.txt, 21_manual_L7.txt, 21_manual_L8.txt, 22_hard100.txt, 22_hard101.txt, 22_hard1011.txt, 22_hard102.txt, 22_hard103.txt, 22_hard104.txt, 22_hard105.txt, 22_hard106.txt, 22_hard107.txt, 22_hard108.txt, 22_hard109.txt, 22_hard110.txt, 22_hard111.txt, 22_hard112.txt, 22_hard113.txt, 22_hard114.txt, 22_hard1140.txt, 22_hard115.txt, 22_hard116.txt, 22_hard117.txt, 22_hard118.txt, 22_hard119.txt, 22_hard120.txt, 22_hard121.txt, 22_hard122.txt, 22_hard123.txt, 22_hard124.txt, 22_hard125.txt, 22_hard126.txt, 22_hard127.txt, 22_hard128.txt, 22_hard129.txt, 22_hard130.txt, 22_hard131.txt, 22_hard132.txt, 22_hard133.txt, 22_hard134.txt, 22_hard135.txt, 22_hard154.txt, 22_hard2096.txt, 22_hard275.txt, 22_hard345.txt, 22_hard351.txt, 22_hard437.txt, 22_hard442.txt, 22_hard524.txt
Case Name Status Exec Time Memory
00_sample0.txt AC 1 ms 256 KB
00_sample1.txt AC 1 ms 256 KB
00_sample2.txt AC 1 ms 256 KB
00_sample3.txt AC 1 ms 256 KB
01_manual1.txt AC 1 ms 256 KB
01_manual2.txt AC 1 ms 256 KB
01_manual3.txt AC 1 ms 256 KB
01_manual4.txt AC 1 ms 256 KB
01_manual5.txt AC 1 ms 256 KB
01_manual6.txt AC 1 ms 256 KB
02_small100.txt AC 1 ms 256 KB
02_small101.txt AC 1 ms 256 KB
02_small102.txt AC 1 ms 256 KB
02_small103.txt AC 1 ms 256 KB
02_small104.txt AC 1 ms 256 KB
02_small105.txt AC 1 ms 256 KB
02_small106.txt AC 1 ms 256 KB
02_small107.txt AC 1 ms 256 KB
02_small108.txt AC 1 ms 256 KB
02_small109.txt AC 1 ms 256 KB
02_small110.txt AC 1 ms 256 KB
02_small111.txt AC 1 ms 256 KB
02_small112.txt AC 1 ms 256 KB
02_small113.txt AC 1 ms 256 KB
02_small114.txt AC 1 ms 256 KB
02_small115.txt AC 1 ms 256 KB
02_small116.txt AC 1 ms 256 KB
02_small117.txt AC 1 ms 256 KB
02_small118.txt AC 1 ms 256 KB
02_small119.txt AC 1 ms 256 KB
02_small120.txt AC 1 ms 256 KB
03_small_b1.txt AC 1 ms 256 KB
03_small_b2.txt AC 1 ms 256 KB
03_small_b3.txt AC 1 ms 256 KB
11_manual0.txt AC 2 ms 384 KB
11_manual1.txt AC 9 ms 768 KB
12_medium100.txt AC 5 ms 384 KB
12_medium101.txt AC 6 ms 384 KB
12_medium102.txt AC 8 ms 384 KB
12_medium103.txt AC 8 ms 384 KB
12_medium104.txt AC 8 ms 384 KB
12_medium105.txt AC 8 ms 384 KB
12_medium106.txt AC 9 ms 640 KB
12_medium107.txt AC 8 ms 384 KB
12_medium108.txt AC 2 ms 512 KB
12_medium109.txt AC 2 ms 512 KB
12_medium110.txt AC 9 ms 768 KB
12_medium111.txt AC 9 ms 768 KB
12_medium112.txt AC 8 ms 384 KB
12_medium113.txt AC 8 ms 384 KB
12_medium114.txt AC 4 ms 384 KB
12_medium115.txt AC 8 ms 384 KB
12_medium116.txt AC 9 ms 640 KB
12_medium117.txt AC 2 ms 256 KB
12_medium118.txt AC 1 ms 256 KB
12_medium119.txt AC 2 ms 256 KB
12_medium120.txt AC 6 ms 384 KB
12_medium121.txt AC 8 ms 384 KB
12_medium122.txt AC 7 ms 384 KB
12_medium123.txt AC 9 ms 768 KB
12_medium124.txt AC 8 ms 384 KB
12_medium125.txt AC 8 ms 512 KB
12_medium126.txt AC 2 ms 384 KB
12_medium127.txt AC 1 ms 256 KB
12_medium128.txt AC 9 ms 768 KB
12_medium129.txt AC 6 ms 384 KB
21_manual0.txt AC 46 ms 8448 KB
21_manual1.txt AC 1223 ms 139520 KB
21_manual10.txt AC 800 ms 91648 KB
21_manual11.txt AC 165 ms 24576 KB
21_manual12.txt AC 94 ms 15616 KB
21_manual13.txt AC 962 ms 101248 KB
21_manual2.txt AC 49 ms 8448 KB
21_manual3.txt AC 1156 ms 139520 KB
21_manual4.txt AC 45 ms 8448 KB
21_manual5.txt AC 1229 ms 139520 KB
21_manual6.txt AC 327 ms 42752 KB
21_manual7.txt AC 520 ms 61056 KB
21_manual8.txt AC 789 ms 93952 KB
21_manual9.txt AC 725 ms 85504 KB
21_manual_L0.txt AC 1154 ms 86144 KB
21_manual_L1.txt AC 1340 ms 115584 KB
21_manual_L2.txt AC 1358 ms 89600 KB
21_manual_L3.txt AC 1320 ms 112512 KB
21_manual_L4.txt AC 871 ms 79360 KB
21_manual_L5.txt AC 9 ms 384 KB
21_manual_L6.txt AC 9 ms 384 KB
21_manual_L7.txt AC 9 ms 384 KB
21_manual_L8.txt AC 9 ms 384 KB
22_hard100.txt AC 6 ms 384 KB
22_hard101.txt AC 6 ms 384 KB
22_hard1011.txt AC 1653 ms 134144 KB
22_hard102.txt AC 1318 ms 108160 KB
22_hard103.txt AC 9 ms 384 KB
22_hard104.txt AC 8 ms 384 KB
22_hard105.txt AC 9 ms 640 KB
22_hard106.txt AC 452 ms 37888 KB
22_hard107.txt AC 9 ms 384 KB
22_hard108.txt AC 1 ms 256 KB
22_hard109.txt AC 395 ms 55552 KB
22_hard110.txt AC 1509 ms 133888 KB
22_hard111.txt AC 1506 ms 139648 KB
22_hard112.txt AC 14 ms 1792 KB
22_hard113.txt AC 8 ms 384 KB
22_hard114.txt AC 8 ms 384 KB
22_hard1140.txt AC 1124 ms 138368 KB
22_hard115.txt AC 1524 ms 135168 KB
22_hard116.txt AC 1171 ms 139520 KB
22_hard117.txt AC 1 ms 256 KB
22_hard118.txt AC 1298 ms 139648 KB
22_hard119.txt AC 53 ms 8576 KB
22_hard120.txt AC 1491 ms 134912 KB
22_hard121.txt AC 19 ms 2816 KB
22_hard122.txt AC 1124 ms 137344 KB
22_hard123.txt AC 4 ms 384 KB
22_hard124.txt AC 35 ms 6656 KB
22_hard125.txt AC 1209 ms 139520 KB
22_hard126.txt AC 1 ms 256 KB
22_hard127.txt AC 675 ms 65920 KB
22_hard128.txt AC 1690 ms 139648 KB
22_hard129.txt AC 9 ms 384 KB
22_hard130.txt AC 8 ms 384 KB
22_hard131.txt AC 1500 ms 139136 KB
22_hard132.txt AC 1207 ms 138240 KB
22_hard133.txt AC 1148 ms 139520 KB
22_hard134.txt AC 1474 ms 139648 KB
22_hard135.txt AC 1447 ms 139648 KB
22_hard154.txt AC 868 ms 80768 KB
22_hard2096.txt AC 1171 ms 139520 KB
22_hard275.txt AC 1306 ms 139648 KB
22_hard345.txt AC 1178 ms 139520 KB
22_hard351.txt AC 1084 ms 138368 KB
22_hard437.txt AC 1427 ms 138880 KB
22_hard442.txt AC 9 ms 640 KB
22_hard524.txt AC 1033 ms 92800 KB