QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#677739#9530. A Game On Treeucup-team008#AC ✓236ms21184kbC++178.8kb2024-10-26 13:20:552024-10-26 13:20:56

Judging History

This is the latest submission verdict.

  • [2024-10-26 13:20:56]
  • Judged
  • Verdict: AC
  • Time: 236ms
  • Memory: 21184kb
  • [2024-10-26 13:20:55]
  • Submitted

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;

struct barrett_reduction {
    unsigned mod;
    uint64_t div;
 
    barrett_reduction(unsigned m) : mod(m), div(-1LLU / m) {}
 
    unsigned operator()(uint64_t a) const {
#ifdef __SIZEOF_INT128__
        uint64_t q = uint64_t(__uint128_t(div) * a >> 64);
        uint64_t r = a - q * mod;
        return unsigned(r < mod ? r : r - mod);
#endif
        return unsigned(a % mod);
    }
};
 
template<const int &MOD, const barrett_reduction &barrett>
struct _b_int {
    int val;
 
    _b_int(int64_t v = 0) {
        if (v < 0) v = v % MOD + MOD;
        if (v >= MOD) v %= MOD;
        val = int(v);
    }
 
    _b_int(uint64_t v) {
        if (v >= uint64_t(MOD)) v %= MOD;
        val = int(v);
    }
 
    _b_int(int v) : _b_int(int64_t(v)) {}
    _b_int(unsigned v) : _b_int(uint64_t(v)) {}
 
    static int inv_mod(int a, int m = MOD) {
        // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
        int g = m, r = a, x = 0, y = 1;
 
        while (r != 0) {
            int q = g / r;
            g %= r; swap(g, r);
            x -= q * y; swap(x, y);
        }
 
        return x < 0 ? x + m : x;
    }
 
    explicit operator int() const { return val; }
    explicit operator unsigned() const { return val; }
    explicit operator int64_t() const { return val; }
    explicit operator uint64_t() const { return val; }
    explicit operator double() const { return val; }
    explicit operator long double() const { return val; }
 
    _b_int& operator+=(const _b_int &other) {
        val -= MOD - other.val;
        if (val < 0) val += MOD;
        return *this;
    }
 
    _b_int& operator-=(const _b_int &other) {
        val -= other.val;
        if (val < 0) val += MOD;
        return *this;
    }
 
    static unsigned fast_mod(uint64_t x) {
#if !defined(_WIN32) || defined(_WIN64)
        return barrett(x);
#endif
        // Optimized mod for Codeforces 32-bit machines.
        // x must be less than 2^32 * MOD for this to work, so that x / MOD fits in an unsigned 32-bit int.
        unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
        unsigned quot, rem;
        asm("divl %4\n"
            : "=a" (quot), "=d" (rem)
            : "d" (x_high), "a" (x_low), "r" (MOD));
        return rem;
    }
 
    _b_int& operator*=(const _b_int &other) {
        val = fast_mod(uint64_t(val) * other.val);
        return *this;
    }
 
    _b_int& operator/=(const _b_int &other) {
        return *this *= other.inv();
    }
 
    friend _b_int operator+(const _b_int &a, const _b_int &b) { return _b_int(a) += b; }
    friend _b_int operator-(const _b_int &a, const _b_int &b) { return _b_int(a) -= b; }
    friend _b_int operator*(const _b_int &a, const _b_int &b) { return _b_int(a) *= b; }
    friend _b_int operator/(const _b_int &a, const _b_int &b) { return _b_int(a) /= b; }
 
    _b_int& operator++() {
        val = val == MOD - 1 ? 0 : val + 1;
        return *this;
    }
 
    _b_int& operator--() {
        val = val == 0 ? MOD - 1 : val - 1;
        return *this;
    }
 
    _b_int operator++(int) { _b_int before = *this; ++*this; return before; }
    _b_int operator--(int) { _b_int before = *this; --*this; return before; }
 
