QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#685551#9530. A Game On TreefgzWA 2ms3596kbC++233.7kb2024-10-28 20:05:222024-10-28 20:05:23

Judging History

This is the latest submission verdict.

  • [2024-10-28 20:05:23]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 3596kb
  • [2024-10-28 20:05:22]
  • Submitted

answer

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0), cin.tie(0)
#define time chrono::system_clock::now().time_since_epoch().count()
using namespace std;
#define int long long
using ll = long long;
using pll = pair<int, int>;

const int mod = 998244353, N = 2e5 + 10;

int qmi(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

using int64 = long long;
template<const int T>
struct ModInt {
    const static int mod = T;
    int x;
    ModInt(int x = 0) : x(x % mod) {}
    int val() { return x; }
    ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
    ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
    bool operator == (const ModInt &a) const { return x == a.x; };
    bool operator != (const ModInt &a) const { return x != a.x; };
    void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
    void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
    void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
    void operator /= (const ModInt &a) { *this = *this / a; }
    friend ModInt operator + (int y, const ModInt &a){ int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    friend ModInt operator - (int y, const ModInt &a){ int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    friend ModInt operator * (int y, const ModInt &a){ return ModInt(1LL * y * a.x % mod);}
    friend ModInt operator / (int y, const ModInt &a){ return ModInt(y) / a;}
    friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x;}
    friend istream &operator>>(istream &is, ModInt &t){return is >> t.x;}

    ModInt pow(int64 n) const {
        ModInt res(1), mul(x);
        while(n){
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    
    ModInt inv() const {
        int a = x, b = mod, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        if (u < 0) u += mod;
        return u;
    }
    
};
using mint = ModInt<mod>;

void solve(){
    int n; cin >> n;
    vector edg(n + 10, vector<int>());
    for (int i = 1; i < n; i ++) {
        int u, v; cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    vector<mint> sz(n + 10, 0);
    vector<mint> sum(n + 10, 0), tot(n + 10, 0);
    mint res = 0;
    function<void(int, int)> dfs1 = [&] (int u, int p) {
        sz[u] = 1;
        for (auto v : edg[u]) {
            if (v == p) continue;
            dfs1(v, u);
            sz[u] += sz[v];
            sum[u] = sum[u] + sz[v] * sz[v];
            tot[u] += sum[v];
        }
        sum[u] = sum[u] + sz[u] * sz[u];
    };
    function<void(int, int)> dfs2 = [&] (int u, int p) {
        for (auto v : edg[u]) {
            if (p == v) continue;
            res += sz[v] * sz[v] * (n - sz[v]) * (n - sz[v]);
            res += 2 * (n - sz[v]) * (n - sz[v]) * (sum[v] - sz[v] * sz[v]);
            res += sum[v] * (tot[u] - sum[v]);
            dfs2(v, u);
        }
    };
    dfs1(1, 0);
    dfs2(1, 0);
    int t = n * n % mod * (n - 1) % mod * (n - 1) % mod * qmi(4, mod - 2) % mod;
    res = res * qmi(t, mod - 2);
    cout << res << '\n';
}

signed main()
{
    ios;
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

详细

Test #1:

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

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

934863761
939119690
381551177
259543533
495302366
760142705
776412276
292818345
628600613
203346074
791817282
243399088
247498602
616913179
298240908
198662952
507601297
584006902
38943856
154050056
359102138
109501295
816465290
98592036
758665710
166649059
961765302
589524409
310564911
431340154
81...

result:

wrong answer 1st lines differ - expected: '948445317', found: '934863761'