题目链接:http://acm.uestc.edu.cn/#/problem/show/1219

题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大。

这是CCPC南阳站的一道题。当时只读了题目发现并不会。

这是一个典型的xor高斯消元。

需要预先dfs出所有的独立回路。

然后线性组合独立回路的xor和,使得ans最大。

最近做过类似的题目,直接粘代码。

代码:

方法一:线性基(O(63n))

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ;
const int maxM = ;
int n, m, top;
LL p[maxN], s[maxM];
int vis[maxN];
//链式前向星
struct Edge
{
int to, next;
LL val;
}edge[maxM*]; int head[maxN], cnt; void addEdge(int u, int v, LL w)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].val = w;
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} void dfs(int now, int fa, int t)
{
vis[now] = t;
int k;
for (int i = head[now]; i != -; i = edge[i].next)
{
k = edge[i].to;
if (k == fa) continue;
if (vis[k] != -)
{
if (vis[k] <= t)
s[top++] = p[now]^p[k]^edge[i].val;
}
else
{
p[k] = p[now]^edge[i].val;
dfs(k, now, t+);
}
}
} void input()
{
initEdge();
memset(vis, -, sizeof(vis));
scanf("%d%d", &n, &m);
int u, v;
LL w;
for (int i = ; i < m; ++i)
{
scanf("%d%d%lld", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
} //xor高斯消元求线性基
//时间复杂度O(63n)
int xorGauss(int n)
{
int row = ;
for (int i = ; i >= ; i--)
{
int j;
for (j = row; j < n; j++)
if(s[j]&((LL)<<i))
break;
if (j != n)
{
swap(s[row], s[j]);
for (j = ; j < n; j++)
{
if(j == row) continue;
if(s[j]&((LL)<<i))
s[j] ^= s[row];
}
row++;
}
}
return row;
} void work()
{
top = ;
p[] = ;
dfs(, , );
int row;
row = xorGauss(top);
LL ans = ;
for (int i = ; i < row; ++i)
ans = max(ans, ans^s[i]);
printf("%lld\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
printf("Case #%d: ", times+);
input();
work();
}
return ;
}

方法二:高斯消元判断是否有解(之前博客讲过)(O(63*63n))

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ;
const int maxM = ;
const int len = ;
int num, a[][maxM*];
bool vis[maxM];
LL p[maxN]; //链式前向星
struct Edge
{
int to, next;
LL val;
//int val;
}edge[maxM*]; int head[maxN], cnt; void addEdge(int u, int v, LL w)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].val = w;
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} LL xorGauss(int n)
{
LL ans = ;
for (int i = len-; i >= ; i--)
{
int j;
for (j = ; j < n; j++)
{
if (a[i][j] && !vis[j])
{
vis[j] = true;
ans += (LL)<<i;
break;
}
}
if(j == n)
{
if(a[i][n] == )
ans += (LL)<<i;
}
else
{
for (int k = i-; k >= ; k--)
{
if (a[k][j])
{
for (int v = ; v <= n; v++)
a[k][v] ^= a[i][v];
}
}
}
}
return ans;
} void dfs(int now, LL val)
{
if (p[now] != -)
{
LL t;
t = p[now]^val;
for (int j = ; t > ; ++j)
{
a[j][num] = t&;
t >>= ;
}
num++;
return;
}
p[now] = val;
int k;
for (int i = head[now]; i != -; i = edge[i].next)
{
k = edge[i].to;
dfs(k, val^edge[i].val);
}
} void input()
{
int n, m;
scanf("%d%d", &n, &m);
initEdge();
int u, v;
LL w;
for (int i = ; i < m; ++i)
{
scanf("%d%d%lld", &u, &v, &w);
addEdge(u, v, w);
addEdge(v, u, w);
}
memset(p, -, sizeof(p));
num = ;
dfs(, );
} void init()
{
memset(a, , sizeof(a));
memset(vis, false, sizeof(vis));
input();
for (int i = ; i < len; i++)
a[i][num] = ;
} void work()
{
LL ans;
ans = xorGauss(num);
//cout << num << endl;
printf("%lld\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
printf("Case #%d: ", times+);
init();
work();
}
return ;
}

ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)的更多相关文章

  1. ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...

  2. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  3. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  4. ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...

  5. HDU 5544 Ba Gua Zhen dfs+高斯消元

    Ba Gua Zhen Problem Description During the Three-Kingdom period, there was a general named Xun Lu wh ...

  6. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  7. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  8. ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...

  9. ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. ...

随机推荐

  1. Linux服务器性能日志收集和分析脚本(转)

    最近老大要求分析服务器的性能数据,找到服务器运行的性能瓶颈,结果花了两天时间,写了两个脚本可以生成日志并可以进行数据提取,最终生成数据可以放到excel生成报表.过程中也学到了不少shell编程技术. ...

  2. JMeter 通过CSV Data Set Config 中文参数化数据,插入数据库后中文显示乱码,解决办法

    问题描述: 1. 需要设置中文参数化,模拟post请求,通过配置元件 - CSV Data Set Config 进行设置. 2. 数据库数据显示乱码(实际数据为 “测试001”) 解决办法: CSV ...

  3. Python高级编程第二版--笔记

    不只是CPython Stackless Python Jython(与java集成) IronPython(与net集成) PyPy python真正出众的领域在于围绕语言打造的整个生态系统. Py ...

  4. 【python】-- SQLAlchemy操作MySQL

    ORM.SQLAchemy orm英文全称object relational mapping,就是对象映射关系程序,简单来说就是类似python这种面向对象的程序来说一切皆对象,但是使用的数据库却都是 ...

  5. Card Collector(期望+min-max容斥)

    Card Collector(期望+min-max容斥) Card Collector woc居然在毫不知情的情况下写出一个min-max容斥 题意 买一包方便面有几率附赠一张卡,有\(n\)种卡,每 ...

  6. 3.写一个hello world页面

    经过1和2的学习,现在已经可以正常启动Django了,这一节说怎么写一个hello world页面,所有的环境基础就是1和2中搭建的 1.在app模块中添加页面 具体为 hello_django\he ...

  7. PHP环境变量归纳(转自网络)

    PHP环境变量主要有$GLOBALS[].$_SERVER[].$_GET[].$_POST[].$_COOKIE[].$_FILES[].$_ENV[].$_REQUEST[].$_SESSION[ ...

  8. Tensorflow官方文档中文版——第一章

    第一示例: import tensorflow as tf import numpy as np x_data=np.float32(np.random.rand(,))#随机输入 y_data=np ...

  9. python 3 并发编程之多进程 multiprocessing模块

    一 .multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程. ...

  10. android OTG【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7707056 一.OTG的概念 OTG是On-The-Go的缩写,是近年发展起来的技术, ...