QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#418999 | #8720. BFS 序 0 | oO_Oo | WA | 66ms | 66820kb | C++14 | 5.5kb | 2024-05-23 16:54:12 | 2024-05-23 16:54:13 |
Judging History
answer
#include <bits/stdc++.h>
// #include <ext/pb_ds/hash_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize(1)
// #pragma GCC optimize(2)
// #pragma GCC optimize(3,"Ofast","inline")
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define lowbit(x) ((x) & -(x))
#define all(x) x.begin(), x.end()
#define set_all(x, y) memset(x, y, sizeof (x))
// #define endl '\n'
// #define int long long
#define ff first
#define ss second
#define pb push_back
#define typet typename T
#define typeu typename U
#define types typename... Ts
#define tempt template <typet>
#define tempu template <typeu>
#define temps template <types>
#define tandu template <typet, typeu>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) do {} while (false)
#endif
using namespace std;
// using namespace __gnu_pbds;
using ULL = unsigned long long;
using LL = long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PIB = pair<int, bool>;
using PDD = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<LL>;
using vvl = vector<vl>;
using vpi = vector<PII>;
// typedef __int128_t i128;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return x;
}
size_t operator () (uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = 300010, M = 2 * N, K = 31, Mod = 1e9 + 7;
const double pi = acos(-1), eps = 1e-7;
const ULL P = 13331;
struct node {
int l, r;
int val;
}tr[N << 2];
void pushup(int u) {
tr[u].val = min(tr[u << 1].val, tr[u << 1 | 1].val);
}
void build(int u, int l, int r) {
if (l == r) {
tr[u] = {l, r, INF};
return;
}
tr[u] = {l, r, INF};
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u].val;
int mid = tr[u].l + tr[u].r >> 1;
int res = INF;
if (l <= mid) res = min(res, query(u << 1, l, r));
if (r > mid) res = min(res, query(u << 1 | 1, l, r));
return res;
}
void modify(int u, int x, int d) {
if (tr[u].l == tr[u].r) {
tr[u].val = d;
} else {
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, d);
else modify(u << 1 | 1, x, d);
pushup(u);
}
}
void solut(int I) {
int n;
cin >> n;
vector<vector<int>> g(n + 1);
for (int i = 2; i <= n; i ++) {
int x;
cin >> x;
g[x].push_back(i);
}
vector<int> sz(n + 1);
vector<int> d(n + 1);
vector<int> dfn(n + 1);
int tot = 0;
vector<vector<int>> fa(n + 1, vector<int>(20));
auto dfs = [&](auto&& self, int u, int f) -> void{
dfn[u] = ++ tot;
sz[u] = 1;
fa[u][0] = f;
for (int i = 1; i < 20; i ++) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (auto v: g[u]) {
if (v == f) continue;
d[v] = d[u] + 1;
self(self, v, u);
sz[u] += sz[v];
}
};
d[1] = 1;
dfs(dfs, 1, 1);
auto lca = [&](int u, int v) -> PII {
if (d[u] < d[v]) {
assert(false);
swap(u, v);
}
for (int i = 19; i >= 0; i --) {
if (d[fa[u][i]] >= d[v]) u = fa[u][i];
}
if (u == v) return {u, -1};
for (int i = 19; i >= 0; i --) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return {u, v};
};
build(1, 1, n);
int q;
cin >> q;
vector<int> xixi(n + 1);
vector<int> ceng(n + 1);
while (q --) {
bool check = true;
int m;
cin >> m;
vector<int> b(m + 1);
vector<int> temp;
for (int i = 1; i <= m; i ++) {
cin >> b[i];
}
// debug(1);
// return;
int prev = 0;
for (int i = 1; i <= m; i ++) {
int j = i + 1;
if (d[b[i]] < prev) {
check = false;
goto label;
}
while (j <= m && d[b[j]] == d[b[i]]) {
auto [x, y] = lca(b[j - 1], b[j]);
// debug(x, y);
if (xixi[x] == 0) {
xixi[x] = ++ ceng[d[x]];
temp.push_back(x);
}
if (xixi[y] == 0) {
xixi[y] = ++ ceng[d[y]];
temp.push_back(y);
}
if (xixi[x] > xixi[y]) {
check = false;
goto label;
}
j ++;
}
prev = d[b[i]];
i = j - 1;
}
label:
for (int i: temp) {
xixi[i] = 0;
ceng[d[i]] = 0;
}
if (check) cout << "Yes" << '\n';
else cout << "No" <<'\n';
}
}
/*
6
1 1 3 2 4
10
4 3 6 2 5
1 4
3 2 4 5
5 2 5 4 6 3
3 1 4 2
3 5 6 3
5 4 5 2 6 1
4 4 3 2 5
4 4 6 2 3
3 3 2 6
*/
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T;
T = 1;
// cin >> T;
for (int _ = 1; _ <= T; _ ++) {
solut(_);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
6 1 1 3 2 4 10 4 3 6 2 5 1 4 3 2 4 5 5 2 5 4 6 3 3 1 4 2 3 5 6 3 5 4 5 2 6 1 4 4 3 2 5 4 4 6 2 3 3 3 2 6
output:
No Yes Yes No No No No No No Yes
result:
ok 10 token(s): yes count is 3, no count is 7
Test #2:
score: -100
Wrong Answer
time: 66ms
memory: 66820kb
input:
300000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
No No No No No No No No No No Yes Yes No Yes No Yes Yes Yes No No No No Yes No No Yes No No No No Yes Yes No No No Yes No No No No No No No Yes No Yes No Yes No No No No No Yes Yes No No Yes No No No No No Yes Yes No No No No No No No No Yes No No No Yes No No Yes No No Yes No Yes No No No Yes No No...
result:
wrong answer expected YES, found NO [2nd token]