「WC2011」Xor - 线性基

给出一个nn个点mm条边的带边权无向联通图,求一条从11nn的路径(可以重复经过某些点和边),使得路径异或和最大

n50000,m100000,ai1018n\le 50000, m \le 100000, a_i\le 10^{18}

BZOJ2115

Solution

这道题的思路在这里写过

就是通过猜测得到两个结论:

对于一条从11nn的路径异或和,一定可以表示成任意一条从11nn的路径异或上某个环

对于这个图的任意一棵生成树,通过其中若干个非树边所构成的环异或之后,一定能表示出这个图中的任意一个环

然后直接用线性基搞就可以了

对于图上路径异或和最大问题好像都是基于这个套路

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 50000 + 100, Maxm = 100000 + 100;

int N, M, e = 1, Begin[Maxn], To[Maxm << 1], Next[Maxm << 1];
LL W[Maxm << 1];

inline void add_edge (int x, int y, LL z) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; W[e] = z; }

LL Dis[Maxn];
int Vis[Maxn], Vis_Edge[Maxm];

inline void dfs (int x)
{
Vis[x] = 1;
for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i]; if (Vis[y]) continue;
Vis_Edge[i >> 1] = 1;

Dis[y] = Dis[x] ^ W[i];
dfs(y);
}
}

struct info {int x, y; LL z; } E[Maxm];

namespace Basis
{
const int Maxlen = 63;
LL A[Maxlen + 10];

inline int insert (LL x)
{
for (int i = Maxlen; i >= 0; --i)
{
if (!(x & (1ll << i))) continue;
if (!A[i])
{
for (int j = 0; j < i; ++j) if (x & (1ll << j)) x ^= A[j];
for (int j = i + 1; j <= Maxlen; ++j) if (A[j] & (1ll << i)) A[j] ^= x;
A[i] = x;
return 1;
}
x ^= A[i];
}
return 0;
}

inline LL query (LL ans)
{
for (int i = 0; i <= Maxlen; ++i) Chkmax(ans, ans ^ A[i]);
return ans;
}
}

inline void Solve ()
{
dfs(1);

for (int i = 1; i <= M; ++i)
{
if (Vis_Edge[i]) continue;
int x = E[i].x, y = E[i].y; LL z = E[i].z;
// printf("(%d %d) : (%lld %lld) %d\n", x, y, Dis[x], Dis[y], HLD :: get_lca(x, y));
LL now = z ^ Dis[x] ^ Dis[y];
Basis :: insert (now);
}

cout << Basis :: query (Dis[N]) << endl;
}

inline void Input ()
{
N = read<int>(), M = read<int>();
for (int i = 1; i <= M; ++i)
{
int x = read<int>(), y = read<int>(); LL z = read<LL>();
E[i] = (info){x, y, z};
add_edge (x, y, z);
add_edge (y, x, z);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}
感谢你的赞赏!