QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602643 | #8720. BFS 序 0 | ucup-team902# | WA | 194ms | 51916kb | C++23 | 2.8kb | 2024-10-01 11:28:16 | 2024-10-01 11:28:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> G[400005], G2[400005];
int fa[400005][20];
int dep[400005], b[400005];
int LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 19; ~i; i--)
if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
if (u == v) return v;
for (int i = 19; ~i; i--)
if (fa[u][i] != fa[v][i]) {
u = fa[u][i], v = fa[v][i];
}
return fa[u][0];
}
int dis(int u, int v) {
return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
}
int jumpfa(int u, int dis) {
for (int i = 19; ~i; i--)
if (dis >= (1 << i)) u = fa[u][i], dis -= (1 << i);
return u;
}
void dfs(int u) {
for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int v : G[u]) {
fa[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
int deg[400005];
bool topsort(vector<int> pt) {
for (int x : pt) deg[x] = 0;
for (int x : pt) {
for (int v : G2[x]) deg[v]++;
}
queue<int> q;
int cnt = 0;
for (int x : pt) {
if (deg[x] == 0) q.push(x);
}
while (!q.empty()) {
int x = q.front();
q.pop();
cnt++;
for (int v : G2[x]) {
deg[v]--;
if (deg[v] == 0) q.push(v);
}
}
return cnt == (int)pt.size();
}
int dd[400005];
void solve() {
cin >> n;
for (int i = 2; i <= n; ++i) {
int x;
cin >> x, G[x].push_back(i);
}
dfs(1);
// for (int i = 1; i <= n; ++i) cout << dep[i] << endl;
int q;
cin >> q;
for (int tt = 1; tt <= q; ++tt) {
int m;
cin >> m;
vector<int> a(m);
for (int i = 0; i < m; ++i) cin >> a[i];
bool flag = 1;
for (int i = 1; i < m; ++i) {
if (dep[a[i]] < dep[a[i - 1]]) flag = 0;
}
if (flag) {
vector<int> vis;
for (int i = 1; i < m; ++i) {
int x = a[i - 1], y = a[i];
if (dep[x] != dep[y]) continue;
if (dd[x] == tt) flag = 0;
dd[x] = tt;
int Lca = LCA(x, y);
int xx = jumpfa(x, dep[x] - dep[Lca] - 1), yy = jumpfa(y, dep[y] - dep[Lca] - 1);
// cout << xx << " " << x << " " << yy << " " << y << " " << Lca << endl;
G2[xx].push_back(yy);
vis.push_back(xx);
vis.push_back(yy);
}
if (dd[a[m - 1]] == tt) flag = 0;
if (flag) flag = topsort(vis);
for (int v : vis) G2[v].clear();
}
cout << (flag ? "Yes" : "No") << endl;
}
}
int main() {
ios::sync_with_stdio(false);
int T = 1;
while (T--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3808kb
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: 194ms
memory: 51916kb
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 No No No No No Yes Yes No No No No No No No No Yes No No No No No Yes No No No Yes No No No No No No No Yes No Yes No No No No No No No Yes Yes No No Yes No No No No No No Yes No No No No No No No No Yes No No No Yes No No No No No No No Yes No No No Yes No No No No No ...
result:
wrong answer expected YES, found NO [2nd token]