题目链接: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. json:js和jquery中轻量级数据交换格式

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  2. STM32F103RCT6移植到STM32F103C8T6注意事项

    1,修改IC为STC32F103C8 2,修改晶振为8.0M 3,修改C/C++宏定义,由STM32F10X_HD,USE_STDPERIPH_DRIVER 改为 STM32F10X_MD,USE_S ...

  3. 3354 [IOI2005]河流

    题目描述 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一个村庄——名叫 ...

  4. poj2115[扩展欧几里德]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22260   Accepted: 6125 Descr ...

  5. 【BZOJ4269】再见Xor 高斯消元

    [BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...

  6. php记录百度等搜索引擎蜘蛛的来访记录

    <?php function is_robot() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($use ...

  7. Django 视图之CBV

    CBV 所谓的CBV(class base view) 在视图里面,用类的方式来写逻辑 那么对于FBV,CBV有什么优势? CBV(class base views) 就是在视图里使用类处理请求. P ...

  8. ABAP文件选择框函数

    因为WS_FILENAME_GET已经被废弃所以使用接口CL_GUI_FRONTEND_SERVICES来实现本地文件的选择. 用接口类CL_GUI_FRONTEND_SERVICES实现的方法 CA ...

  9. zabbix监控入门初步

    1.Zabbix是什么? Zabbix是一个基于Web界面的分布式系统监控的企业级开源软件.可以监视各种系统与设备的参数,保障服务器及设备的安全运营. 2.Zabbix的功能和特性 (1)安装与配置简 ...

  10. Visual Studio 2017 扩展推荐

    ReSharper : 首先的是Resharper,这个基本是目前是我开发过程中必备的工具集,唯一的缺点就是吃内存,所以你的内存要是低于8G,就不要使用它了.它的特点可以快速重构.高亮显示错误.导航和 ...