QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#537223#8551. DFS Order 5obiwan114WA 30ms5616kbC++202.9kb2024-08-29 23:36:132024-08-29 23:36:14

Judging History

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

  • [2024-08-29 23:36:14]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:5616kb
  • [2024-08-29 23:36:13]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int MAXX = 1e5 + 5;
vector<int>g[MAXX];
int fa[MAXX],dep[MAXX],siz[MAXX],son[MAXX],top[MAXX],dfn[MAXX],rnk[MAXX];
int lowbit(int x){return -x & x;}
void dfs1(int u){
    son[u] = -1;
    siz[u] = 1;
    for(int v : g[u]){
        if(dep[v])continue;
        dep[v] = dep[u] + 1;
        fa[v] = u;
        dfs1(v);
        siz[u] += siz[v];
        if(son[u] == -1 || siz[v] > siz[son[u]]){
            son[u] = v;
        }
    }
}
int cnt = 0;
void dfs2(int u,int t){
    cnt++;
    dfn[u] = cnt;
    rnk[cnt] = u;
    top[u] = t;
    if(son[u] == -1)return;
    dfs2(son[u],t);
    for(int v : g[u]){
        if(v == fa[u])continue;
        if(v == son[u])continue;
        dfs2(v,v);
    }
}
int n,q;
int a[MAXX];
vector<int>sum;
int lca(int u, int v) {
    while (top[u] != top[v]) {
        if (dep[top[u]] > dep[top[v]])
            u = fa[top[u]];
        else
            v = fa[top[v]];
    }
    return dep[u] > dep[v] ? v : u;
}
int query(int x){
    int res = 0;
    while(x){
        res += sum[x];
        x -= lowbit(x);
    }
    return res;
}
void update(int x){
    while(x <= n){
        sum[x]++;
        x += lowbit(x);
    }
}
void solve() {
    int k;
    cin >> k;
    for(int i = 1;i <= k;i++){
        cin >> a[i];
    }
    sum = vector<int>(n + 2);
    stack<int>st;
    update(dfn[a[1]]);
    st.push(a[1]);
    for(int i = 2;i <= k;i++){
        int x = a[i - 1];
        int y = a[i];
        if(x != fa[y] && son[x] != -1){
            cout << "No\n";
            return;
        }
        if(x != fa[y]){
            int l = lca(x,y);
            while(!st.empty()){
                int t = st.top();
                if(dep[t] <= dep[l])break;
                st.pop();
                if(query(dfn[t] + siz[t] - 1) - query(dfn[t] - 1) < siz[t]){
                    cout << "No\n";
                    return;
                }
            }
            x = l;
            if(x != fa[y]){
                cout << "No\n";
                return;
            }
            if(query(dfn[y] + siz[y] - 1) - query(dfn[y] - 1)){
                cout << "No\n";
                return;
            }
        }else{
            if(query(dfn[y] + siz[y] - 1) - query(dfn[y] - 1)){
                cout << "No\n";
                return;
            }
            st.push(y);
        }
        update(dfn[y]);
    }
    cout << "Yes\n";
}

signed main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> q;
    for(int i = 1;i < n;i++){
        int u,v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dep[1] = 1;
    dfs1(1);
    dfs2(1,1);
    while (q--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5616kb

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: 30ms
memory: 3548kb

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 4151st words differ - expected: 'No', found: 'Yes'