QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#782613#9530. A Game On TreeWaO_oWA 2ms5632kbC++203.3kb2024-11-25 20:40:382024-11-25 20:40:38

Judging History

This is the latest submission verdict.

  • [2024-11-25 20:40:38]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 5632kb
  • [2024-11-25 20:40:38]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

#define deg( x ) cout<<""#x"="<<x<<endl
#define int long long
#define endl '\n'

const int N=1e5+10;
const int mod=998244353;

vector<int> G[ N ];

int sz[ N ];
int sum[ N ];
int faa[ N ];
int hh[ N ];

void init( int n ){
    for( int i=1; i<=n; i++ ){
        G[ i ].clear();
        hh[ i ]=faa[ i ]=sz[ i ]=sum[ i ]=0;
    }
}

void dfs( int rt , int fa ){
    sz[ rt ]=1;
    faa[ rt ]=fa;
    int re=0;

    for( auto son:G[ rt ] ){
        if( son==fa ) continue;
        dfs( son , rt );

        hh[ rt ]+=sz[ son ]*sz[ son ];
        hh[ rt ]%=mod;
        sz[ rt ]+=sz[ son ];
        sz[ rt ]%=mod;
        re+=sum[ son ];
        re%=mod;
    }

    sum[ rt ]+=re+sz[ rt ]*sz[ rt ];
    sum[ rt ]%=mod;
}

int exgcd( int a , int b , int &x, int &y ) //扩展欧几里得求同余方程解; x,y及为 x*a+y*b=1 的一组可行解;
{
    if( b==0 ){
        x=1,y=0;
        return a;
    }

    int com=exgcd( b , a%b , x , y );

    int _x=x , _y=y ;
    y=_x-(a/b)*_y; x=_y;

    return com;
}

int getinv( int x , int m ) // exgcd获取x在模m情况下的逆元
{
    int ans,y;
    exgcd( x , m , ans , y );
    ans=( ( ans%m )+m )%m;
    return ans;
}


void solve(){
    int n;
    cin>>n;

    init( n );

    for( int i=1,u,v; i<n; i++ ){
        cin>>u>>v;
        G[ u ].emplace_back( v );
        G[ v ].emplace_back( u );
    }

    dfs( 1 , 0 );

    int ans=0;
    for( int i=1; i<=n; i++ ){
        int cnt=0;

        int rt=i;
        for( auto son:G[ rt ] ){
            if( son==faa[ rt ] ) continue;

            cnt=sz[ son ]*sz[ son ]; cnt%=mod;
            cnt*=( n-sz[ son ] ); cnt%=mod;
            cnt*=( n-sz[ son ] ); cnt%=mod;
            ans+=cnt; ans%=mod;

            cnt=0;
            cnt=2*( n-sz[ son ] )*( n-sz[ son ] );
            cnt%=mod;

            int e=( sum[ son ]+mod*mod-sz[ son ]*sz[ son ] );
            e%=mod;
            cnt*=e; cnt%=mod;
            ans+=cnt; ans%=mod;

            cnt=sz[ son ]*sz[ son ]; cnt%=mod;

            e=( sum[ rt ]-sum[ son ]-sz[ rt ]*sz[ rt ]+mod*mod+mod )%mod;
            cnt*=2*e; cnt%=mod;
            ans+=cnt; ans%=mod;
        }
    }

    int jian=0;

    for( int i=1; i<=n; i++ ){
        int rt=i;
        for( auto son:G[ rt ] ){
            if( son==faa[ rt ] ) continue;
            int cnt=sz[ son ]*sz[ son ];
            int e=hh[ rt ]-cnt+mod;
            e%=mod;
            jian+=cnt*e;
            jian%=mod;
        }
    }

    ans=ans-jian+mod;
    ans%=mod;

//    for( int i=1; i<=n; i++ ){
//        cout<<sz[ i ]<<" ";
//    }
//    cout<<endl;
//
//    for( int i=1; i<=n; i++ ){
//        cout<<sum[ i ]<<" ";
//    }
//    cout<<endl;

//    ans*=2; ans%=mod;

//    deg( ans );
    int inv=( ( n )*( n-1 ) )/2;
    inv%=mod;

    inv*=inv;
    inv%=mod;

    inv=getinv( inv , mod );

    ans*=inv;
    ans%=mod;

    cout<<ans<<endl;

//    ans=918384806;
//    ans*=100;
//    ans%=mod;
//    deg( ans );
}

signed main() {
    ios::sync_with_stdio( 0 );
    cin.tie( 0 ); cout.tie( 0 );

    int T=1;
    cin>>T;

    while( T-- ){
        solve();

    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5632kb

input:

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

output:

443664158
918384806

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3600kb

input:

1000
7
3 6
4 3
5 3
2 6
1 4
7 1
12
5 7
10 7
2 10
11 2
1 7
8 1
4 2
9 11
6 9
12 11
3 5
6
2 5
1 2
4 5
6 4
3 6
5
2 5
1 5
4 5
3 2
8
1 8
2 8
4 2
6 1
5 6
7 6
3 8
8
3 8
7 3
4 8
6 4
2 7
5 2
1 4
4
3 1
4 3
2 1
6
5 1
6 1
2 5
3 5
4 2
12
8 11
5 11
12 8
3 12
6 12
2 3
4 6
10 11
1 5
9 5
7 5
9
6 1
7 6
4 7
8 7
5 4
9 6
...

output:

948445317
941869675
550143557
918384806
171891568
487662742
776412276
869581749
350393854
663955737
211048577
887328316
890334966
829120312
528946270
554580198
8020790
122234004
908525604
807222287
433351719
56023919
660874735
183319566
698771049
317458206
333734040
599710576
310564911
609298777
730...

result:

wrong answer 2nd lines differ - expected: '468414020', found: '941869675'