QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#732772 | #8551. DFS Order 5 | arnold518# | WA | 40ms | 8680kb | C++17 | 2.7kb | 2024-11-10 15:51:19 | 2024-11-10 15:51:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long long lint;
const int MAXN = 1e5;
int N, Q;
vector<int> adj[MAXN+10];
int par[MAXN+10][21], dep[MAXN+10];
int L[MAXN+10], R[MAXN+10], cnt;
vector<int> V;
void dfs(int now, int bef, int d)
{
L[now]=++cnt;
par[now][0]=bef;
dep[now]=d;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, d+1);
}
R[now]=cnt;
}
int lca(int u, int v)
{
if(dep[u]>dep[v]) swap(u, v);
for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) v=par[v][i];
if(u==v) return u;
for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
return par[u][0];
}
int jump(int u, int p)
{
for(int i=20; i>=0; i--) if(dep[par[u][i]]>dep[p]) u=par[u][i];
return u;
}
bool f(int pos)
{
int now=V[pos];
int sz=R[now]-L[now]+1;
for(int i=pos+1; i<V.size() && i<pos+sz;)
{
int now2=V[i];
int sz2=R[i]-L[i]+1;
if(par[now2][0]!=now) return false;
if(!f(i)) return false;
i+=sz2;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> Q;
for(int i=1; i<N; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1, 1);
for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];
while(Q--)
{
int k;
cin >> k;
vector<int> VV;
while(k--)
{
int t;
cin >> t;
V.push_back(t);
VV.push_back(t);
}
// 1. unique
sort(VV.begin(), VV.end());
VV.erase(unique(VV.begin(), VV.end()), VV.end());
if(VV.size()!=V.size()) cout << "No\n";
else
{
bool ans=true;
set<int> S;
for(int i=0; i<V.size();)
{
int now=V[i];
int sz=R[now]-L[now]+1;
if(S.count(now))
{
ans=false;
break;
}
if(!f(i))
{
ans=false;
break;
}
if(i+sz>=V.size()) break;
int nxt=V[i+sz];
int w=lca(now, nxt);
if(w!=par[nxt][0])
{
ans=false;
break;
}
S.insert(w);
S.insert(jump(now, w));
i+=sz;
}
if(ans) cout << "Yes\n";
else cout << "No\n";
}
V.clear();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 8680kb
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: 40ms
memory: 7616kb
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 321st words differ - expected: 'No', found: 'Yes'