結果

問題 No.1784 Not a star yet...
ユーザー Jerry Rowe (cooluo)
提出日時 2025-06-08 23:02:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 8,601 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 202,984 KB
実行使用メモリ 38,860 KB
最終ジャッジ日時 2025-06-08 23:02:34
合計ジャッジ時間 5,835 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;

#define ll long long
#define ul unsigned ll
#define LL __int128_t
#define UL __uint128_t
#define db double
#define DB long db
#define pii pair<int, int>
#define fi first
#define se second
#define mkpr make_pair
#define vi vector<int>
#define vmi vector<mint>
#define vii vector<pii>
#define rsz resize
#define ep emplace
#define pb pop_back
#define eb emplace_back
#define all(c) (c).begin(), (c).end()
#define disc(c) (sort(all(c)), (c).rsz(unique(all(c)) - (c).begin()))
#define ers(S, x) ((S).erase((S).find(x)))
#define bit(k) (1u << (k))
#define Bit(k) (1ull << (k))
#define BIT(k) ((UL)1 << (k))
#define lowbit(x) ((x) & -(x))
#define bin(s, k) ((s) >> (k) & 1)
#define lg2(x) (31 - __builtin_clz(x))
#define LG2(x) (63 - __builtin_clzll(x))
#define highbit(x) bit(lg2(x))
#define highbitll(x) Bit(LG2(x))
#define popcnt(x) __builtin_popcount(x)
#define popcntll(x) __builtin_popcountll(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define req(i, l, r) for (int i(l), i##End(r); i < i##End; i = -~i)
#define qer(i, r, l) for (int i(r), i##End(l); i > i##End; i = ~-i)
#define rep(i, l, r) for (int i(l), i##End(r); i <= i##End; i = -~i)
#define per(i, r, l) for (int i(r), i##End(l); i >= i##End; i = ~-i)

// #define FILERR

#ifdef JYR
#include "debug.h"
#define errm(x, ...) fprintf(stderr, x, ##__VA_ARGS__)
#define errs(x, ...) errm(x "\n", ##__VA_ARGS__)
#else
#define dbg(...) (__VA_ARGS__)
#define dbgArr(...) (__VA_ARGS__)
#define errm(x, ...) (x, ##__VA_ARGS__)
#define errs(x, ...) (x, ##__VA_ARGS__)
#endif

#define __

template<typename T, typename U> void chkmx(T &_a, U _b) { if (_a < _b) _a = _b; }
template<typename T, typename U> void chkmn(T &_a, U _b) { if (_b < _a) _a = _b; }
template<typename T, typename U> T OxO(T _a, U _b) { return _a >= _b ? -1 : _a; }

bool Mbe;

struct FastIO {
    char buf[1 << 20], *p1, *p2;
    char puf[1 << 20], *pf;
    
    FastIO() : p1(buf), p2(buf), pf(puf) {}
    ~FastIO() { fwrite(puf, 1, pf - puf, stdout); }
    
    char gc() {
        if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin);
        return p1 == p2 ? EOF : *p1++;
    }
    
    bool blank(char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }
    
    char rd() {
        char c = gc(); while (blank(c)) c = gc();
        return c;
    }
    
    template<typename T> T rd() {
        T x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        return f ? -x : x;
    }
    
    int rds(char *s) {
        char c = gc(), *S = s;
        while (blank(c)) c = gc();
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }
    
    int rdl(char *s) {
        char c = gc(), *S = s;
        while (c == '\r' || c == '\n') c = gc();
        while (c != '\r' && c != '\n' && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }
    
    void rd(char &c) { c = rd(); }
    
    void rd(char *s) {
        char c = gc();
        while (blank(c)) c = gc();
        if (c == EOF) { *s = 0; return; }
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        *s = 0;
    }
    
    void rd(string &s) {
        char c = gc(); s = "";
        while (blank(c)) c = gc();
        if (c == EOF) return;
        while (!blank(c) && c != EOF) s += c, c = gc();
    }
    
    template<typename T> void rd(T &x) {
        x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        if (f) x = -x;
    }
    
    template<typename T, typename... Ts>
    void rd(T& x, Ts&... xs) { rd(x), rd(xs...); }
    
    template<typename T>
    void rda(T* x, int l, int r) { rep(i, l, r) rd(x[i]); }
    
    template<typename T>
    void rda(T* x, int n) { rep(i, 1, n) rd(x[i]); }
    
    void pc(const char &c) {
        if (pf - puf == 1 << 20) fwrite(pf = puf, 1, 1 << 20, stdout);
        *pf++ = c;
    }
    
    void prt(char c) { pc(c); }
    void prt(char* s) { while (*s) pc(*s++); }
    void prt(const char* s) { while (*s) pc(*s++); }
    void prt(bool b) { pc(b ? '1' : '0'); }
    void prt(string s) { for (auto &&c : s) pc(c); }
    
    template<typename T> void prt(T x) {
        static int st[41], tp = 0;
        if (x == 0) { pc('0'); return; }
        if (x < 0) x = -x, pc('-');
        while (x) st[++tp] = x % 10, x /= 10;
        while (tp) pc(st[tp--] + '0');
    }
    
    template<typename T> void prt(T *x) { while (*x) pc(*x++); }
    
    template<typename T, typename... Ts>
    void prt(T x, Ts... xs) { prt(x), prt(xs...); }
    
    template<typename T> void prb(T x) { prt(x, ' '); }
    
    template<typename T> void prt(vector<T> vt) { for (auto x : vt) prb(x); }
    
    template<typename T, typename... Ts>
    void prb(T x, Ts... xs) { prt(x, ' '), prb(xs...); }
    
    template<typename T> void prd(T x) { prt(x, '\n'); }
    
    template<typename T, typename... Ts>
    void prd(T x, Ts... xs) { prt(x, ' '), prd(xs...); }
} IO;

