比赛 CSP2023-S模拟赛 评测结果 AAAAAAEEEE
题目名称 编辑题目 最终得分 60
用户昵称 usr10086 运行时间 1.941 s
代码语言 C++ 内存使用 7.47 MiB
提交时间 2023-10-17 21:43:56
显示代码纯文本
#include <bits/stdc++.h>

using namespace std;

ifstream fin("edit.in");
ofstream fout("edit.out");
#define cin fin
#define cout fout

const int N = 2e5 + 10;

typedef long long ll;
const ll MOD = 998244353;

ll inv(ll x)
{
    ll res = 1, e = MOD - 2;
    while (e)
    {
        if (e & 1) (res *= x) %= MOD;
        (x *= x) %= MOD, e >>= 1;
    }
    return res;
}

struct E
{
    int id, to, w;
};

int n, m;
bool vis[N];
vector<E> g[N];
struct F
{
    int id, u, v, w;
};
vector<F> el;

int main()
{
    cin >> n >> m;
    el.push_back({0,0,0});
    for (int i = 1, u, v, w; i <= m; i++) cin >> u >> v >> w, el.push_back({i, u, v, w}), g[u].push_back({i, v, w}), g[v].push_back({i, u, w});
    ll ans = 0;
    ll dvd = inv(n)*inv(m)%MOD;
    for (int i = 1; i <= m; i++)
    {
        int u = el[i].u, v = el[i].v, w = el[i].w;
        queue<int> q;
        memset(vis, 0, sizeof(bool)*(n+1));
        q.push(u);
        int hasu = 0, szu = 0, hasv = 0, szv = 0;
        vis[u] = 1;
        while (!q.empty())
        {
            int k = q.front(); q.pop();
            szu++;
            for (const E& e : g[k])
            {
                if (e.id == i) continue;
                if (e.w == w) hasu = 1;
                if (!vis[e.to]) vis[e.to] = 1, q.push(e.to);
            }
        }
        if (!vis[v]) q.push(v), vis[v] = 1;
        while (!q.empty())
        {
            int k = q.front(); q.pop();
            szv++;
            for (const E& e : g[k])
            {
                if (e.id == i) continue;
                if (e.w == w) hasv = 1;
                if (!vis[e.to]) vis[e.to] = 1, q.push(e.to);
            }
        }
//        printf("added %lld/%d\n", ll(szu*hasu+szv*hasv), n*m);
        (ans += ll(szu*hasu+szv*hasv)*dvd) %= MOD;
    }
    cout << ans;
}