QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#310051#8134. LCA Countingucup-team1134#WA 1ms3740kbC++233.7kb2024-01-21 00:35:552024-01-21 00:35:55

Judging History

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

  • [2024-01-21 00:35:55]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3740kb
  • [2024-01-21 00:35:55]
  • 提交

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;

struct HeavyLightDecomposition{
    int n;
    vector<int> sz,in,out,nxt,par,depth;
    vector<vector<int>> G;
    
    HeavyLightDecomposition(){}
    
    HeavyLightDecomposition(int n_){
        n=n_;
        sz.assign(n,0);
        in.assign(n,0);
        out.assign(n,0);
        nxt.assign(n,0);
        par.assign(n,0);
        depth.assign(n,0);
        G.assign(n,vector<int>());
    }
    
    void add_edge(int u,int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }
    
    void dfs_sz(int u,int p){
        par[u]=p;
        sz[u]=1;
        if(G[u].size()&&G[u][0]==p) swap(G[u][0],G[u].back());
        for(auto &a:G[u]){
            if(a==p) continue;
            depth[a]=depth[u]+1;
            dfs_sz(a,u);
            sz[u]+=sz[a];
            if(sz[a]>sz[G[u][0]]){
                swap(a,G[u][0]);
            }
        }
    }
    
    void dfs_hld(int u,int p,int &t){
        in[u]=t++;
        for(auto a:G[u]){
            if(a==p) continue;
            nxt[a]=(a==G[u][0] ? nxt[u] : a);
            dfs_hld(a,u,t);
        }
        out[u]=t;
    }
    
    void build(int u){
        int t=0;
        dfs_sz(u,-1);
        dfs_hld(u,-1,t);
    }
    
    int lca(int u,int v){
        if(in[u]>in[v]) swap(u,v);
        if(nxt[u]==nxt[v]) return u;
        return lca(u,par[nxt[v]]);
    }
    
    int mov1(int a,int b){
        if(a==b) return a;
        int c=lca(a,b);
        if(c==a){
            int l=0,r=si(G[a]);
            while(r-l>1){
                int m=(l+r)/2;
                if(par[a]==G[a][m]){
                    if(m+1<r){
                        if(r-l==2){
                            l=m+1;
                            break;
                        }
                        if(in[G[a][m+1]]<=in[b]) l=m+1;
                        else r=m;
                    }else{
                        if(r-l==2){
                            l=m-1;
                            break;
                        }
                        if(in[G[a][m-1]]<=in[b]) l=m-1;
                        else r=m-1;
                    }
                }else{
                    if(in[G[a][m]]<=in[b]) l=m;
                    else r=m;
                }
            }
            if(par[a]!=G[a][l]) return G[a][l];
            else return G[a][l+1];
            //return G[a][l];
        }else{
            return par[a];
        }
    }
    //aからbに向かって1進んだところ
};

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    HeavyLightDecomposition hld(N);
    for(int i=1;i<N;i++){
        int p;cin>>p;p--;
        hld.add_edge(p,i);
    }
    
    hld.build(0);
    
    vector<pair<int,int>> S;
    for(int i=1;i<N;i++){
        if(si(hld.G[i])==1) S.push_back(mp(hld.in[i],i));
    }
    sort(all(S));
    
    set<int> SE;
    for(int i=0;i+1<si(S);i++){
        SE.insert(hld.lca(S[i].se,S[i+1].se));
    }
    
    int ans=0;
    
    for(int i=1;i<=si(S);i++){
        ans++;
        if(i>1&&(i-1)<=si(SE)) 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: 3740kb

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

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

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

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

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'