QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#244300 | #7764. 世界沉睡童话 | hos_lyric# | 15 | 43ms | 11392kb | C++14 | 7.2kb | 2023-11-08 23:00:31 | 2024-07-04 02:23:29 |
Judging History
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 <random>
#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")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
int N, R;
vector<int> A, B;
vector<int> D;
vector<vector<int>> G;
vector<int> J;
vector<Mint> two;
vector<int> invA;
vector<int> par, dep;
vector<int> us;
void dfs(int u) {
us.push_back(u);
for (const int v : G[u]) {
par[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
bool prob1() {
auto bs = B;
for (int j = N; --j >= 1; ) {
const int u = us[j];
if (A[u] != bs[u]) swap(bs[par[u]], bs[u]);
if (A[u] != bs[u]) return false;
}
return true;
}
Mint prob3_brute() {
Mint ans = 0;
int now = N - 1;
vector<int> on(N, 0);
vector<int> fs(N, -1);
for (int u = 0; u < N; ++u) {
// cerr<<"u = "<<u<<endl;
for (int v = u; ~v; v = par[v]) on[v] = 1;
auto check = [&](int phase) -> bool {
int tmp = now;
for (int j0 = D[u] - 1, v = u; ~v; j0 = J[v], v = par[v]) {
for (int j = j0; j >= 0; --j) {
const int w = G[v][j];
// cerr<<" fs = "<<fs<<", phase = "<<phase<<", w = "<<w<<", tmp = "<<tmp<<endl;
if (phase == 0 && A[w] < B[u]) {
if (!~fs[w]) {
// cerr<<" ans += 2^"<<tmp-1<<endl;
ans += two[tmp - 1];
} else if (fs[w] != on[w]) {
// cerr<<" ans += 2^"<<tmp<<endl;
ans += two[tmp];
}
}
if (phase == 1 && A[w] == B[u]) {
if (!~fs[w]) {
--tmp;
fs[w] = on[w] ^ 1;
} else if (fs[w] == on[w]) {
return false;
}
now = tmp;
return true;
}
if (!~fs[w]) {
--tmp;
if (phase == 1) {
fs[w] = on[w];
}
} else if (fs[w] != on[w]) {
return false;
}
}
}
if (phase == 0 && A[R] < B[u]) {
// cerr<<" fs = "<<fs<<", phase = "<<phase<<", R = "<<R<<", tmp = "<<tmp<<endl;
// cerr<<" ans += 2^"<<tmp<<endl;
ans += two[tmp];
}
if (phase == 1 && A[R] == B[u]) {
now = tmp;
return true;
}
return false;
};
check(0);
if (!check(1)) {
break;
}
for (int v = u; ~v; v = par[v]) on[v] = 0;
// cerr<<" fs = "<<fs<<", now = "<<now<<", ans = "<<ans<<endl;
}
return ans;
}
int main() {
for (; ~scanf("%d%d", &N, &R); ) {
--R;
A.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%d", &A[u]);
--A[u];
}
D.resize(N);
G.resize(N);
J.assign(N, -1);
for (int u = 0; u < N; ++u) {
scanf("%d", &D[u]);
G[u].resize(D[u]);
for (int j = 0; j < D[u]; ++j) {
scanf("%d", &G[u][j]);
--G[u][j];
J[G[u][j]] = j;
}
}
B.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%d", &B[u]);
--B[u];
}
two.resize(N + 1);
two[0] = 1;
for (int i = 1; i <= N; ++i) two[i] = two[i - 1] * 2;
invA.assign(N, -1);
for (int u = 0; u < N; ++u) {
invA[A[u]] = u;
}
par.assign(N, -1);
dep.assign(N, 0);
us.clear();
dfs(R);
const int maxD = *max_element(D.begin(), D.end());
const int maxDep = *max_element(dep.begin(), dep.end());
const bool ans1 = prob1();
puts(ans1 ? "YES" : "NO");
puts("no comment");
if(1){// if (maxD <= 20 && maxDep <= 20) {
const Mint ans3 = prob3_brute();
printf("%u\n", ans3.x);
} else {
puts("no comment");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 3804kb
input:
20 10 20 6 18 12 8 14 15 5 1 17 11 10 19 13 7 16 2 9 3 4 3 13 16 20 1 4 1 17 0 1 19 0 0 0 3 5 8 11 3 1 3 18 0 0 2 15 12 3 6 9 2 1 14 0 0 0 1 7 0 19 6 17 12 3 14 13 5 8 9 11 7 10 1 20 16 2 18 15 4
output:
YES no comment 443216
result:
ok Your answer to Question 3 is correct. 100%.
Test #2:
score: 5
Accepted
time: 0ms
memory: 3800kb
input:
20 15 10 16 5 1 14 15 2 8 19 9 6 3 7 12 13 11 4 20 17 18 0 0 1 10 0 0 1 17 0 3 6 20 19 3 8 12 1 1 5 3 18 14 2 2 11 13 0 0 2 9 16 1 3 1 7 1 4 0 0 12 18 9 7 5 16 20 3 17 2 14 15 13 19 11 6 1 10 8 4
output:
NO no comment 458752
result:
ok Your answer to Question 3 is correct. 100%.
Test #3:
score: 5
Accepted
time: 0ms
memory: 4088kb
input:
20 20 7 6 19 4 16 12 1 15 14 10 8 13 9 18 3 17 2 11 5 20 3 4 14 10 0 3 9 2 16 0 1 18 0 0 2 17 5 0 2 6 12 0 1 3 2 11 1 1 8 0 0 0 1 19 1 15 2 13 7 20 19 17 4 18 12 9 16 14 13 8 10 7 15 11 6 2 5 3 1
output:
YES no comment 524287
result:
ok Your answer to Question 3 is correct. 100%.
Subtask #2:
score: 5
Accepted
Test #4:
score: 5
Accepted
time: 24ms
memory: 9632kb
input:
80000 1 77649 27240 19178 10270 1981 6216 42189 63630 66779 68852 46110 27187 4598 75779 69930 43632 68065 13395 33234 17719 76420 65825 3003 67410 18637 3380 32923 66692 9430 1915 118 62287 69323 9735 72936 30221 46487 33367 59048 61582 55572 10657 61645 68649 23643 39197 38086 66512 58864 51881 69...
output:
YES no comment 524240706
result:
ok Your answer to Question 3 is correct. 100%.
Test #5:
score: 5
Accepted
time: 27ms
memory: 9724kb
input:
80000 1 40751 74046 38639 8349 10511 79922 55598 63363 5123 68628 47324 17756 21270 35623 48141 31851 51342 75440 32 64257 20805 21871 72745 39461 76181 72542 12937 23663 69050 49377 74431 74875 66326 28811 65655 37532 53189 6723 56502 67191 76122 35519 22721 36131 31071 4212 11534 64384 71045 33940...
output:
NO no comment 10861120
result:
ok Your answer to Question 3 is correct. 100%.
Test #6:
score: 5
Accepted
time: 22ms
memory: 9720kb
input:
80000 1 52 92 20 169 34 249 69 125 25 131 77 32 55 182 43 150 219 5 15 236 26 155 6 19 39 4 93 23 21 47 318 40 179 328 7 38 95 42 146 49 268 227 61 214 36 148 73 3 10 60 90 33 28 56 220 18 122 46 64 188 44 1 50 396 173 218 561 58 51 79 72 53 17 100 97 66 78 118 8 68 80 81 211 84 99 136 133 205 11 11...
output:
NO no comment 599441604
result:
ok Your answer to Question 3 is correct. 100%.
Subtask #3:
score: 5
Accepted
Dependency #2:
100%
Accepted
Test #7:
score: 5
Accepted
time: 43ms
memory: 9740kb
input:
80000 3823 8672 63816 1855 36775 59031 46214 57526 45575 56480 24946 44377 71695 48249 27546 7294 50921 44846 15666 48938 25415 52920 13826 22835 21341 1142 24320 29132 52868 64771 21558 60343 15685 75914 11345 44134 68985 77283 78587 12672 69815 6415 5331 75886 12703 35068 3032 31409 31937 9552 185...
output:
YES no comment 612179383
result:
ok Your answer to Question 3 is correct. 100%.
Test #8:
score: 5
Accepted
time: 43ms
memory: 9624kb
input:
80000 60188 5794 26969 68934 26583 48201 17389 14742 77469 44368 2137 74498 60703 9179 31494 20743 20484 30465 15763 2175 10298 54198 810 52901 22117 69570 14168 20437 66380 11645 68961 53532 24346 8175 66161 79610 68212 11083 30646 53788 257 6139 33662 24868 65419 14595 38655 32846 48437 5746 7958 ...
output:
NO no comment 196571817
result:
ok Your answer to Question 3 is correct. 100%.
Test #9:
score: 5
Accepted
time: 28ms
memory: 9632kb
input:
80000 79998 1148 7193 27496 1966 51563 50524 12463 55117 63933 49247 60771 74288 46586 10841 55278 15357 76933 58367 3881 42134 41227 41943 36434 22191 61595 51327 67615 76770 25655 17597 49007 19958 22646 43900 8657 46333 46826 45346 39216 23828 50433 15579 72477 4164 43118 71917 18319 23835 40211 ...
output:
YES no comment 291865406
result:
ok Your answer to Question 3 is correct. 100%.
Subtask #4:
score: 0
Time Limit Exceeded
Dependency #2:
100%
Accepted
Test #10:
score: 5
Accepted
time: 20ms
memory: 11392kb
input:
80000 1 23182 57274 19130 76507 5924 29197 57478 51488 74017 47401 32091 39255 79861 41955 26974 60662 2225 70963 4735 74401 52909 69178 36760 4690 44532 40697 73039 16264 67322 64193 8773 62412 3428 53091 14416 64609 25648 38035 58938 30410 9510 44821 74764 18342 70921 74358 36541 4577 75727 28512 ...
output:
NO no comment 295664390
result:
ok Your answer to Question 3 is correct. 100%.
Test #11:
score: 0
Time Limit Exceeded
input:
80000 1 61602 27516 53669 51811 14948 76631 192 16777 38570 5946 6954 12227 19942 68561 44448 64006 37108 15992 41511 40400 74124 37704 11985 29943 7097 41084 19256 29568 37388 56685 22863 77327 58896 48299 22126 51515 68870 30219 42431 18685 19954 27935 44008 75233 1372 26374 13728 29643 15853 1632...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #3:
100%
Accepted
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #4:
0%
Subtask #7:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #5:
0%
Subtask #8:
score: 0
Skipped
Dependency #7:
0%
Subtask #9:
score: 0
Skipped
Dependency #8:
0%
Subtask #10:
score: 0
Skipped
Dependency #9:
0%