QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#465307#8551. DFS Order 5ucup-team191#WA 26ms9196kbC++143.1kb2024-07-06 20:00:552024-07-06 20:00:56

Judging History

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

  • [2024-07-06 20:00:56]
  • 评测
  • 测评结果:WA
  • 用时:26ms
  • 内存:9196kb
  • [2024-07-06 20:00:55]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long llint;
typedef pair <int, int> pi;
typedef vector <int> vi;

const int MAXN = 100005;
const int LOG = 19;

int n, q;
int par[MAXN][LOG], dub[MAXN], bio[MAXN], broj_djece[MAXN], deg[MAXN];
vector <int> v[MAXN];

void dfs (int x, int rod) {
    par[x][0] = rod;
    dub[x] = 1 + dub[rod];
    for (auto sus : v[x]) {
        if (sus == rod) continue;
        dfs(sus, x);
        broj_djece[x]++;
    }
}

void precompute_lca () {
    for (int i = 1; i < LOG; i++) {
        for (int j = 1; j <= n; j++) {
            par[j][i] = par[par[j][i - 1]][i - 1];
        }
    }
}

int kth (int x, int k) {
    int pot = 0;
    while (k > 0) {
        if (k % 2 == 1) x = par[x][pot];
        k /= 2;
        pot++;
    }
    return x;
}

int lca (int a, int b) {
    if (dub[a] < dub[b]) swap(a, b);
    a = kth(a, dub[a] - dub[b]);
    if (a == b) return a;
    for (int i = LOG - 1; i >= 0; i--) {
        if (par[a][i] != par[b][i]) {
            a = par[a][i];
            b = par[b][i];
        }
    }
    return par[a][0];
}

bool ima_duplica (const vi& r) {
    for (auto x : r) {
        bio[x] = 0;
    }
    for (auto x : r) {
        bio[x]++;
        if (bio[x] == 2) return 1;
    }
    return 0;
}

bool popaj (vector <pi> &st) {
    int node = st.back().first, tip = st.back().second;
    if (tip == 1) {
        st.pop_back();
        return 1;
    }
    if (deg[node] == broj_djece[node]) {
        st.pop_back();
        deg[par[node][0]]++;
        return 1;
    }
    return 0;
}

bool solve (const vi& r) {
    if (ima_duplica(r)) return 0;

    vector <pi> st;
    st.push_back({r[0], 0});

    for (auto x : r) {
        deg[x] = 0;
    }

    for (int i = 1; i < (int)r.size(); i++) {
        int novi = r[i];
        int stari = st.back().first;
        int L = lca(stari, novi);

        if (L == novi) {
            return 0;
        }

        if (L == stari) {
            if (par[novi][0] != stari) {
                return 0;
            }
            st.push_back({novi, 0});
        } else {
            while (!st.empty() && lca(st.back().first, novi) != st.back().first) {
                if (popaj(st) == 0) return 0;
            }
            if (st.empty() || st.back().first != L) {
                st.push_back({L, 1});
            }
            if (par[novi][0] != L) {
                return 0;
            }
            st.push_back({novi, 0});
        }
    }

    return 1;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> q;
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1, 0);
    precompute_lca();
    for (int i = 0; i < q; i++) {
        int siz;
        cin >> siz;
        vi r(siz);
        for (int j = 0; j < siz; j++) {
            cin >> r[j];
        }
        int res = solve(r);
        if (res) cout << "Yes\n"; else cout << "No\n";
    }
    return 0;
}

詳細信息

Test #1:

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

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: 8228kb

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'