QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#49541#4540. Counting StickmenCDsmen#AC ✓268ms16052kbC++2.7kb2022-09-21 21:48:372022-09-21 21:48:40

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-21 21:48:40]
  • Judged
  • Verdict: AC
  • Time: 268ms
  • Memory: 16052kb
  • [2022-09-21 21:48:37]
  • Submitted

answer

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long llint;

inline int read()
{
    int ans = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ans = ans * 10 + ch - '0';
        ch = getchar();
    }
    return ans * w;
}
const llint mods = 998244353;
llint qpow(llint a,llint b){
    llint ans = 1;
    while(b){
        if(b&1)
            ans = ans * a % mods;
        a = a * a % mods;
        b >>= 1;
    }
    return ans;
}

llint inv2;
llint ans = 0;
int n;
int in[500005];
int head[500005], tot;
struct D
{
    int to, next;
} edge[1000005];
inline void init()
{
    memset(head, 0, sizeof(int) * (n + 1));
    memset(in, 0xff, sizeof(int) * (n + 1));
    tot = 1;
    ans = 0;
}
inline void adde(int u, int v)
{
    edge[tot] = {v, head[u]};
    head[u] = tot++;
}

int tmpin[500005], tmptot;

void dfs(int u, int fa)
{

    llint sum = 0;
    for (int i = head[u]; i; i = edge[i].next)
    {
        int v = edge[i].to;
        
        if (fa == v)
            continue;
        dfs(v, u);
    }

    if(in[u]<3)
        return;

    

    tmptot = 0;
    for (int i = head[u]; i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (in[v])
        {
            sum = (sum + in[v]) % mods;
            tmpin[tmptot++] = in[v];
        }
    }

    

    llint tmp = sum * (sum - 1) % mods;
    //cout << tmp << endl;
    for (int i = 0; i < tmptot; i++)
    {
        tmp -= tmpin[i] * (tmpin[i] - 1) % mods;
        tmp = (tmp % mods + mods) % mods;
    }
    tmp = tmp * inv2 % mods;
    //cout << tmp << endl;

    for (int i = head[u]; i; i = edge[i].next)
    {
        int x = edge[i].to;
        if(in[x]<2) continue;
        llint C = in[x] * (in[x] - 1) % mods *inv2 % mods;
        llint ans1 = ((tmp - in[x]*(sum - in[x])%mods)%mods+mods)%mods;
        ans += C * ans1 % mods * (in[u] - 2);
        //cout << C << ' ' << ans1 << ' ' << (in[u] - 2) << endl;
        ans %= mods;
    }
}

inline void solve()
{
    n = read();
    init();
    for (int i = 1; i < n; i++)
    {
        int u = read(), v = read();
        adde(u, v);
        adde(v, u);
        in[u]++;
        in[v]++;
    }
    dfs(1, 0);
    printf("%lld\n", ans);
}

int main()
{
    // IO;
    //  init();
    inv2 = qpow(2, mods - 2);
    int tt = 1;
    tt = read();
    //   scanf("%d", &tt);
    while (tt--)
    {
        solve();
        // cout << tt << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 268ms
memory: 16052kb

input:

15
9
1 2
2 3
3 4
2 5
5 6
2 7
7 8
7 9
9999
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1...

output:

1
311255965
672169659
323323769
834196165
147532751
608867683
433545054
268282647
580524749
250163191
0
876200211
781358751
903681473

result:

ok 15 lines