QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#691266 | #8551. DFS Order 5 | szy10010 | WA | 26ms | 11856kb | C++23 | 2.4kb | 2024-10-31 10:38:22 | 2024-10-31 10:38:24 |
Judging History
answer
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define u1 (u<<1)
#define u2 (u<<1|1)
#define pb push_back
#define pp pop_back()
#define int long long
#define laile cout<<"laile"<<endl
#define lowbit(x) ((x)&(-x))
#define double long double
#define sf(x) scanf("%lld",&x)
#define sff(x,y) scanf("%lld %lld",&x,&y)
#define sd(x) scanf("%Lf",&x)
#define sdd(x,y) scanf("%Lf %Lf",&x,&y)
#define _for(i,n) for(int i=0;i<(n);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define _pre(i,a,b) for(int i=(a);i>=(b);--i)
#define all(x) (x).begin(), (x).end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef unsigned long long ULL;
typedef pair<int,int>PII;
const int N=1e6+10,INF=4e18;
int n,m;
int fa[N],dep[N],top[N],sz[N],son[N],cnt[N];
vector<int>e[N];
void dfs1(int u, int father) {
fa[u] = father, sz[u] = 1, dep[u] = dep[father] + 1;
for (auto j : e[u]) {
if (j == father)continue;
dfs1(j, u);
sz[u] += sz[j];
if (sz[son[u]] < sz[j])son[u] = j;
}
return;
}
void dfs2(int u, int v) { //找到u结点在重链上深度最小的顶点,所以可以找到每个点的链头O(n)
top[u] = v; //记录链头
if (!son[u])return; //没有重儿子,结束
dfs2(son[u], v); //搜重儿子
for (auto j : e[u]) {
if (j == fa[u] || j == son[u])continue;
dfs2(j, j); //搜轻儿子
}
return;
}
int lca(int u, int v) { //log(n)因为每一条链不会被切分成超过logn条链
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]])swap(u, v); //保证u的链头深度更深
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
void solve()
{
cin>>n>>m;
_rep(i,2,n)
{
int a,b;
cin>>a>>b;
e[a].pb(b);
e[b].pb(a);
}
dfs1(1,0);
dfs2(1,1);
while(m--)
{
cin>>n;
bool bl=true;
int pre=0;
_rep(i,1,n)
cin>>son[i];
_rep(i,1,n)
{
cnt[son[i]]++;
if(pre)
{
if(pre==fa[son[i]])
cnt[pre]++;
else
{
if(!fa[son[i]]||fa[son[i]]!=lca(son[i],pre)||cnt[pre]!=sz[pre])
{
bl=false;
break;
}
}
}
pre=son[i];
}
_rep(i,1,n)cnt[son[i]]=0;
if(bl)cout<<"Yes\n";
else cout<<"No\n";
}
return ;
}
signed main()
{
IOS;
int T=1;
// cin>>T;
while(T--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 11856kb
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: 26ms
memory: 11836kb
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 Yes No No No No No No No ...
result:
wrong answer 89th words differ - expected: 'No', found: 'Yes'