QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#710787 | #8551. DFS Order 5 | Wolam# | WA | 48ms | 7724kb | C++20 | 2.8kb | 2024-11-04 21:48:39 | 2024-11-04 21:48:41 |
Judging History
answer
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
const int maxn = 1e5 + 10;
const int root = 1;
vector<int> G[maxn];
set<pair<int,int>> edge;
int fa[maxn],sz[maxn],dfn[maxn],dep[maxn],deg[maxn],d[maxn];
int vis[maxn],arr[maxn],tot;
void dfs(int u,int pa)
{
dep[u]=dep[pa]+1;
fa[u]=pa;
// fa[u][0]=pa;
// for(int i=1;i<=__lg(deg[u]);i++)
// {
// fa[u][i]=fa[fa[u][i-1]][i-1];
// }
dfn[u]=++tot;
sz[u]=1;
for(const auto& v : G[u])
{
if(v!=pa)
{
dfs(v,u);
sz[u]+=sz[v];
}
}
}
bool ins(int u,int v)//v belong to u
{
return dfn[v]>=dfn[u]&&dfn[v]<=dfn[u]+sz[u]-1;
}
bool conn(int u,int v)//has edge <u,v>
{
if(u>v)
{
swap(u,v);
}
return edge.count({u,v});
}
void solve(void)
{
int n,q,u,v;
cin>>n>>q;
for(int i=1;i<n;i++)
{
cin>>u>>v;
if(u>v)
{
swap(u,v);
}
edge.emplace(u,v);
G[u].emplace_back(v);
G[v].emplace_back(u);
deg[u]++;
deg[v]++;
}
dfs(root,0);
for(int i=1;i<=q;i++)
{
cin>>n;
for(int j=1;j<=n;j++)
{
cin>>arr[j];
vis[arr[j]]=d[arr[j]]=0;
}
bool valid=true;
for(int j=1;j<=n;j++)
{
if(vis[arr[j]])
{
valid=false;
}
vis[arr[j]]=1;
}
vector<int> sub;
int u=arr[1];
sub.emplace_back(arr[1]);
for(int j=2;j<=n;j++)
{
while(u!=fa[arr[j-1]]&&(!conn(u,arr[j])||arr[j]==fa[u]))
{
if(fa[u]==fa[arr[j-1]])
{
valid&=(d[u]+1>=deg[u]);
}
else
{
valid&=(d[u]==deg[u]);
}
u=fa[u];
}
if(u==fa[arr[j-1]])
{
valid&=ins(fa[arr[j]],arr[j-1]);
sub.emplace_back(arr[j]);
}
else
{
d[u]++;
d[arr[j]]++;
}
u=arr[j];
}
u=sub.back();
for(int j=0;j+1<(int)sub.size();j++)
{
if(ins(u,sub[j]))
{
valid=false;
}
}
if(valid)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
while(t--)
{
solve();
}
return 0;
}
/*
6 1
1 2
1 3
2 4
3 5
2 6
2 4 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7724kb
input:
6 7 1 2 1 3 2 4 3 5 2 6 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 2 4 6 6 1 2 6 4 3 5
output:
No No Yes No No Yes Yes
result:
ok 7 tokens
Test #2:
score: -100
Wrong Answer
time: 48ms
memory: 5700kb
input:
10 100000 7 2 1 7 7 10 8 6 8 7 1 3 4 5 9 5 5 8 8 8 9 7 2 8 1 6 1 4 8 3 5 2 6 7 10 3 9 9 1 1 1 8 10 3 2 9 3 8 7 3 7 5 6 2 8 5 9 1 6 3 4 6 2 1 3 5 8 9 2 4 9 1 3 2 1 5 5 8 5 1 7 9 10 5 2 9 2 6 4 10 6 3 8 3 4 5 8 2 8 4 9 4 10 1 2 4 3 3 6 3 1 3 6 1 1 6 8 3 1 3 7 3 2 3 9 1 5 4 3 7 8 10 9 4 2 3 10 2 5 4 3 ...
output:
No No No Yes No No No No Yes No No No No No No Yes No No No No No No No No No No No No No No No Yes No No No No No No Yes No No No No No Yes Yes 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 Yes Yes No No No No Yes No No No No No No No No No No No No No No No No N...
result:
wrong answer 899th words differ - expected: 'No', found: 'Yes'