2115: [Wc2011] Xor

时间:2021-03-01 16:47:41

2115: [Wc2011] Xor

链接

分析:

  对于图中的一个环,是可以从1到这个环,转一圈然后在回到1的,所以可以一开始走很多个环,然后在走一条1到n的路径。

  那么可以求出所有的环,加入到线性基中,然后任意一条1->n的路径,取一遍最大值。

  如果1->n的路径就是最终要走的路径,那么就取到了。如果不是,这我们走的这条路径是p1,最终答案最大的路径是p2,那么p1和p2合起来就是个环,如果p2更优和这个环异或,就把p1消掉了。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline LL read() {
LL x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
struct Edge{ int to, nxt; LL w; } e[N * ];
int head[N], En;
LL dis[N], b[N];
bool vis[N];
vector<LL> cir; inline void add_edge(int u,int v,LL w) {
++En; e[En].to = v, e[En].w = w, e[En].nxt = head[u]; head[u] = En;
}
void dfs(int u) {
vis[u] = ;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (vis[v]) {
cir.push_back(dis[u] ^ e[i].w ^ dis[v]);
}
else {
dis[v] = dis[u] ^ e[i].w;
dfs(v);
}
}
}
void Insert(LL x) {
for (int i = ; ~i; --i)
if ((x >> i) & ) {
if (b[i]) x ^= b[i];
else {
b[i] = x; break;
}
}
}
int main() {
int n = read(), m = read();
for (int i = ; i <= m; ++i) {
int u = read(), v = read(); LL w = read();
add_edge(u, v, w);
}
dfs();
for (int i = ; i < (int)cir.size(); ++i) Insert(cir[i]);
LL ans = dis[n];
for (int i = ; ~i; --i)
if (ans < (ans ^ b[i])) ans = ans ^ b[i];
cout << ans;
return ;
}