QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#423653 | #8720. BFS 序 0 | kjhhjki# | RE | 154ms | 42936kb | C++20 | 3.4kb | 2024-05-28 14:40:57 | 2024-05-28 14:40:58 |
Judging History
answer
#include <bits/stdc++.h>
#define For(i, a, b) for(i32 i = (a), endi = (b); i <= endi; ++i)
#define foR(i, a, b) for(i32 i = (a), endi = (b); i >= endi; --i)
using i32 = int;
using i64 = long long;
struct HLD
{
i32 n, t = 0;
std::vector<i32> sz, top, dep, pre, dfn, out, seq;
std::vector<std::vector<i32>> adj;
void init(i32 _n = 0)
{
n = _n; t = 0;
sz.assign(n+1, 1); seq.assign(n+1, 0);
dep = pre = top = dfn = out = seq;
adj.assign(n+1, std::vector<i32>());
}
HLD(i32 n = 0) { init(n); }
void addEdge(i32 u, i32 v) { adj[u].push_back(v); adj[v].push_back(u); }
void work(i32 rt = 1)
{
pre[rt] = 0; dep[rt] = 1; top[rt] = rt;
dfs1(rt); dfs2(rt);
}
void dfs1(i32 u)
{
if(pre[u]) adj[u].erase(find(adj[u].begin(), adj[u].end(), pre[u]));
sz[u] = 1;
for(auto &v: adj[u])
{
pre[v] = u; dep[v] = dep[u] + 1;
dfs1(v); sz[u] += sz[v];
if(sz[v] > sz[adj[u][0]]) std::swap(v, adj[u][0]);
}
}
void dfs2(i32 u)
{
dfn[u] = ++t; seq[t] = u;
for(auto v: adj[u])
top[v] = (v == adj[u][0]? top[u]: v),
dfs2(v);
out[u] = t;
}
i32 lca(i32 u, i32 v)
{
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]]) v = pre[top[v]];
else u = pre[top[u]];
}
return dep[u] < dep[v]? u: v;
}
i32 dis(i32 u, i32 v) { return dep[u] + dep[v] - dep[lca(u, v)] * 2; }
i32 jump(i32 u, i32 k)
{
if(dep[u] < k) return 0;
while(dep[u] - dep[top[u]] < k) k -= dep[u] - dep[top[u]] + 1, u = pre[top[u]];
return seq[dfn[u]-k];
}
};
int main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0);
i32 n;
std::cin >> n;
HLD hld(n);
For(i, 2, n)
{
i32 p;
std::cin >> p;
hld.addEdge(i, p);
}
hld.work();
std::vector<std::vector<i32>> adj(n+1);
std::vector<i32> in(n+1);
i32 q;
std::cin >> q;
while(q--)
{
i32 m;
std::cin >> m;
std::vector<i32> a(m+1);
std::vector<i32> used;
bool flag = true;
For(i, 1, m)
{
std::cin >> a[i];
if(i == 1) continue;
if(a[i] == a[i-1] || hld.dep[a[i]] < hld.dep[a[i-1]]) flag = false;
if(hld.dep[a[i]] == hld.dep[a[i-1]])
{
i32 lca = hld.lca(a[i], a[i-1]);
i32 u = hld.jump(a[i-1], hld.dep[a[i-1]] - hld.dep[lca] - 1);
i32 v = hld.jump(a[i], hld.dep[a[i]] - hld.dep[lca] - 1);
used.push_back(u); used.push_back(v);
adj[u].push_back(v); ++in[v];
}
}
std::sort(used.begin(), used.end());
used.erase(std::unique(used.begin(), used.end()), used.end());
i32 cnt(0);
std::queue<i32> q;
for(auto u: used) if(!in[u]) q.push(u);
while(!q.empty())
{
i32 u = q.front(); q.pop(); ++cnt;
for(auto v: adj[u])
if(!--in[v])
q.push(v);
}
flag &= (cnt == i32(used.size()));
std::cout << (flag? "Yes": "No") << '\n';
for(auto u: used) adj[u].clear(), in[u] = 0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3644kb
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: 0
Accepted
time: 154ms
memory: 42652kb
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 No No No Y...
result:
ok 254 token(s): yes count is 89, no count is 165
Test #3:
score: 0
Accepted
time: 138ms
memory: 42676kb
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:
Yes No No Yes No No Yes Yes No Yes No Yes No No No Yes No No No No Yes No No No No No Yes Yes No No No Yes No No No No No Yes Yes Yes No No No No No No No Yes No Yes No No No Yes No No No Yes No No Yes No Yes Yes No No No Yes No Yes Yes Yes No Yes No Yes Yes Yes Yes Yes No Yes No No No No No Yes No ...
result:
ok 266 token(s): yes count is 102, no count is 164
Test #4:
score: 0
Accepted
time: 134ms
memory: 42936kb
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 Yes No Yes No No No No No No No No Yes No No No No No No No No Yes No No No No Yes No No No No No No No Yes Yes No No No No No No Yes No No No Yes Yes No No No Yes Yes No Yes No Yes No Yes No Yes No Yes No No Yes No No No No No No No Yes Yes No Yes No No No No No No Yes Yes No Yes Yes No No No...
result:
ok 252 token(s): yes count is 87, no count is 165
Test #5:
score: 0
Accepted
time: 6ms
memory: 3552kb
input:
10 1 1 3 3 1 3 1 3 9 10000 4 3 5 6 9 5 3 1 4 9 6 5 6 4 3 5 7 28 8 3 4 3 1 6 3 5 8 7 2 4 4 4 2 7 5 9 2 4 10 8 4 10 3 4 5 7 3 5 2 9 6 4 7 2 3 9 5 5 9 2 5 6 1 12 3 5 8 8 1 3 5 8 4 5 9 1 29 3 9 9 1 5 8 5 5 6 10 8 8 4 8 4 2 8 10 5 7 3 7 1 8 4 7 5 1 6 7 5 9 8 3 2 1 6 4 4 1 9 5 22 10 9 5 4 5 8 10 3 2 1 10 ...
output:
No No No 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 No No Yes No No No No No Yes No No No No No No No Yes No No No No Yes No No No Yes No No Yes No No No No No No No No No No No No No No No Yes No Yes No No No No No No No No No Yes Yes No Yes No No No No No No ...
result:
ok 10000 token(s): yes count is 1058, no count is 8942
Test #6:
score: -100
Runtime Error
input:
9 1 2 1 2 2 4 2 6 10000 6 5 1 4 3 7 8 14 6 8 7 9 7 5 1 8 3 6 3 8 5 2 2 6 9 17 7 8 6 5 2 1 5 5 9 3 4 1 5 2 7 2 5 3 8 6 5 5 3 9 1 6 8 8 3 5 8 7 9 4 6 2 4 8 9 6 5 7 4 7 2 9 3 8 1 6 1 4 9 5 6 2 4 7 5 4 3 5 9 5 2 1 3 4 4 5 7 3 3 9 1 8 4 4 2 8 6 26 7 4 7 6 1 7 9 4 5 5 8 9 8 2 4 8 9 6 2 8 6 4 5 5 4 4 45 3 ...