Description
Solution
其实题目前面提到的问题并不是那么困难,其实就是【BZOJ2115】【WC2011】Xor,直接把环搞出来后丢到线性基里面然后求最大值就行了。
对于这个题,我们按位考虑,设
fu
f
u
表示从
u
u
走到这一位为
1
1
的概率,那么我们有:
其中 degu d e g u 表示 u u 的出度,表示 u u 到的路径的权值在这一位上是否为 1 1 。
特别地,我们有。
直接高斯消元求得 f1 f 1 即可。
注意判断自环,只需要连一次边即可,因为这题是随机选择与其相连的一条边,如果自环记了两次,那么该边权值就变成了2。
Code
/************************************************
* Au: Hany01
* Date: Apr 8th, 2018
* Prob: [BZOJ2337][HNOI2011] XOR和路径
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 105, maxm = 20005;
double Ans[maxn];
int n, v[maxm], e, beg[maxn], w[maxm], deg[maxn], nex[maxm], m;
const double eps = 1e-8;
inline int dcmp(double x) {
if (fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
struct Line { double a[maxn], b; }A[maxn];
Line operator + (Line A, Line B) {
Line C;
For(i, 1, n) C.a[i] = A.a[i] + B.a[i];
C.b = A.b + B.b;
return C;
}
Line operator - (Line A, Line B) {
Line C;
For(i, 1, n) C.a[i] = A.a[i] - B.a[i];
C.b = A.b - B.b;
return C;
}
Line operator * (Line A, double B) {
Line C;
For(i, 1, n) C.a[i] = A.a[i] * B;
C.b = A.b * B;
return C;
}
Line operator / (Line A, double B) {
Line C;
For(i, 1, n) C.a[i] = A.a[i] / B;
C.b = A.b / B;
return C;
}
inline void add(int uu, int vv, int ww) { v[++ e] = vv, w[e] = ww, nex[e] = beg[uu], beg[uu] = e, ++ deg[uu]; }
inline double Gauss()
{
For(i, 1, n) {
if (!dcmp(A[i].a[i])) {
For(j, i + 1, n) if (dcmp(A[j].a[i])) { swap(A[i], A[j]); break; }
assert(dcmp(A[i].a[i]));
}
For(j, i + 1, n) if (dcmp(A[j].a[i]))
A[j] = A[j] * (A[i].a[i] / A[j].a[i]) - A[i];
}
Fordown(i, n, 1) {
double tmp = 0.;
For(j, i + 1, n) tmp += Ans[j] * A[i].a[j];
Ans[i] = (A[i].b - tmp) / A[i].a[i];
}
return Ans[1];
}
int main()
{
#ifdef hany01
File("bzoj2337");
#endif
static int uu, vv, ww;
n = read();
for (static int m = read(); m --; ) {
uu = read(), vv = read(), ww = read(), add(uu, vv, ww);
if (uu != vv) add(vv, uu, ww);
}
static double Ans = 0.;
rep(bi, 31)
{
For(u, 1, n - 1) {
For(i, 1, n) A[u].a[i] = 0.;
A[u].b = 0;
A[u].a[u] = deg[u];
for (register int i = beg[u]; i; i = nex[i])
if (w[i] >> bi & 1) ++ A[u].a[v[i]], ++ A[u].b;
else -- A[u].a[v[i]];
}
A[n].a[n] = 1, A[n].b = 0;
Ans += Gauss() * (1 << bi);
}
printf("%.3lf\n", Ans);
return 0;
}
//家国兴亡自有时,吴人何苦怨西施。
// -- 罗隐《西施》