QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#687145#9530. A Game On TreeicealsoheatAC ✓536ms23904kbC++206.0kb2024-10-29 17:24:352024-10-29 17:24:44

Judging History

This is the latest submission verdict.

  • [2024-10-29 17:24:44]
  • Judged
  • Verdict: AC
  • Time: 536ms
  • Memory: 23904kb
  • [2024-10-29 17:24:35]
  • Submitted

answer

#pragma GCC optimize(3)  //O2优化开启
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int mod=998244353;
const int MX=0x3f3f3f3f3f3f3f3f; 
//inline int read()                     //快读
//{
//    int xr=0,F=1; char cr;
//    while(cr=getchar(),cr<'0'||cr>'9') if(cr=='-') F=-1;
//    while(cr>='0'&&cr<='9')
//        xr=(xr<<3)+(xr<<1)+(cr^48),cr=getchar();
//    return xr*F;
//}
//void write(int x)                     //快写
//{
//    if(x<0) putchar('-'),x=-x;
//    if(x>9) write(x/10); putchar(x%10+'0');
//}
// 比 unordered_map 更快的哈希表
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
//     int operator()(int x) const { return x ^ RANDOM; }
// };
// typedef gp_hash_table<int, int, chash> hash_t;

template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(ll x) : x{norm(x % getMod())} {}

    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt power(MInt a, ll b) {
        MInt res = 1;
        for (; b; b /= 2, a *= a) {
            if (b % 2) {
                res *= a;
            }
        }
        return res;
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        ll v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;            //这里为自动模数
using mint = MInt<P>;           //mint是其定理的类型

vector<int>ve[300005];

int n;

mint dp[300005];

int sz[300005];

mint ans;

void dfs(int x,int fa){

    sz[x]=1;

    dp[x]=0;

    for(auto i:ve[x]){

        if(i==fa)continue;

        dfs(i,x);

        sz[x]+=sz[i];

        dp[x]+=sz[i]*sz[i]+dp[i];

    }

    for(auto i:ve[x]){

        if(i==fa)continue;

        mint res=(sz[i]*(n-sz[i]));

        res*=res;

        ans+=res;

    }

}

void dfss(int x,int fa,mint res){

    for(int i:ve[x]){
        // if(x==1){
        //     cout<<res<<"+++\n";
        // }
        if(i==fa)continue;
        ans+=res*sz[i]*sz[i]*2;
        dfss(i,x,res);
        res+=dp[i]+sz[i]*sz[i];
        mint u=n-sz[i];
        u=u*u;
        ans+=u*dp[i]*2;

    }

}

void icealsoheat(){

    cin>>n;

    ans=0;

    for(int i=1;i<=n;i++)ve[i].clear();

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

        int l,r;
        cin>>l>>r;

        ve[l].push_back(r);
        ve[r].push_back(l);

    }

    dfs(1,0);

    // cout<<ans<<"+++\n";

    dfss(1,0,0);

    // cout<<ans<<"\n";
    mint cnt=(n*(n-1)/2);
    cnt=cnt*cnt;
    // ans=10;
    // mint bi=cnt*918384806;
    // cout<<bi<<"---\n";
    ans=ans*cnt.inv();

    cout<<ans<<"\n";

}
signed main(){
    ios::sync_with_stdio(false);          //int128不能用快读!!!!!!
    cin.tie();
    cout.tie();
    int _yq;
    _yq=1;
    cin>>_yq;
    while(_yq--){
        icealsoheat();
    }
}
//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 7732kb

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: 0
Accepted
time: 0ms
memory: 7748kb

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
468414020
550143557
918384806
711758412
487662742
776412276
869581749
240852807
765628773
211048577
887328316
890334966
940494682
760637552
908032643
592850815
584006902
908525604
221832080
433351719
56023919
867301808
183319566
698771049
366957926
449579681
599710576
310564911
286902823
3...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 10ms
memory: 7828kb

input:

1000
94
59 1
33 59
73 1
6 33
83 59
4 59
20 59
61 6
39 1
76 73
71 6
44 39
9 71
24 4
87 9
57 83
2 9
81 71
82 20
90 2
85 39
12 9
30 83
66 30
53 9
47 9
36 44
43 53
29 12
31 53
64 81
38 31
84 82
77 38
23 71
93 84
78 83
58 31
68 90
42 1
55 64
13 78
70 78
62 24
19 55
92 87
14 57
10 84
65 81
63 6
75 36
91 1...

output:

508107725
996793960
201633249
335988372
842755864
460619380
342223697
207523414
429241811
391691799
542977964
786416604
454278948
685531402
25914978
440729774
228518323
679471537
82764520
554190841
432505337
143444089
189106586
337234245
61954935
905141094
532919674
703954588
185671863
942858630
692...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 449ms
memory: 23080kb

