QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#182558#4896. Alice、Bob 与 DFShos_lyric#0 1ms3988kbC++144.5kb2023-09-18 07:55:532024-07-04 02:01:53

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 02:01:53]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3988kb
  • [2023-09-18 07:55:53]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


int N;
vector<int> C;
vector<int> deg;
vector<vector<int>> graph;
int K;
vector<int> R;


namespace brute {
// state: just determined to call dfs(stack.back().first)
map<vector<pair<int, int>>, int> cache;
int calc(const vector<pair<int, int>> &stack) {
  auto it = cache.find(stack);
  if (it != cache.end()) return it->second;
  
  set<int> app;
  for (auto ujs = stack; ; ujs.pop_back()) {
    if (ujs.empty()) {
      app.insert(0);
      goto done;
    }
    const int u = ujs.back().first;
    for (int &j = ujs.back().second; ++j < deg[u]; ) {
      auto vjs = ujs;
      vjs.emplace_back(graph[u][j], -1);
      app.insert(calc(vjs));
      if (!C[u]) {
        goto done;
      }
    }
  }
 done:{}
  int ret;
  for (ret = 0; app.count(ret); ++ret) {}
#ifdef LOCAL
cerr<<"calc "<<stack<<" = "<<ret<<endl;
#endif
  
  return cache[stack] = ret;
}
int run() {
  cache.clear();
  int ans = 0;
  for (const int r : R) {
    ans ^= calc({make_pair(r, -1)});
  }
  return ans;
}
}  // brute


namespace sub4 {
vector<int> dp;
set<int> us;
vector<int> freq;
priority_queue<int, vector<int>, greater<int>> que0, que1;
void add(int u) {
  us.insert(u);
  if (!freq[dp[u]]++) {
    que1.push(dp[u]);
  }
}
void rem(int u) {
  assert(us.erase(u));
  if (!--freq[dp[u]]) {
    que0.push(dp[u]);
  }
}
int get() {
  for (; !que1.empty() && que0.top() == que1.top(); que0.pop(), que1.pop()) {}
  return que0.top();
}

void dfs(int u) {
  for (int j = deg[u]; --j >= 0; ) {
    dfs(graph[u][j]);
  }
  dp[u] = get();
// cerr<<u<<": "<<dp[u]<<"; ";pv(us.begin(),us.end());
  for (const int v : graph[u]) {
    rem(v);
  }
  add(u);
}
int run() {
  vector<int> par(N, -1);
  for (int u = 0; u < N; ++u) {
    for (const int v : graph[u]) {
      par[v] = u;
    }
  }
  freq.assign(N + 1, 0);
  que0 = {};
  que1 = {};
  for (int x = 0; x <= N + 1; ++x) {
    que0.push(x);
  }
  dp.assign(N + 1, 0);
  for (int u = 0; u < N; ++u) if (!~par[u]) {
    add(N);
    dfs(u);
    for (const int v : us) {
      rem(v);
    }
  }
  int ans = 0;
  for (const int r : R) {
    ans ^= dp[r];
  }
  return ans;
}
}  // sub4


int main() {
  for (; ~scanf("%d", &N); ) {
    C.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%d", &C[u]);
    }
    deg.resize(N);
    graph.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%d", &deg[u]);
      graph[u].resize(deg[u]);
      for (int j = 0; j < deg[u]; ++j) {
        scanf("%d", &graph[u][j]);
        --graph[u][j];
      }
    }
    scanf("%d", &K);
    R.resize(K);
    for (int k = 0; k < K; ++k) {
      scanf("%d", &R[k]);
      --R[k];
    }
    
    vector<int> indeg(N, 0);
    for (int u = 0; u < N; ++u) {
      for (const int v : graph[u]) {
        ++indeg[v];
      }
    }
    bool spe4 = true;
    spe4 = spe4 && (C == vector<int>(N, 1));
    for (int u = 0; u < N; ++u) {
      spe4 = spe4 && (indeg[u] <= 1);
    }
    for (const int r : R) {
      spe4 = spe4 && (indeg[r] <= 0);
    }
    
    int ans = -1;
    if (spe4) {
cerr<<"sub4"<<endl;
      ans = sub4::run();
    } else {
      ans = brute::run();
    }
    puts(ans ? "Alice" : "Bob");
#ifdef LOCAL
brute::run();
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Memory Limit Exceeded

Test #1:

score: 0
Memory Limit Exceeded

input:

1000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #18:

score: 15
Accepted
time: 1ms
memory: 3692kb

input:

7
0 0 1 1 0 1 1
1 2
2 3 4
0
2 5 6
0
1 7
0
1
1

output:

Bob

result:

ok "Bob"

Test #19:

score: 0
Accepted
time: 1ms
memory: 3752kb

input:

6
0 1 0 0 1 0
2 2 6
3 3 4 5
0
0
0
0
1
1

output:

Bob

result:

ok "Bob"

Test #20:

score: 0
Accepted
time: 1ms
memory: 3752kb

input:

3
0 1 0
1 2
1 3
0
1
1

output:

Bob

result:

ok "Bob"

Test #21:

score: -15
Runtime Error

input:

10
1 1 1 1 1 1 1 1 1 1
1 2
4 3 5 7 8
1 4
0
1 6
0
0
1 9
1 10
0
1
1

output:


result:


Subtask #3:

score: 0
Runtime Error

Test #55:

score: 15
Accepted
time: 1ms
memory: 3752kb

input:

7
0 0 1 1 0 1 1
1 2
2 3 4
0
2 5 6
0
1 7
0
1
1

output:

Bob

result:

ok "Bob"

Test #56:

score: 0
Accepted
time: 0ms
memory: 3988kb

input:

6
0 1 0 0 1 0
2 2 6
3 3 4 5
0
0
0
0
1
1

output:

Bob

result:

ok "Bob"

Test #57:

score: 0
Accepted
time: 1ms
memory: 3704kb

input:

3
0 1 0
1 2
1 3
0
1
1

output:

Bob

result:

ok "Bob"

Test #58:

score: -15
Runtime Error

input:

10
1 1 1 1 1 1 1 1 1 1
1 2
4 3 5 7 8
1 4
0
1 6
0
0
1 9
1 10
0
1
1

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #103:

score: 0
Runtime Error

input:

10
1 1 1 1 1 1 1 1 1 1
1 2
4 3 5 7 8
1 4
0
1 6
0
0
1 9
1 10
0
1
1

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%