QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417352 | #8720. BFS 序 0 | light_ink_dots# | WA | 110ms | 105876kb | C++14 | 3.1kb | 2024-05-22 17:52:42 | 2024-05-22 17:52:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
static const int maxn = 500010, maxlg = 20;
int n;
scanf("%d", &n);
static int fa[maxn];
static vector<int> G[maxn];
for (int i = 2; i <= n; i++) scanf("%d", fa + i), G[fa[i]].push_back(i);
static int dfn[maxn], dep[maxn];
static int lg[maxn], st[maxlg][maxn];
static auto get = [](int u, int v) { return dep[u] < dep[v] ? u : v; };
static vector<int> vec[maxn];
function<void(int)> dfs = [&dfs](int u) {
static int c;
st[0][dfn[u] = ++c] = fa[u];
for (int i : G[u]) dep[i] = dep[u] + 1, dfs(i), vec[u].push_back(dfn[i]);
return;
};
dfs(1);
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int l = 1; l <= lg[n]; l++)
for (int i = 1; i + (1 << l) - 1 <= n; i++)
st[l][i] = get(st[l - 1][i], st[l - 1][i + (1 << (l - 1))]);
auto lca = [](int u, int v) {
if (u == v)
return u;
u = dfn[u], v = dfn[v];
if (u > v)
swap(u, v);
int Lg = lg[v - (++u) + 1];
return get(st[Lg][u], st[Lg][v - (1 << Lg) + 1]);
};
int q;
scanf("%d", &q);
while (q--) {
int m;
bool ans = true;
scanf("%d", &m);
static int a[maxn];
for (int i = 1; i <= m; i++) scanf("%d", a + i);
static bool vis[maxn];
static vector<int> all;
static vector<pair<int, int>> G[maxn];
for (int i = 1; i + 1 <= m; i++) {
if (dep[a[i]] != dep[a[i + 1]]) {
ans &= dep[a[i]] < dep[a[i + 1]];
continue;
}
int l = lca(a[i], a[i + 1]);
if (!vis[l])
vis[l] = true, all.push_back(l);
int u = *prev(upper_bound(vec[l].begin(), vec[l].end(), dfn[a[i]]));
int v = *prev(upper_bound(vec[l].begin(), vec[l].end(), dfn[a[i + 1]]));
G[l].emplace_back(u, v);
}
for (int u : all) {
vector<int> cur;
static vector<int> g[maxn];
static bool vis[maxn];
static int deg[maxn];
for (auto e : G[u]) {
if (!vis[e.first])
vis[e.first] = true, cur.push_back(e.first);
if (!vis[e.second])
vis[e.second] = true, cur.push_back(e.second);
g[e.first].push_back(e.second), deg[e.second]++;
}
int tot = 0;
queue<int> q;
for (int i : cur)
if (!deg[i])
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop(), tot++;
for (int i : g[u])
if (!(--deg[i]))
q.push(i);
}
ans &= tot == cur.size();
for (int i : cur) vis[i] = false, deg[i] = 0, g[i].clear();
G[u].clear(), vis[u] = false;
}
printf(ans ? "Yes\n" : "No\n");
all.clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 58852kb
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: 110ms
memory: 105876kb
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 Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes No Yes Yes No Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No Yes Yes No Yes Yes No No Yes Yes Yes Yes Yes Yes Yes Yes No Yes Yes...
result:
wrong answer expected NO, found YES [5th token]