QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#310057#8134. LCA Countingucup-team1134#WA 1ms3840kbC++233.8kb2024-01-21 00:40:472024-01-21 00:40:47

Judging History

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

  • [2024-01-21 00:40:47]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3840kb
  • [2024-01-21 00:40:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

// https://beet-aizu.github.io/library/tree/auxiliarytree.cpp

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct LowestCommonAncestor{
  int h;
  vector< vector<int> > G,par;
  vector<int> dep;
  LowestCommonAncestor(int n):G(n),dep(n){
    h=1;
    while((1<<h)<=n) h++;
    par.assign(h,vector<int>(n,-1));
  }

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void dfs(int v,int p,int d){
    par[0][v]=p;
    dep[v]=d;
    for(int u:G[v])
      if(u!=p) dfs(u,v,d+1);
  }

  void build(int r=0){
    int n=G.size();
    dfs(r,-1,0);
    for(int k=0;k+1<h;k++)
      for(int v=0;v<n;v++)
        if(~par[k][v])
          par[k+1][v]=par[k][par[k][v]];
  }

  int lca(int u,int v){
    if(dep[u]>dep[v]) swap(u,v);
    for(int k=0;k<h;k++)
      if((dep[v]-dep[u])>>k&1)
        v=par[k][v];

    if(u==v) return u;

    for(int k=h-1;k>=0;k--)
      if(par[k][u]!=par[k][v])
        u=par[k][u],v=par[k][v];

    return par[0][u];
  }

  int distance(int u,int v){
    return dep[u]+dep[v]-dep[lca(u,v)]*2;
  }
};
//END CUT HERE

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

#define call_from_test
//#include "lowestcommonancestor.cpp"
#undef call_from_test

#endif

//BEGIN CUT HERE
struct AuxiliaryTree : LowestCommonAncestor{
  using super = LowestCommonAncestor;

  vector<int> idx;
  vector<vector<int>> T;
  AuxiliaryTree(int n):super(n),idx(n),T(n){}

  void dfs(int v,int p,int &pos){
    idx[v]=pos++;
    for(int u:G[v])
      if(u!=p) dfs(u,v,pos);
  }

  void build(int r=0){
    super::build(r);
    int pos=0;
    dfs(r,-1,pos);
  }

  void add_aux_edge(int u,int v){
    T[u].emplace_back(v);
    T[v].emplace_back(u);
  }

  using super::lca, super::dep;
  void query(vector<int> &vs){
    assert(!vs.empty());
    sort(vs.begin(),vs.end(),
         [&](int a,int b){return idx[a]<idx[b];});
    vs.erase(unique(vs.begin(),vs.end()),vs.end());

    int k=vs.size();
    stack<int> st;
    st.emplace(vs[0]);
    for(int i=0;i+1<k;i++){
      int w=lca(vs[i],vs[i+1]);
      if(w!=vs[i]){
        int l=st.top();st.pop();
        while(!st.empty() and dep[w]<dep[st.top()]){
          add_aux_edge(st.top(),l);
          l=st.top();st.pop();
        }
        if(st.empty() or st.top()!=w){
          st.emplace(w);
          vs.emplace_back(w);
        }
        add_aux_edge(w,l);
      }
      st.emplace(vs[i+1]);
    }

    while(st.size()>1){
      int c=st.top();st.pop();
      add_aux_edge(st.top(),c);
    }
  }

  void clear(const vector<int> &ws){
    for(int w:ws) T[w].clear();
  }
};
//END CUT HERE

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    AuxiliaryTree G(N);
    vector<int> ha(N,1);
    for(int i=1;i<N;i++){
        int p;cin>>p;p--;
        G.add_edge(p,i);
        ha[p]=false;
    }
    G.build();
    vector<int> vs;
    
    int hacn=0;
    for(int i=1;i<N;i++){
        if(ha[i]){
            hacn++;
            vs.push_back(i);
        }
    }
    
    G.query(vs);
    
    int ans=0;
    
    for(int i=1;i<=hacn;i++){
        ans++;
        if(i>1&&(i-1)<=si(vs)-hacn) ans++;
        if(i>1) cout<<" ";
        cout<<ans;
    }
    cout<<endl;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

7
1 1 2 4 2 2

output:

1 3 5 6

result:

ok 4 number(s): "1 3 5 6"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 5 6 7 8 9

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 3592kb

input:

10
1 2 2 4 5 6 1 2 4

output:

1 3 5 7 8

result:

ok 5 number(s): "1 3 5 7 8"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

10
1 2 3 3 3 5 6 4 9

output:

1 3 4

result:

ok 3 number(s): "1 3 4"

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3840kb

input:

1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...

output:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 12...

result:

wrong answer 5th numbers differ - expected: '8', found: '9'