比赛 |
CSP2023-S模拟赛 |
评测结果 |
AAAEEEEEEE |
题目名称 |
编辑题目 |
最终得分 |
30 |
用户昵称 |
心灵震荡 |
运行时间 |
1.309 s |
代码语言 |
C++ |
内存使用 |
4.13 MiB |
提交时间 |
2023-10-17 20:11:02 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 105, mod = 998244353;
int n, m, q, a[N], head[N], tot, u, v, w, dis[N], ans, cnt[N], ed;
bool vis[N], flag_w1 = 1, flag;
struct edge {int to, nxt, val, flag;} e[N << 1];
void add_edge(int u, int v, int w)
{
e[++tot] = {v, head[u], w, 1}, head[u] = tot, cnt[u]++;
}
void dfs(int u)
{
if(flag) return;
for(int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if(!vis[v] && e[i].flag)
{
if(e[i].val == e[ed].val) {flag = 1; return;}
vis[v] = 1, dfs(v), vis[v] = 0;
}
}
return;
}
inline int ksm(int x, int y)
{
int res = 1;
while(y)
{
if(y & 1) res = res * x % mod;
x = x * x % mod, y >>= 1;
}
return res;
}
signed main()
{
freopen("edit.in", "r", stdin);
freopen("edit.out", "w", stdout);
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
add_edge(u, v, w);
add_edge(v, u, w);
flag_w1 &= (w == 1);
}
if(flag_w1)
{
int tmp = 0;
for(int i = 1; i <= n; i++) tmp += (cnt[i] > 1 ? m : m - 1);
cout << tmp * ksm(n * m, mod - 2) % mod;
return 0;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
memset(vis, 0, sizeof vis);
e[(j << 1) - 1].flag = e[j << 1].flag = 0, v = j;
vis[i] = 0, ed = (j << 1) - 1, flag = 0;
dfs(i);
e[(j << 1) - 1].flag = e[j << 1].flag = 1, ans += flag;
}
cout << ans * ksm(n * m, mod - 2) % mod;
return 0;
}