社区讨论
自闭的萌新+1
P5129 不可思议的迷宫参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lobl6uzm
- 此快照首次捕获于
- 2023/10/29 22:49 2 年前
- 此快照最后确认于
- 2023/11/04 03:44 2 年前
代码实在不知道哪里错了,求助:
CPP#include<bits/stdc++.h>
using namespace std;
#define Reimu inline void
#define Marisa inline int
namespace FastInput {
template<typename Ty>
inline Ty read() {
Ty x = 0;
int f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= c == '-';
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c & 15);
return f ? -x : x;
}
template<>
inline double read() {
double x = 0;
int f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= c == '-';
for (; isdigit(c); c = getchar()) x = x * 10 + (c & 15);
if (c == '.') {
double e = 1;
for (c = getchar(); isdigit(c); c = getchar()) x += (c & 15) * (e *= .1);
}
return f ? -x : x;
}
template<>
inline char read() {
char c = getchar();
while (!isgraph(c)) c = getchar();
return c;
}
template<>
inline string read() {
string s = "";
char c = getchar();
for (; !isgraph(c); c = getchar());
for (; isgraph(c); c = getchar()) s += c;
return s;
}
template<typename Ty>
Reimu read(Ty &x) {
x = read<Ty>();
}
template<typename Ty, typename...Arr>
Reimu read(Ty &x, Arr &...arr) {
read(x);
read(arr...);
}
}
using namespace FastInput;
namespace Mod {
const int mod = 19260817;
Marisa add(int x, int y) {
return (x += y) < mod ? x < 0 ? x + mod : x : x - mod;
}
Marisa Add(int&x, int y) {
return (x += y) < mod ? x < 0 ? (x += mod) : x : (x -= mod);
}
Marisa mul(int x, int y) {
return 1LL * x * y % mod;
}
Marisa Mul(int&x, int y) {
return x = 1LL * x * y % mod;
}
Marisa qpow(int x, int y) {
int res = 1;
for (; y; y >>= 1) {
if (y & 1) Mul(res, x);
Mul(x, x);
}
return res;
}
Marisa inv(int x) {
return qpow(x, mod - 2);
}
}
using namespace Mod;
typedef pair<int, int> Pii;
typedef pair<int, Pii> Piii;
#define fi first
#define se second
#define mp make_pair
const int N = 300010;
int n;
int inc[N], sz[N];
Piii e[N];
vector<int> g[N];
Reimu getCir(int x, int fa) {
inc[x] = 1;
try {
for (auto y: g[x]) {
if (y == fa) continue;
if (inc[y]) throw y;
getCir(y, x);
}
} catch(int v) {
if (!v) throw inc[x] = 0;
if (v == x) throw 0;
throw v;
}
}
Reimu dfs(int x, int fa) {
sz[x] = 1;
for (auto y: g[x]) {
if (y == fa || inc[y]) continue;
dfs(y, x);
sz[x] += sz[y];
}
}
int main() {
read(n);
for (int i = 1; i <= n; ++i) {
int x = e[i].se.fi = read<int>(), y = e[i].se.se = read<int>();
read(e[i].fi);
g[x].emplace_back(y);
g[y].emplace_back(x);
}
try {
getCir(1, 0);
} catch(int) {}
int t = 0, c = 0, ans = 0, inv2 = inv(2);
for (int i = 1; i <= n; ++i) {
if (inc[i]) {
dfs(i, 0);
Add(t, mul(sz[i], sz[i]));
++c;
}
}
c = add(add(mul(n, n), -t), add(mul(n, 2), -c));
for (int i = 1; i <= n; ++i) {
int v = e[i].fi, x = e[i].se.fi, y = e[i].se.se;
if (inc[x] && inc[y]) Add(ans, mul(mul(v, c), inv2));
else {
int l = min(sz[x], sz[y]), r = n - l;
Add(ans, mul(mul(v, mul(l, r)), 2));
}
}
printf("%d", mul(ans, inv(mul(n, n))));
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...