QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#552417#8551. DFS Order 5ucup-team3646#WA 34ms3656kbC++204.8kb2024-09-07 22:42:072024-09-07 22:42:08

Judging History

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

  • [2024-09-07 22:42:08]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:3656kb
  • [2024-09-07 22:42:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define elif else if
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int,int>


#define repname(a, b, c, d, e, ...) e
#define rep(...)                    repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define rep0(x)                     for (int rep_counter = 0; rep_counter < (x); ++rep_counter)
#define rep1(i, x)                  for (int i = 0; i < (x); ++i)
#define rep2(i, l, r)               for (int i = (l); i < (r); ++i)
#define rep3(i, l, r, c)            for (int i = (l); i < (r); i += (c))





struct ScalarInput {
    template<class T>
    operator T(){
        T ret;
        cin >> ret;
        return ret;
    }
};
struct VectorInput {
    size_t n;
    VectorInput(size_t n): n(n) {}
    template<class T>
    operator vector<T>(){
        vector<T> ret(n);
        for(T &x : ret) cin >> x;
        return ret;
    }
};
ScalarInput input(){ return ScalarInput(); }
VectorInput input(size_t n){ return VectorInput(n); }

template<typename T>
void print(vector<T> a){
  for(int i=0;i<a.size();i++){
    cout<<a[i]<<" \n"[i+1==a.size()];
  }
}

template<class T>
void print(T x){
    cout << x << '\n';
}
 
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail){
  cout << head << ' ';
  print(forward<Tail>(tail)...);
}

template <typename G>
struct DoublingLowestCommonAncestor {
  const int LOG;
  vector<int> dep;
  const G &g;
  vector<vector<int> > table;

  DoublingLowestCommonAncestor(const G &g)
      : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector<int>(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for (auto &to : g[idx]) {
      if (to != par) dfs(to, idx, d + 1);
    }
  }

  void build() {
    dfs(0, -1, 0);
    for (int k = 0; k + 1 < LOG; k++) {
      for (int i = 0; i < table[k].size(); i++) {
        if (table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int query(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    for (int i = LOG - 1; i >= 0; i--) {
      if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if (u == v) return u;
    for (int i = LOG - 1; i >= 0; i--) {
      if (table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }

  int query0(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    for (int i = LOG - 1; i >= 0; i--) {
      if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if (u == v) return u;
    for (int i = LOG - 1; i >= 0; i--) {
      if (table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return u;
  }
};


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N,Q;
  cin>>N>>Q;
  vector<vector<int>>edge(N);
  rep(N-1){
    int u,v;
    cin>>u>>v;
    u--;v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  DoublingLowestCommonAncestor<vector<vector<int>>>LCA(edge);
  LCA.build();
  vector<int>par(N,-1);
  par[0]=-2;
  vector<int>todo,sz(N,1);
  todo.push_back(0);
  while(!todo.empty()){
    int v=todo.back();
    todo.pop_back();
    if(v<0){
      v=~v;
      sz[par[v]]+=sz[v];
    }
    for(auto u:edge[v]){
      if(par[u]==-1){
        par[u]=v;
        todo.push_back(~u);
        todo.push_back(u);
      }
    }
  }

  vector<int>ng(Q,-1);
  auto query=[&](int qi){
    int K;
    cin>>K;
    vector<int>A=input(K);
    for(int i=0;i<K;i++)A[i]--;
    vector<int>B=A;
    sort(B.begin(),B.end());
    rep(i,K-1){
      if(B[i]==B[i+1]){
        print("No");
        return;
      }
    }
    vector<pair<int,int>>stc;
    for(int i=1;i<K;i++){
      int frm=A[i-1];
      int to=A[i];
      if(to==0){
          print("No");
          return;
        }
      if(par[to]==frm){
        stc.push_back({to,1});
      }
      else{
        int L=LCA.query(to,frm);
        if(L!=par[to]){
          print("No");
          return;
        }
        if(ng[to]==qi){
          print("No");
          return;
        }
        int prev=LCA.query0(frm,L);
        ng[prev]=qi;
        int p=par[to];
        while(!stc.empty()){
          if(stc.back().first==p)break;
          else{
            auto [v,S]=stc.back();
            stc.pop_back();
            if(sz[v]!=S){
              print("No");
              return;
            }
            if(!stc.empty()){
              stc.back().second+=S;
            }
          }
        }
        stc.push_back({to,1});
      }
    }
    print("Yes");
    return;
  };

  rep(qi,Q){
    query(qi);
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 34ms
memory: 3656kb

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'