QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#589481 | #8720. BFS 序 0 | tarjen# | WA | 2687ms | 70040kb | C++20 | 2.3kb | 2024-09-25 18:07:03 | 2024-09-25 18:07:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn=3e5+10;
vector<int>ve[maxn];
int dep[maxn],f[21][maxn];
void dfs(int x,int fa)
{
dep[x]=dep[fa]+1;
for(int i=0;i<=19;i++)
f[i+1][x]=f[i][f[i][x]];
for(auto it:ve[x])
{
if(it==fa) continue;
f[0][it]=x;
dfs(it,x);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=20;i>=0;i--)
{
if(dep[f[i][x]]>=dep[y]) x=f[i][x];
if(x==y) return x;
}
for(int i=20;i>=0;i--)
if(f[i][x]!=f[i][y])
x=f[i][x],y=f[i][y];
return f[0][x];
}
int findk(int x,int k){
for(int i=20;i>=0;i--)if(k>>i&1)x=f[i][x];
return x;
}
vector<int> ve2[maxn];
int cnt[maxn];
bool check(vector<pair<int,int>> &edges){
set<int> s;
for(auto [x,y]:edges)s.insert(x),s.insert(y),ve2[x].push_back(y);
vector<int> v(s.begin(),s.end());
for(auto [x,y]:edges)cnt[y]++;
queue<int> qu;
for(auto it:v)if(cnt[it]==0)qu.push(it);
int all=(int)edges.size();
while(!qu.empty()){
int x=qu.front();qu.pop();
for(auto it:ve2[x]){
if((--cnt[it])==0)qu.push(it);
all--;
}
}
for(auto it:v)cnt[it]=0,ve2[it].clear();
return all==0;
}
void solve()
{
int n;cin>>n;
for(int i=1;i<=n;i++)ve[i].clear();
for(int i=2;i<=n;i++){
int x;cin>>x;
ve[x].push_back(i);
ve[i].push_back(x);
}
dfs(1,0);
vector<vector<pair<int,int>>> edges(n+1);
auto sol = [&]()
{
int len;cin>>len;
vector<int> a(len);
set<int> s;
for(int i=0;i<len;i++)cin>>a[i],s.insert(a[i]);
if((int)s.size()!=len)return "No";
for(int i=0;i+1<len;i++)if(dep[a[i]]>dep[a[i+1]])return "No";
vector<int> in;
for(int i=0;i+1<len;i++)if(dep[a[i]]==dep[a[i+1]]){
int l=lca(a[i],a[i+1]);
in.push_back(l);
edges[l].emplace_back(findk(a[i],dep[a[i]]-dep[l]-1),findk(a[i+1],dep[a[i+1]]-dep[l]-1));
}
sort(in.begin(),in.end());
for(auto it:in)if(!check(edges[it]))return "No";
for(auto it:in)edges[it].clear();
return "Yes";
};
int q;cin>>q;
while(q--){
cout<<sol()<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T=1;while(T--)solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 26120kb
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: 2687ms
memory: 70040kb
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:
wrong answer expected YES, found NO [93rd token]