QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#874580#9539. Disrupting CommunicationslunchboxRE 88ms10892kbC++173.0kb2025-01-28 11:22:422025-01-28 11:22:42

Judging History

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

  • [2025-01-28 11:22:42]
  • 评测
  • 测评结果:RE
  • 用时:88ms
  • 内存:10892kb
  • [2025-01-28 11:22:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5,L=17,MOD=998244353;
int mad(int&a,int b){if((a+=b)>=MOD)a-=MOD;return a;}
int msb(int&a,int b){if((a-=b)<0)a+=MOD;return a;}
int add(int a,int b){return mad(a,b);}
int sub(int a,int b){return msb(a,b);}
int mul(int a,int b){return(ll)a*b%MOD;}
int mmu(int&a,int b){return a=(ll)a*b%MOD;}
int inv(int a){return a==1?1:mul(inv(MOD%a),MOD-MOD/a);}
int n;
vector<int>g[N];
int dd[N],jj[N][L];
int say[N],way[N],me[N];
int tin[N],tout[N],tt;
int upway[N],upme[N];
int psay[N],pway[N];
void dfs(int i){
    for(int l=1;l<L;l++)jj[i][l]=jj[jj[i][l-1]][l-1];
    say[i]=0,me[i]=1;
    tin[i]=tt++;
    for(int j:g[i]){
        //g[j].erase(find(g[j].begin(),g[j].end(),i));
        jj[j][0]=i;
        dd[j]=dd[i]+1;
        dfs(j);
        mad(say[i],way[j]);
        mmu(me[i],1+me[j]);
    }
    tout[i]=tt-1;
    way[i]=add(say[i],me[i]);
    //cout<<i<<"=>way="<<way[i]<<",say="<<say[i]<<endl;
}
int lca(int i,int j){
    if(dd[i]<dd[j])swap(i,j);
    int d=dd[i]-dd[j];
    for(int l=0;l<L;l++)if(d>>l&1)i=jj[i][l];
    if(i==j)return i;
    for(int l=L-1;l>=0;l--)if(jj[i][l]!=jj[j][l])i=jj[i][l],j=jj[j][l];
    return jj[i][0];
}
bool anc(int i,int j){
    return tin[i]<=tin[j]&&tout[i]>=tout[j];
}
void reroot(int i){
    //cout<<"i="<<i<<",got uway="<<upway[i]<<", ume="<<upme[i]<<endl;
    int nway=upway[i],nme=1+upme[i];
    for(int j:g[i])mad(nway,way[j]),mmu(nme,1+me[j]);
    for(int j:g[i]){
        upme[j]=mul(nme,inv(1+me[j]));
        upway[j]=add(sub(nway,way[j]),upme[j]);
        reroot(j);
    }
}
void make(int i){
    psay[i]=say[i],pway[i]=way[i];
    if(i)mad(psay[i],psay[jj[i][0]]),mad(pway[i],pway[jj[i][0]]);
    for(int j:g[i])make(j);
}
int qry(int i,int j){
    //assert(anc(i,j));
    int a=sub(psay[j],i?psay[jj[i][0]]:0);
    int b=sub(pway[j],pway[i]);
    //cout<<"got "<<a<<' '<<b<<endl;
    return sub(a,b);
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--){
        int q;
        cin>>n>>q;
        for(int i=1;i<n;i++){
            int p;
            cin>>p,p--;
            g[p].push_back(i);
        }
        tt=0;
        dfs(0);
        upway[0]=upme[0]=0;
        reroot(0);
        make(0);
        while(q--){
            int i,j;
            cin>>i>>j,i--,j--;
            if(tin[i]>tin[j])swap(i,j);
            int x=0;
            if(anc(i,j)){
                mad(x,qry(i,j));
                mad(x,upway[i]);
            }else{
                int p=lca(i,j);
                //cout<<"parent="<<p<<endl;
                mad(x,qry(p,i));
                mad(x,qry(p,j));
                msb(x,say[p]);
                mad(x,upway[p]);
                //cout<<"minus "<<say[p]<<", add "<<upme[p]<<endl;
            }
            //cout<<"x="<<x<<endl;
            cout<<sub(way[0],x)<<'\n';
        }
        for(int i=0;i<n;i++){
            g[i].clear();
        }
    }
}

詳細信息

Test #1:

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

input:

2
3 2
1 1
2 3
1 2
5 3
1 1 2 2
4 5
2 4
2 3

output:

6
5
14
13
15

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 83ms
memory: 6344kb

input:

3000
98 100
1 2 1 3 5 3 2 5 5 4 3 7 12 10 12 8 10 4 4 3 10 14 11 11 22 23 14 20 29 1 18 7 12 29 20 29 12 21 6 20 3 25 7 21 16 44 38 44 7 11 5 24 34 24 41 48 56 58 56 3 26 55 62 33 9 38 63 39 3 67 14 24 60 35 1 22 74 36 57 61 55 46 44 12 16 60 44 50 22 58 78 15 57 57 75 88 15
43 28
67 66
3 39
6 19
84...

output:

964062690
949799024
949777463
964050185
119859605
949794873
949799267
964064991
836980963
964045750
964065023
959849545
536098301
964045791
964064966
964046253
964052677
949782329
964050627
949794848
188617843
964065041
2617316
949782330
964046253
536098346
949777935
964052584
949777939
964046254
94...

result:

ok 300000 lines

Test #3:

score: 0
Accepted
time: 88ms
memory: 10892kb

input:

300
998 1000
1 2 1 3 3 2 2 8 5 2 8 8 12 3 13 3 7 8 16 14 10 22 10 1 24 17 16 1 16 21 2 23 2 1 20 11 1 1 22 19 5 15 10 37 15 39 13 16 33 37 37 36 37 16 3 45 10 28 14 4 16 17 55 6 6 5 31 67 51 35 47 48 10 16 75 21 45 71 28 64 39 9 37 5 65 79 28 84 29 79 21 50 21 16 93 72 58 35 30 14 86 90 60 65 33 47 ...

output:

327306708
121504060
970333956
71256467
492200713
164920447
56359491
54857868
62271655
175858304
373532115
138628785
54854112
616763633
41337286
837501264
861536431
572242958
417784906
22152900
460075855
89587278
985881197
291627546
96610921
437457168
101307362
180803897
373532108
80109336
837492247
...

result:

ok 300000 lines

Test #4:

score: -100
Runtime Error

input:

3
100000 100000
1 1 2 4 2 4 6 5 1 8 9 7 12 10 7 12 10 4 9 7 6 22 10 5 18 3 8 18 5 20 12 26 10 11 14 5 28 29 33 12 5 10 30 21 36 24 1 26 39 29 2 42 40 33 41 39 23 2 50 11 47 61 61 52 3 27 65 4 24 1 15 41 68 5 62 1 44 60 44 79 68 6 53 72 21 58 66 24 54 78 29 39 75 74 13 52 71 35 40 85 47 19 60 44 101 ...

output:


result: