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. ...
随机推荐
- 做完task1-21的阶段总结
[说明]这是自注册修真院的第七天,也是第七篇日报,觉得是一个好的时机总结一下. 因为任务一虽然看起来仅仅是“完成学员报名的DB设计并读写数据库”,但是做了几天之后就发现在任务“搭建自己的服务器”之前的 ...
- nginx学习之web服务器(四)
1. 定义一个虚拟服务器 http { server { # Server configuration } } 可以在http {}块里面添加多个server {}块,每一个server {}块代表一 ...
- zookeeper curator CRUD
目录 Curator客户端的基本操作 写在前面 1.1.1. Curator客户端的依赖包 1.1.2. Curator 创建会话 1.1.3. CRUD 之 Create 创建节点 1.1.4. C ...
- Delphi 64与32位的差异
Delphi 64与32位的差异 最近,Delphi推出了64位预览版本, 我做为一个忠实的Delphier, 看到这消息后,第一时间学习,并写下这个做为以后的参考资料. 相同点: 在Delphi ...
- Dajngo admin使用
Dajngo admin使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INS ...
- vs+opencv
vs + opencv 配置见网页 https://blog.csdn.net/qq_17550379/article/details/78201442 统一所有工程配置见下图:
- VIM复制粘贴 的快捷键是什么
yy复制游标所在行整行.或大写一个Y. 2yy或y2y复制两行. ㄟ ,请举一反三好不好! :-) y^复制至行首,或y0.不含游标所在处字元. y$复制至行尾.含游标所在处字元. yw复制一个wor ...
- codeforces 60B bfs
题意:给出一个六面体分为k层,每层n行m列,每个小立方体有'.'(空)与'#'(障碍)的状态,第一层某个空的位置有一个水龙头,水流每次往六个方向流动(...).最少时间水流能把立方体空的部分填满. 思 ...
- 算法(Algorithms)第4版 练习 1.3.7
package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementException; import ed ...
- oracle 从select的结果update其他表
update a set a.id=(selelct b.id from temp b where b.line = a.line) where a.line = (select line from ...