QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#603738 | #8720. BFS 序 0 | reaepita | WA | 246ms | 49636kb | C++14 | 1.8kb | 2024-10-01 18:43:44 | 2024-10-01 18:43:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
vector<int> G[maxn];
vector<int> G2[maxn];
int fa[maxn][20], dep[maxn];
void dfs(int u)
{
for (int i = 1; i <= 19; i++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
dep[v] = dep[u] + 1;
dfs(v);
}
}
int lca(int u, int v)
{
if (dep[u] < dep[v])
swap(u, v);
for (int i = 19; i >= 0; i--)
if (dep[fa[u][i]] >= dep[v])
u = fa[u][i];
if (u == v)
return u;
for (int i = 19; i >= 0; i--)
if (fa[u][i] != fa[v][i])
u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int in[maxn];
bool topsort(vector<int> V)
{
int t = 0;
queue<int> q;
for (auto x : V)
if (!in[x])
q.push(x);
while (!q.empty())
{
int u = q.front();
q.pop();
t++;
for (int i = 0; i < G2[u].size(); i++)
{
int v = G2[u][i];
in[v]--;
if (!in[v])
q.push(v);
}
}
for (auto x : V)
in[x] = 0,G2[x].clear();
return t == V.size();
}
int n, q;
int main()
{
scanf("%d", &n);
for (int i = 2; i <= n; i++)
{
int x;
scanf("%d", &x);
fa[i][0] = x;
G[x].push_back(i);
}
dfs(1);
scanf("%d",&q);
while (q--)
{
vector<int> v;
int k;
scanf("%d", &k);
int a, b = 0, flag = 0;
while (k--)
{
scanf("%d", &a);
if(dep[b]>dep[a])flag=1;
if (b&&dep[a]==dep[b])
{
int c = lca(a, b);
for (int i = 19; i >= 0; i--)
if (dep[fa[a][i]] > dep[c])
a = fa[a][i];
for (int i = 19; i >= 0; i--)
if (dep[fa[b][i]] > dep[c])
b = fa[b][i];
G2[b].push_back(a);
in[a]++;
v.push_back(a);
v.push_back(b);
}
b = a;
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
if (topsort(v)&&(!flag))
puts("Yes");
else
puts("No");
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20312kb
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: 246ms
memory: 49636kb
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 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 Yes No No Yes No No No No No No No Yes No Yes No Yes No No No No Yes Yes Yes No No Yes No No No No No Yes Yes No No No No No Yes Yes No Yes No No No Yes No No Yes No No Yes Yes Yes Yes No No ...
result:
wrong answer expected NO, found YES [87th token]