QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#474769#8551. DFS Order 5real_sigma_teamWA 32ms3640kbC++234.5kb2024-07-13 00:43:062024-07-13 00:43:07

Judging History

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

  • [2024-07-13 00:43:07]
  • 评测
  • 测评结果:WA
  • 用时:32ms
  • 内存:3640kb
  • [2024-07-13 00:43:06]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef lisie_bimbi
#else
#define endl '\n'
#endif
//#define int long long
typedef long long ll;

#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,bmi2,fma")

struct lca {
    int n;
    vector<vector<int>> v;
    vector<vector<int>> up;
    vector<int> d;
    vector<int> tin, tout;
    int tt = 0;

    void dfs(int u, int par) {
        tin[u] = tt++;
        if (par == -1) {
            d[u] = 0;
            up[u][0] = u;
        } else {
            d[u] = d[par] + 1;
            up[u][0] = par;
        }
        for (int j = 1; j < 20; j++) {
            up[u][j] = up[up[u][j - 1]][j - 1];
        }
        for (auto i: v[u]) {
            if (i != par) {
                dfs(i, u);
            }
        }
        tout[u] = tt++;
    }

    void init(int root) {
        n = v.size();
        up.resize(n, vector<int>(20));
        tin.resize(n);
        tout.resize(n);
        d.resize(n);
        dfs(root, -1);
    }

    int la(int a, int x) {
        for (int i = 19; i >= 0; i--) {
            if (x >= (1 << i)) {
                x -= (1 << i);
                a = up[a][i];
            }
        }
        return a;
    }

    bool isp(int a, int b) {
        return (tin[a] < tin[b]) && (tout[b] < tout[a]);
    }

    int get(int a, int b) {
        if (isp(a, b)) {
            return a;
        }
        if (isp(b, a)) {
            return b;
        }
        if (a == b) return a;
        for (int i = 19; i >= 0; i--) {
            if (!isp(up[a][i], b)) {
                a = up[a][i];
            }
        }
        return up[a][0];
    }

    int govno(int a, int b) {
        if (!isp(a, b)) {
            return -1;
        }
        int x = d[b] - d[a];
        int c = la(b, x - 1);
        return c;
    }

    int lox(int a, int b){
        int c = get(a, b);
        int ans;
        if(c == b){
            ans = govno(b, a);
        }else{
            ans = up[b][0];
        }
        //cout << "aaaa " << ans << " " << c << endl;
        if(ans != c){
            return -1;
        }else{
            return ans;
        }
    }
};

lca l;
vector<vector<int>> v;

bool reb(int a, int b){
    if(b > v[a].back()){
        return 0;
    }
    return (*lower_bound(v[a].begin(), v[a].end(), b) == b);
} 

bool dfs(int u, int par, int &now, vector<int> &ord){
    if(now >= ord.size()){
        return 1;
    }
    if(ord[now] != u){
        return 0;
    }
    now++;
    if(now >= ord.size()){
        return 1;
    }
    if(v[u].size() > 1){
        bool f = 1;
        int x;
        for(int i = 0; (i < v[u].size() - 1) && f; i++){
            if(now >= ord.size()){
                return 0;
            }
            x = ord[now];
            if(!reb(u, x)){
                return 0;
            }
            f = f && dfs(x, u, now, ord);
        }
        return f;
    }else{
        return 1;
    }
}

bool ___(vector<int> a){
    bool f = 0;
    if(a[0] == 0){
        f = 1;
    }
    sort(a.begin(), a.end());
    for(int i = 0; i + 1 < a.size(); i++){
        if(a[i] == a[i + 1]){
            return 0;
        }
    }
    if(a[0] == 0){
        return f;
    }
    return 1;
}

void solve(){
    int k;
    cin >> k;
    vector<int> ord(k);
    for(int i = 0; i < k; i++){
        cin >> ord[i];
        ord[i]--;
    }
    //////////////
    if(!___(ord)){
        cout << "No\n";
        return;
    }
    /////////////
    int now = 1;
    int x = ord[0];
    int y;
    bool f = 1;
    while(f && (now < ord.size())){
        y = ord[now];
        if(!reb(x, y)){
            //cout << "tttttt " << x << " " << y << endl;
            x = l.lox(x, y);
            //cout << x << endl;
            if(x == -1){
                cout << "No\n";
                return;
            }
        }
        if(l.up[x][0] == y){
            cout << "No\n";
            return;
        }
        f = f && dfs(y, x, now, ord);
    }
    if(f){
        cout << "Yes\n";
    }else{
        cout << "No\n";
    }
}

signed main(){
#ifdef lisie_bimbi
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);    
#endif        
    int n, q;
    cin >> n >> q;
    l.v.resize(n);
    v.resize(n);
    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        a--;b--;
        l.v[a].push_back(b);
        l.v[b].push_back(a);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    for(int i = 0; i < n; i++){
        sort(v[i].begin(), v[i].end());
    }
    l.init(0);
    while(q--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

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: 32ms
memory: 3640kb

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