QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#808571 | #8551. DFS Order 5 | TJUHuangTao | WA | 51ms | 32252kb | C++20 | 3.7kb | 2024-12-10 21:36:12 | 2024-12-10 21:36:13 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ll long long
using namespace std;
const int maxn = 2e5 + 10;
vector<int> G[maxn];
int siz[maxn], fa[maxn], L[maxn], R[maxn], ncnt;
struct RMQlca {
int dfn[maxn << 1], tot, st[23][maxn << 1], pos[maxn], rev[23][maxn << 1],
dep[maxn << 1], lg[maxn << 1];
void dfs(int u, int deep) {
dfn[++tot] = u, dep[tot] = deep, pos[u] = tot;
L[u] = ++ncnt;
siz[u] = 1;
for (auto v : G[u]) {
if (!pos[v]) {
fa[v] = u;
dfs(v, deep + 1);
siz[u] += siz[v];
dfn[++tot] = u, dep[tot] = deep;
}
}
R[u] = ncnt;
}
void init() {
for (int i = 2; i <= tot + 1; i++)
lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= tot; i++)
st[0][i] = dep[i], rev[0][i] = dfn[i];
for (int i = 1; i <= lg[tot]; i++)
for (int j = 1; j + (1 << i) - 1 <= tot; j++)
if (st[i - 1][j] < st[i - 1][j + (1 << i - 1)])
st[i][j] = st[i - 1][j], rev[i][j] = rev[i - 1][j];
else
st[i][j] = st[i - 1][j + (1 << i - 1)],
rev[i][j] = rev[i - 1][j + (1 << i - 1)];
}
int LCA2(int l, int r) {
if (pos[l] > pos[r])
swap(l, r);
l = pos[l], r = pos[r];
int k = lg[r - l + 1];
return st[k][l] < st[k][r + l - (1 << k)] ? rev[k][l]
: rev[k][r + 1 - (1 << k)];
}
} rmqlca;
int LCA(int u, int v) {
return rmqlca.LCA2(u, v);
}
struct stable {
vector<vector<int>> mylist;
int n;
int (*op)(int a, int b);
stable(vector<int>& a, int (*f)(int, int)) {
op = f;
n = a.size() - 1;
mylist = vector<vector<int>>(n + 1, vector<int>(__lg(n) + 1));
for (int i = 1; i <= n; i++)
mylist[i][0] = a[i];
for (int j = 1; j <= __lg(n); j++)
for (int i = 1; i <= n - (1 << j) + 1; i++)
mylist[i][j] =
op(mylist[i][j - 1], mylist[i + (1 << j - 1)][j - 1]);
}
int q(int l, int r) {
assert(l >= 1 && r <= n && l <= r);
int len = __lg(r - l + 1);
return op(mylist[l][len], mylist[r - (1 << len) + 1][len]);
}
};
void solve() {
ncnt = 0;
int n, q;
cin >> n >> q;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
rmqlca.dfs(1, 0);
rmqlca.init();
while (q--) {
int k;
cin >> k;
set<int> s;
vector<int> arr(k + 1);
for (int i = 1; i <= k; i++)
cin >> arr[i], s.insert(arr[i]);
if (s.size() != k) {
cout << "No\n";
continue;
}
s.clear();
stable st(arr, LCA);
int flag = 1;
for (int i = 1; i < k && flag; i++)
flag &= LCA(arr[i], arr[i + 1]) == fa[arr[i + 1]];
for (int i = 1; i <= k && flag; i++) {
flag &= arr[i] == st.q(i, min(k, i + siz[arr[i]] - 1));
}
for (int i = 1; i <= k && flag; i++) {
if (s.lower_bound(L[arr[i]]) != s.end() &&
*s.lower_bound(L[arr[i]]) <= R[arr[i]])
flag = 0;
s.insert(L[arr[i]]);
}
if (flag)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 26176kb
input:
6 7 1 2 1 3 2 4 3 5 2 6 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 2 4 6 6 1 2 6 4 3 5
output:
No No Yes No No Yes Yes
result:
ok 7 tokens
Test #2:
score: -100
Wrong Answer
time: 51ms
memory: 32252kb
input:
10 100000 7 2 1 7 7 10 8 6 8 7 1 3 4 5 9 5 5 8 8 8 9 7 2 8 1 6 1 4 8 3 5 2 6 7 10 3 9 9 1 1 1 8 10 3 2 9 3 8 7 3 7 5 6 2 8 5 9 1 6 3 4 6 2 1 3 5 8 9 2 4 9 1 3 2 1 5 5 8 5 1 7 9 10 5 2 9 2 6 4 10 6 3 8 3 4 5 8 2 8 4 9 4 10 1 2 4 3 3 6 3 1 3 6 1 1 6 8 3 1 3 7 3 2 3 9 1 5 4 3 7 8 10 9 4 2 3 10 2 5 4 3 ...
output:
No No No Yes No No No No Yes No No No No No No Yes No No No No No No No No No No No No No No No Yes No No No No No No Yes No No No No No Yes Yes No No No No No No No No No No No No No No No No No No No No No No No No Yes No No Yes Yes No No No No Yes No No No No No No No No No No No No No No No No N...
result:
wrong answer 479th words differ - expected: 'Yes', found: 'No'