ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)
题目链接: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高斯消元)的更多相关文章
- ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...
- ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...
- HDU 5544 Ba Gua Zhen dfs+高斯消元
Ba Gua Zhen Problem Description During the Three-Kingdom period, there was a general named Xun Lu wh ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...
- ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. ...
随机推荐
- [Android Studio 权威教程]AS 中配置强大的版本号管理系统(Git、SVN、等)
在Eclipse中加入Git等版本号管理工具须要自己加入插件.并且个人认为不咋好用,在AS中已经给我们集成好了,我们仅仅须要配置一下就OK了.今天就和大家聊聊怎么配置以及使用的要点. 1. 安装Git ...
- 安装部署服务器和javaweb项目
[说明]总算告一段落了,服务器啊服务器,你可是把我折磨的够呛,不过现在的状态我已经很满足了. [说明]下面的图片是我这两天一直在搞的,内容不能说是重复,只能说是不停地修改修改,出错出错. 1) 虚拟主 ...
- [转]jquery中innerWidth(),outerWidth(),outerWidth(true)和width()的区别
转自:http://www.cnblogs.com/keyi/p/5933981.html jquery中innerWidth(),outerWidth(),outerWidth(true)和wi ...
- Ogbect对象转换为泛型对象
相信很多人都自己写个这个转换的方法,再次附上我自己的写转换方法仅供参考. T t = BeanUtil.dbObject2Bean(obj, tClass); public static <T& ...
- 洛谷 P3629 [APIO2010]巡逻
题目在这里 这是一个紫题,当然很难. 我们往简单的想,不建立新的道路时,从1号节点出发,把整棵树上的每条边遍历至少一次,再回到1号节点,会恰好经过每条边两次,路线总长度为$2(n-1)$,根据树的深度 ...
- win8 office 2013激活方法
先在用win8的人越来越多了,可是某些软件对win8不太友好(也可以说是win8对某些低版本软件不友好),office注册软件office toolkit就是,我在win7上使用2.4.1版本没有问题 ...
- Qt Creator 调试器 在 Ubuntu 13.10下 局部变量和表达式(Locals) 无内容
此篇算是一个翻译,万一有国内同样的小白遇到同样问题,方便参考. 原文http://hostilefork.com/2013/10/20/qtcreator-debugger-no-locals-ubu ...
- JS中如何获取<Select>中value和text的值
原文地址:JS中如何获取<Select>中value和text的值 html代码: <select id = "city" onchange="chan ...
- Data Structure Linked List: Write a function to reverse a linked list
iterative太简单不写了 http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/ ...
- js原生函数一些封装
这是一些js原生封装的函数,主要是为了兼容IE浏览器,如下 获取css样式 function getStyle(ele, prop) { if(window.getComputedStyle) { r ...