input:

10000
8
1 4
3 1
5 1
7 3
8 4
6 8
2 7
8
2 6
4 6
5 6
8 5
7 6
3 5
1 7
8
8 5
6 5
2 5
7 2
1 6
3 1
4 8
9
8 6
9 8
3 6
1 8
5 9
2 8
4 3
7 9
8
8 6
3 6
5 8
1 6
4 3
7 6
2 6
9
9 5
7 5
2 7
8 7
4 9
3 7
6 3
1 4
8
1 4
5 1
6 5
3 4
8 4
7 8
2 5
9
1 8
6 1
2 1
3 8
5 3
9 8
7 8
4 8
9
4 9
2 9
1 2
3 4
5 2
6 9
8 3
7 2
8
1 2
8 ...

output:

49657566
56023919
387074343
97051536
701572244
211048577
711758412
308100110
761007271
711758412
178698065
285212675
80216065
43380497
267677376
818005792
53239701
765628773
970145625
387074343
436731906
422725927
479157293
977872021
436731906
925779210
487662742
705549251
267677376
711758412
526851...

result:

ok 10000 lines

Test #5:

score: 0
Accepted
time: 418ms
memory: 23904kb

input:

10000
8
7 6
8 6
5 7
4 6
1 4
2 5
3 5
10
10 7
8 7
9 8
2 8
6 7
1 7
5 9
4 1
3 6
10
2 6
3 6
5 6
7 2
1 3
4 5
8 5
9 5
10 4
10
6 5
2 5
4 6
8 5
10 5
9 5
1 8
3 6
7 1
8
5 2
3 5
6 5
1 2
8 2
4 1
7 5
9
5 1
3 1
7 5
9 7
6 3
8 6
2 1
4 9
9
9 8
4 8
3 8
6 9
2 8
7 6
1 2
5 6
9
2 5
8 5
7 8
9 7
1 8
4 8
6 9
3 1
8
6 7
8 6
2 ...

output:

711758412
286902823
691130166
841483019
650641410
887328317
331207619
733278261
56023919
977872021
414394648
183319566
239374924
696059768
855285904
761007271
711758412
86268032
599710576
728310932
178698065
178698065
422725927
219002589
178698065
202450068
599710576
56023919
449579681
760637552
925...

result:

ok 10000 lines

Test #6:

score: 0
Accepted
time: 478ms
memory: 23488kb

input:

10000
9
8 1
2 8
4 1
3 2
7 1
6 7
9 3
5 1
9
7 5
1 7
3 7
9 5
2 3
8 1
6 8
4 6
9
7 8
4 7
5 4
3 4
6 8
1 3
9 8
2 7
9
8 7
2 8
9 8
5 8
1 2
3 7
6 3
4 7
8
6 8
4 6
2 4
5 8
7 5
1 6
3 7
9
9 8
7 8
2 9
5 9
1 5
3 1
6 2
4 8
10
2 10
4 10
6 4
1 10
9 6
8 9
5 10
7 4
3 2
10
3 9
5 3
4 3
7 9
6 3
10 6
2 9
8 5
1 2
10
8 5
2 8
...

output:

211048577
354315128
178698065
705549251
285212675
138645051
449579681
286902823
925779210
294297225
519087065
368179632
422725927
603876215
539175192
867301808
977540027
669439919
211048577
701572244
977872021
138645051
267677376
855285904
977872021
286902823
925286249
705549251
219002589
331207619
...

result:

ok 10000 lines

Test #7:

score: 0
Accepted
time: 536ms
memory: 21992kb

input:

10000
8
4 2
6 2
1 6
5 1
3 1
7 5
8 5
8
4 3
8 4
6 8
2 3
5 8
1 4
7 5
9
6 1
8 6
7 8
5 6
4 1
9 6
3 1
2 5
8
3 2
5 2
7 2
8 2
1 7
4 3
6 8
10
10 2
7 10
3 7
8 7
5 10
1 5
4 3
6 4
9 7
8
5 4
8 4
2 5
7 4
6 8
1 7
3 1
9
3 1
8 1
5 1
6 5
2 6
9 5
7 5
4 2
10
1 3
6 1
2 3
7 3
8 7
9 1
10 7
5 7
4 10
10
3 2
10 3
5 10
9 3
1 ...

output:

422725927
977872021
867301808
407446676
466833287
387074343
97051536
292325385
301691628
765628773
285212675
711758412
650641410
178698065
543242114
286902823
473241769
109930120
841975980
836553418
422725927
286902823
414394648
739440264
436731906
56023919
436731906
530918109
603876215
977872021
40...

result:

ok 10000 lines

Test #8:

score: 0
Accepted
time: 517ms
memory: 23880kb

input:

