「CF938G」Shortest Path Queries - 线段树分治 + 线性基 + 并查集

给出一个nn个点的连通带权无向图,边有边权,要求支持qq 个操作:

  • 1 x y d 在原图中加入一条xxyy 权值为dd的边

  • 2 x y 把图中xxyy 的边删掉

  • 3 x y 表示询问xxyy的异或最短路

保证任意操作后原图连通无重边自环且操作均合法

n,m,q200000n,m,q\le200000

CF938G

Solution

先线段树分治把时间这一维去掉

用可撤销并查集维护联通性,顺便维护一下当前点到父亲这条边上的异或值

这道题的套路,把出现的环丢到线性基里即可

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#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 = 4e5 + 100, Maxm = 4e5 + 100, Maxlen = 30;

int N, M, Q, edge_cnt;

struct edge
{
int x, y, z;
int l, r;
}E[Maxm];

map <pii, int> Vis;

struct Basis
{

int A[Maxlen + 2];

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

inline int query (int ans = 0)
{
for (int i = Maxlen; i >= 0; --i) Chkmin(ans, ans ^ A[i]);
return ans;
}
}B[Maxn << 1];

namespace DSU
{
int fa[Maxn], sum[Maxn], size[Maxn];

inline void init (int maxn) { for (int i = 1; i <= maxn; ++i) fa[i] = i, sum[i] = 0, size[i] = 1; }

/**/inline int find (int x) { return fa[x] == x ? x : find(fa[x]); }

inline int get_sum (int x) { int ans = 0; while (fa[x] != x) ans ^= sum[x], x = fa[x]; return ans; }

inline pii link (int now, int x, int y, int z)
{
z ^= get_sum (x) ^ get_sum (y);
x = find(x), y = find(y);
if (x == y) { B[now].insert (z); return mp(0, 0); }

if (size[x] < size[y]) swap(x, y);
fa[y] = x, sum[y] = z, size[x] += size[y];
return mp(x, y);
}

inline int query (int x, int y) { return get_sum(x) ^ get_sum(y); }

inline void pop (pii now)
{
int x = now.x, y = now.y;
fa[y] = y, sum[y] = 0, size[x] -= size[y];
}
}

int Ans[Maxn];
pii Query[Maxn];

namespace SEG
{
#define mid ((l + r) >> 1)
#define ls root << 1
#define rs root << 1 | 1
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r

struct info { int x, y, z; };

vector <info> node[Maxn << 2];
vector <pii> Stack[Maxn << 2];

inline void modify (int root, int l, int r, int x, int y, int u, int v, int w)
{
if (x <= l && r <= y) { node[root].pb((info){u, v, w}); return ; }

if (x <= mid) modify (lson, x, y, u, v, w);
if (y > mid) modify (rson, x, y, u, v, w);
}

inline void process (int root, int l, int r)
{
for (int i = 0; i < node[root].size(); ++i)
{
int x = node[root][i].x, y = node[root][i].y, z = node[root][i].z;
Stack[root].pb(DSU :: link (root, x, y, z));
}

if (l == r) Ans[l] = B[root].query (DSU :: query (Query[l].x, Query[l].y));
else
{
B[ls] = B[rs] = B[root];
process (lson), process (rson);
}

for (int i = Stack[root].size() - 1; ~i; --i) DSU :: pop (Stack[root][i]);
}
}

inline void Solve ()
{
/**/for (int i = 1; i <= edge_cnt; ++i) SEG :: modify (1, 1, Q, E[i].l, E[i].r, E[i].x, E[i].y, E[i].z);

DSU :: init (N);
/**/SEG :: process (1, 1, Q);

for (int i = 1; i <= Q; ++i) if (Query[i].x) printf("%d\n", Ans[i]);
}

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

Q = read<int>();
for (int i = 1; i <= M; ++i) E[i].l = 1, E[i].r = Q, Vis[mp(E[i].x, E[i].y)] = i;

for (int i = 1; i <= Q; ++i)
{
int op = read<int>(), x = read<int>(), y = read<int>(), z;
if (op == 1)
{
z = read<int>();
E[++edge_cnt] = (edge){x, y, z, i, Q};
Vis[mp(x, y)] = edge_cnt;
}
else if (op == 2)
{
E[Vis[mp(x, y)]].r = i;
Vis[mp(x, y)] = 0;
}
else Query[i] = mp(x, y);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("G.in", "r", stdin);
freopen("G.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}

Debug

  • 78L:写成return fa[x] == x ? x : fa[x]
  • 147L,150L:线段树范围写成NN
感谢你的赞赏!