#define rd IO.rd
#define rda IO.rda
#define rds IO.rds
#define rdl IO.rdl
#define ri rd<int>()
#define rl rd<ll>()
#define prt IO.prt
#define prs(...) prt(__VA_ARGS__, '\n')
#define prb IO.prb
#define prd IO.prd
#define edl IO.pc('\n')

// #define MC

#define N 255
#define mod 998244353
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f

ll qpow(ll a, ll b = mod - 2, ll P = mod) {
    ll ml = 1, bs = a % P;
    while (b) {
        if (b & 1) (ml *= bs) %= P;
        (bs *= bs) %= P, b >>= 1;
    }
    return ml;
}

int n;
ll ans;
int d0[N], X;
int d1[N], Y;
ll c[N][N][N];
ll a[N][N];
ll f[N][N];

int id(int i, int j) { return i * (Y + 1) + j; }

void Gauss() {
    rep(i, 0, Y) {
        int p = i;
        while (a[p][i] == 0) p++;
        if (i != p) swap(a[i], a[p]);
        rep(j, 0, Y) if (i != j) {
            ll x = a[j][i] * qpow(a[i][i]) % mod;
            rep(k, i, Y + 1) (a[j][k] += mod - a[i][k] * x % mod) %= mod;
        }
    }
}

void add(ll z[], int I, int J, ll p) {
    rep(k, 0, Y + 1) (z[k] += c[I][J][k] * p) %= mod;
}

void mslv() {
    req(i, 1, n = ri) {
        int u, v, w; rd(u, v, w);
        if (w == 1) X++, d0[u]++, d0[v]++;
        else        Y++, d1[u]++, d1[v]++;
    }
    ll P = ((ll)n * (n - 1) % mod * ((mod + 1) >> 1) % mod - (n - 2) + mod) % mod;
    rep(i, 0, Y) c[0][i][i] = 1;
    req(i, 0, X) rep(j, 0, Y) {
        ll Q = n - i - j;
        ll p1 = i * (P - Q + mod) % mod;
        ll p2 = 2 * j * (P - Q + mod) % mod;
        ll p3 = (X - i) * (Q - 1) % mod;
        ll p4 = 2 * (Y - j) * (Q - 1) % mod;
        ll b = P * (i + j * 2) % mod * ((mod + 1) >> 1) % mod;
        add(c[i + 1][j], i, j, (p1 + p2 + p3 + p4) % mod);
        if (i) add(c[i + 1][j], i - 1, j, mod - p1);
        if (j) add(c[i + 1][j], i, j - 1, mod - p2);
        if (j != Y) add(c[i + 1][j], i, j + 1, mod - p4);
        c[i + 1][j][Y + 1] += mod - b;
        ll P3 = qpow(p3);
        rep(k, 0, Y + 1) (c[i + 1][j][k] *= P3) %= mod;
    }
    for (int i = X, j = 0; j < Y; j++) {
        ll Q = n - i - j;
        ll p1 = i * (P - Q + mod) % mod;
        ll p2 = 2 * j * (P - Q + mod) % mod;
        ll p3 = (X - i) * (Q - 1) % mod;
        ll p4 = 2 * (Y - j) * (Q - 1) % mod;
        ll b = P * (i + j * 2) * ((mod + 1) >> 1) % mod;
        add(a[j], i, j, (p1 + p2 + p3 + p4) % mod);
        if (i) add(a[j], i - 1, j, mod - p1);
        if (j) add(a[j], i, j - 1, mod - p2);
        add(a[j], i, j + 1, mod - p4);
        a[j][Y + 1] = (b - a[j][Y + 1] + mod) % mod;
    } a[Y][0] = 1;
    Gauss(), f[0][Y + 1] = 1;
    rep(j, 0, Y) f[0][j] = a[j][Y + 1] * qpow(a[j][j]) % mod;
    rep(i, 1, X) rep(j, 0, Y) rep(k, 0, Y + 1)
        (f[i][j] += c[i][j][k] * f[0][k]) %= mod;
    rep(i, 1, n) (ans += f[d0[i]][d1[i]]) %= mod;
    prs(((ans - f[X][Y] - X * f[1][0] - Y * f[0][1]) % mod + mod) % mod);
}

void mprw() {}

bool Med;

int main() {
    #ifdef JYR
    errs("\033[1;34mRunning!\033[0;m");
    freopen("Test.in", "r", stdin);
    freopen("Test.out", "w", stdout);
    #ifdef FILERR
    freopen("Test.err", "w", stderr);
    #endif
    #endif
    mprw();
    #ifdef MC
    int _ = ri;
    while (_--) errs("------------------------------"), mslv();
    errs("------------------------------");
    #else
    mslv();
    #endif
    #ifdef JYR
    errm("%.3lfMB ", abs(&Med - &Mbe) / 1048576.);
    errm("%.0lfms\n", clock() * 1000. / CLOCKS_PER_SEC);
    #endif
    return 0;
}
0