QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#534386#8551. DFS Order 5rotcar07WA 27ms8956kbC++141.9kb2024-08-27 08:20:562024-08-27 08:20:56

Judging History

你现在查看的是最新测评结果

  • [2024-08-27 08:20:56]
  • 评测
  • 测评结果:WA
  • 用时:27ms
  • 内存:8956kb
  • [2024-08-27 08:20:56]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=1e5+5;
int n,q;
vector<int> e[maxn];
int tqm,sz[maxn],dfn[maxn],st[maxn][25],fa[maxn];
inline int cmin(int x,int y){return dfn[x]<dfn[y]?x:y;}
void dfs(int p,int f){
    dfn[p]=++tqm;st[dfn[p]-1][0]=f;fa[p]=f;
    sz[p]=1;
    vector<int> tmp;
    for(auto x:e[p]) if(x!=f)dfs(x,p),sz[p]+=sz[x],tmp.push_back(x);
    swap(e[p],tmp);
}
inline int lca(int x,int y){
    if(x==y) return x;
    x=dfn[x],y=dfn[y];
    if(x>y) swap(x,y);
    int k=__lg(y-x);
    // cout<<x<<' '<<y<<' '<<k<<' '<<st[x][k]<<' '<<st[y-(1<<k)][k]<<'\n';
    return cmin(st[x][k],st[y-(1<<k)][k]);
}
int cnt[maxn];
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>q;
    for(int i=1,u,v;i<n;i++) cin>>u>>v,e[u].push_back(v),e[v].push_back(u);
    dfs(1,0);
    for(int j=1;1<<j<n;j++)
    for(int l=1;l+(1<<j)-1<n;l++)
        st[l][j]=cmin(st[l][j-1],st[l+(1<<j-1)][j-1]);
    while(q--){
        int k;cin>>k;
        vector<int> a(k);
        for(auto&x:a) cin>>x,cnt[x]=0;
        stack<int> st;st.push(a[0]);
        for(int i=1;i<k;i++){
            int lst=0;
            while(!st.empty()&&cnt[st.top()]==e[st.top()].size()) lst=st.top(),st.pop();
            // cout<<i<<' '<<a[i]<<' '<<lst<<' '<<st.top()<<' '<<e[st.top()].size()<<' '<<cnt[st.top()]<<' '<<st.size()<<'\n';
            if(!st.empty()){
                if(fa[a[i]]==st.top()) cnt[st.top()]++,st.push(a[i]);
                else{
                    goto fail;
                }
            }
            if(st.empty()){
                // cout<<a[i]<<' '<<lst<<' '<<lca(a[i],lst)<<'\n';
                if(fa[a[i]]==lca(a[i],lst)){
                    st.push(a[i]);
                }
                else goto fail;
            }
        }
        cout<<"Yes\n";
        continue;
        fail:
        cout<<"No\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8496kb

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: 27ms
memory: 8956kb

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'