10000
9
9 3
7 9
2 3
5 2
6 2
1 9
4 5
8 9
10
4 6
9 4
8 6
5 4
10 5
7 8
2 8
3 7
1 2
10
5 4
1 4
3 4
9 3
6 3
7 9
8 7
2 4
10 7
10
3 8
4 3
10 8
6 10
9 4
5 6
2 8
1 2
7 5
10
10 9
4 9
6 10
5 10
2 6
1 10
3 1
8 3
7 10
10
9 7
10 7
2 7
3 10
1 3
4 3
8 9
6 3
5 6
9
2 4
7 4
5 7
9 5
3 5
6 7
8 7
1 2
9
2 4
1 4
9 2
7 2
8 ...

output:

409773147
306621231
836553418
760637552
519087065
304649390
97051536
742521264
387074343
855285904
874737082
358875008
733278261
698524570
908525604
387074343
970145625
449579681
286902823
239374924
650641410
691130166
765628773
603876215
839572800
977872021
742521264
908032643
874737082
299719788
7...

result:

ok 10000 lines

Test #9:

score: 0
Accepted
time: 508ms
memory: 23124kb

input:

10000
9
8 4
5 4
6 4
3 5
7 4
9 6
1 9
2 9
10
3 9
10 3
2 10
6 2
1 6
8 2
4 1
5 3
7 9
9
9 3
7 9
4 3
5 3
2 3
6 5
1 6
8 9
8
6 1
8 1
5 6
7 6
4 8
3 6
2 7
8
2 3
7 2
8 2
6 8
5 2
1 3
4 2
10
2 7
8 7
9 2
5 8
10 8
3 7
1 7
4 8
6 7
9
3 8
4 3
5 3
2 8
9 2
1 5
6 8
7 4
9
5 1
6 5
4 6
9 6
3 4
2 9
8 1
7 4
8
2 4
8 4
3 2
5 3...

output:

331207619
28098733
97051536
599710576
701572244
277043619
368179632
138645051
711758412
626059423
86268032
414394648
368179632
993314752
321410036
530918109
711758412
712327454
603876215
49657566
705549251
765628773
56023919
299719788
887328316
839572800
650641410
211048577
286902823
908032643
28690...

result:

ok 10000 lines

Test #10:

score: 0
Accepted
time: 506ms
memory: 22704kb

input:

10000
10
4 9
7 4
2 4
5 4
6 7
10 2
3 9
8 10
1 8
8
2 4
3 2
5 2
6 4
1 4
8 3
7 1
8
8 7
6 7
4 8
5 4
1 6
2 5
3 4
8
7 2
3 2
5 3
1 5
8 1
4 7
6 2
9
5 9
6 9
4 6
7 9
8 4
3 6
1 6
2 5
10
7 1
4 7
2 7
5 2
8 5
3 2
6 7
10 4
9 2
8
6 2
3 6
7 2
8 3
5 6
4 5
1 2
10
2 8
5 8
10 8
4 10
7 8
3 2
1 10
6 4
9 10
10
3 5
6 5
10 5
...

output:

440213438
977872021
285212675
285212675
705549251
267677376
436731906
267677376
440213438
712327454
711758412
191268549
321410036
436731906
839572800
49657566
519087065
178698065
977872021
285212675
574298605
368179632
466833287
696059768
86268033
308100110
487662742
887328317
977872021
701572244
99...

result:

ok 10000 lines

Test #11:

score: 0
Accepted
time: 517ms
memory: 22652kb

input:

10000
8
1 3
8 3
2 3
6 8
5 8
4 1
7 1
10
5 7
10 7
2 5
4 5
8 10
6 7
1 5
3 2
9 8
9
4 7
2 4
1 4
5 4
9 1
3 2
6 4
8 9
8
5 7
3 5
2 5
6 5
4 7
8 3
1 7
9
1 5
9 5
3 1
7 3
8 3
6 3
4 1
2 8
8
1 2
4 2
6 2
7 2
8 7
3 8
5 6
9
4 5
3 4
6 4
7 3
1 5
9 3
2 1
8 7
9
1 6
3 1
2 3
5 2
4 3
8 5
9 5
7 1
8
5 3
7 3
4 3
8 7
1 3
6 1
2...

output:

202450068
449579681
742521264
56023919
705549251
599710576
765628773
887328316
599710576
97051536
286902823
603876215
321410036
221832080
294297225
479157293
650641410
765628773
908525604
285212675
125704848
414394648
599254713
286902823
707938599
13864507
599710576
304649390
691130166
56023919
7656...

result:

ok 10000 lines

Test #12:

score: 0
Accepted
time: 2ms
memory: 7688kb

input:

1
2
1 2

output:

1

result:

ok single line: '1'

Extra Test:

score: 0
Extra Test Passed