2115: [Wc2011] Xor

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 3915  Solved: 1633
[Submit][Status][Discuss]

Description

Input

第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目。 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边。 图中可能有重边或自环。

Output

仅包含一个整数,表示最大的XOR和(十进制结果),注意输出后加换行回车。

Sample Input

5 7

1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2

Sample Output

6

HINT

弄了那么久还是讲不清楚线性基是什么

大概就是在异或时去除掉一些重复的元素,使得剩下的元素异或不出0且值域覆盖原来的异或值域?

学完回来补坑

除此之外,就是找到1到N的一条路径,对于路径上所有的环记录下来进行一次高斯消元,贪心异或即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 50005,maxm = 200005,INF = 1000000000;
inline LL RD(){
LL out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}
return out * flag;
}
LL N,M,d[maxn],V[maxm],A[65],cir = 0,tot = 0,bin[65];
bool vis[maxn];
int head[maxn],nedge = 0;
struct EDGE{LL to,w,next;}edge[maxm];
inline void build(int u,int v,LL w){
edge[nedge] = (EDGE){v,w,head[u]}; head[u] = nedge++;
edge[nedge] = (EDGE){u,w,head[v]}; head[v] = nedge++;
}
void dfs(int u){
vis[u] = true; int to;
Redge(u) if (!vis[to = edge[k].to]){
d[to] = d[u] ^ edge[k].w;
dfs(to);
}else V[++cir] = d[to] ^ d[u] ^ edge[k].w;
}
void gaosi(){
for (LL j = bin[60]; j; j >>= 1){
int i = tot + 1;
while (i <= cir && !(V[i] & j)) i++;
if (i == cir + 1) continue;
swap(V[++tot],V[i]);
for (int k = 1; k <= cir; k++)
if (k != tot && (V[k] & j))
V[k] ^= V[tot];
}
}
int main(){
bin[0] = 1;REP(i,60) bin[i] = bin[i - 1] << 1;
memset(head,-1,sizeof(head));
N = RD(); M = RD(); LL a,b,w;
while (M--){
a = RD(); b = RD(); w = RD();
build(a,b,w);
}
dfs(1); gaosi();
LL ans = d[N];
for (int i = 1; i <= cir; i++)
ans = max(ans,ans ^ V[i]);
cout<<ans<<endl;
return 0;
}

BZOJ2115 [Wc2011] Xor 【线性基】的更多相关文章

  1. BZOJ2115:[WC2011] Xor(线性基)

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  2. 【BZOJ-2115】Xor 线性基 + DFS

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2142  Solved: 893[Submit][Status] ...

  3. BZOJ.2115.[WC2011]Xor(线性基)

    题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...

  4. BZOJ 2115 [Wc2011] Xor ——线性基

    [题目分析] 显然,一个路径走过两边是不需要计算的,所以我么找到一条1-n的路径,然后向该异或值不断异或简单环即可. 但是找出所有简单环是相当复杂的,我们只需要dfs一遍,找出所有的环路即可,因为所有 ...

  5. BZOJ 2115: [Wc2011] Xor 线性基 dfs

    https://www.lydsy.com/JudgeOnline/problem.php?id=2115 每一条从1到n的道路都可以表示为一条从1到n的道路异或若干个环的异或值. 那么把全部的环丢到 ...

  6. bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...

  7. bzoj千题计划194:bzoj2115: [Wc2011] Xor

    http://www.lydsy.com/JudgeOnline/problem.php?id=2115 边和点可以重复经过,那最后的路径一定是从1到n的一条路径加上许多环 dfs出任意一条路径的异或 ...

  8. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  9. BZOJ2115 [Wc2011] Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

随机推荐

  1. 为什么不早点使用 Git...

    教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013739628770 ...

  2. 2.5 进程控制之wait函数

    一.绪论 一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还保留着,内核在其中保存了一些信息:如果 是正常终止则保存着退出状态,如果是异常终止则保存着导致该进程终止的信号是 ...

  3. Leecode刷题之旅-C语言/python-69x的平方根

    /* * @lc app=leetcode.cn id=69 lang=c * * [69] x 的平方根 * * https://leetcode-cn.com/problems/sqrtx/des ...

  4. Leecode刷题之旅-C语言/python-26.移除元素

    /* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-elem ...

  5. 03---Nginx配置文件

    #启动子进程程序默认用户#user nobody;#一个主进程和多个工作进程.工作进程是单进程的,且不需要特殊授权即可运行:这里定义的是工作进程数量worker_processes 1; #全局错误日 ...

  6. codeforces 845A Chess Tourney

    参考:https://blog.csdn.net/zhongyuchen/article/details/77478039 #include <iostream> #include < ...

  7. shell重温---基础篇(参数传递&echo命令)

    经过前两天的学习,关于shell的基础算是知道的一般般啦,最起码不算是小白了(纯属意淫).今天就来点干货哈.   首先是运行shell脚本时的参数传递.脚本内获取参数的格式为$n.n代表了一个数字,例 ...

  8. P1016 旅行家的预算

    P1016 旅行家的预算 题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2 ...

  9. Android Studio引入AAR文件

    一.编译生成AAR文件 二.把AAR文件复制到项目的libs目录下 三.在项目的配置文件中加入如下代码: android { //other code repositories{ flatDir{ d ...

  10. linux中wget 、apt-get、yum rpm区别

    wget 类似于迅雷,是一种下载工具, 通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理名字是World Wide Web”与“get”的结合. yum: 是r ...