    _b_int operator-() const {
        return val == 0 ? 0 : MOD - val;
    }
 
    friend bool operator==(const _b_int &a, const _b_int &b) { return a.val == b.val; }
    friend bool operator!=(const _b_int &a, const _b_int &b) { return a.val != b.val; }
    friend bool operator<(const _b_int &a, const _b_int &b) { return a.val < b.val; }
    friend bool operator>(const _b_int &a, const _b_int &b) { return a.val > b.val; }
    friend bool operator<=(const _b_int &a, const _b_int &b) { return a.val <= b.val; }
    friend bool operator>=(const _b_int &a, const _b_int &b) { return a.val >= b.val; }
 
    _b_int inv() const {
        return inv_mod(val);
    }
 
    _b_int pow(int64_t p) const {
        if (p < 0)
            return inv().pow(-p);
 
        _b_int a = *this, result = 1;
 
        while (p > 0) {
            if (p & 1)
                result *= a;
 
            p >>= 1;
 
            if (p > 0)
                a *= a;
        }
 
        return result;
    }
 
    friend ostream& operator<<(ostream &os, const _b_int &m) {
        return os << m.val;
    }
 
    friend istream& operator>>(istream &is, _b_int &m) {
        int64_t x;
        is >> x;
        m = x;
        return is;
    }
};
 
int MOD = 998244353;
barrett_reduction barrett(MOD);
using mnum = _b_int<MOD, barrett>;

void rsolve() {
  int n;
  cin >> n;
  vector<vector<int>> edges(n);
  for(int i = 0; i < n-1; i++) {
    int a, b;
    cin >> a >> b; a--; b--;
    edges[a].pb(b);
    edges[b].pb(a);
  }
  vector<mnum> treesz(n);
  vector<mnum> sumtreeszsq(n);
  vector<mnum> sumtreesz(n); // sum of all subtree sizes, including self
  vector<mnum> sumfnminusf(n);
  mnum ret = 0;
  auto dfs = y_combinator([&](auto self, int curr, int par) -> void {
    treesz[curr] = 1;
    mnum inner = 0;
    mnum innersq = 0;
    for(int out: edges[curr]) {
      if(out == par) continue;
      self(out, curr);
      inner += sumtreeszsq[curr] * sumtreeszsq[out];
      mnum innercand = 2 * sumtreeszsq[out] - treesz[out] * treesz[out];
      innercand *= (n - treesz[out]) * (n - treesz[out]);
      innersq += innercand; 
      sumtreesz[curr] += sumtreesz[out];
      sumtreeszsq[curr] += sumtreeszsq[out];
      sumfnminusf[curr] += sumfnminusf[out];
      treesz[curr] += treesz[out];
    }
    inner *= 2;
    ret += inner;
    ret += innersq;
    sumtreesz[curr] += treesz[curr];
    sumtreeszsq[curr] += treesz[curr] * treesz[curr];
    sumfnminusf[curr] += treesz[curr] * (n - treesz[curr]);
  });
  dfs(0, -1);
  mnum denom = n;
  denom *= n-1;
  denom /= 2;
  denom *= denom;
  cout << ret/denom << "\n";
}
void solve() {
  int t;
  cin >> t;
while(t--) rsolve();
}

// what would chika do
// are there edge cases?
// did you actually sort the thing instead of just thinking it?
// integer overflow?

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

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

详细

Test #1:

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

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: 2ms
memory: 3596kb

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: 16ms
memory: 3572kb

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: 221ms
memory: 20564kb

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: 225ms
memory: 20536kb

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: 232ms
memory: 20912kb

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: 236ms
memory: 19220kb

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: 235ms
memory: 21184kb

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: 227ms
memory: 20112kb

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: 231ms
memory: 19296kb

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: 235ms
memory: 18596kb

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: 0ms
memory: 3520kb

input:

1
2
1 2

output:

1

result:

ok single line: '1'

Extra Test:

score: 0
Extra Test Passed