【bzoj2115】[Wc2011] Xor 高斯消元+dfs

Description

http://www.lydsy.com/JudgeOnline/images/2606_1.jpg

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

http://www.lydsy.com/JudgeOnline/images/2606_3.jpg

Source


要求的其实是起点到终点的路径和一堆环的异或最大值

其实是【求选出一些数与给定数异或的最大值】。找出环来然后丢矩阵里高斯消元一下就行了

一定要处处开longlong!!

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;
const int SZ = 1000010;
const int INF = 1000000010;

int head[SZ],nxt[SZ],tot = 1;

struct edge{
    int t;
    LL d;
}l[SZ];

void build(int f,int t,LL d)
{
    l[++ tot].t = t;
    l[tot].d = d;
    nxt[tot] = head[f];
    head[f] = tot;
}

int n,m,r = 0;
LL dist[SZ],g[SZ];
bool vis[SZ];

void dfs(int u)
{
    vis[u] = 1;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(!vis[v])
        {
            dist[v] = dist[u] ^ l[i].d;
            dfs(v);
        }
        else
            g[++ r] = dist[u] ^ dist[v] ^ l[i].d;
    }
}

LL gauss()
{
    int now = 0;
    for(int i = 63;i >= 0;i --)
    {
        int j = now + 1;
        while(j <= r && !(g[j] >> i & 1)) j ++;
        if(j > r) continue;
        now ++;
        swap(g[now],g[j]);
        for(int j = 1;j <= r;j ++)
            if((g[j] >> i & 1) && j != now)
                g[j] ^= g[now];
    }
    LL ans = dist[n];
    for(int i = 1;i <= now;i ++)
        ans = max(ans,ans ^ g[i]);
    return ans;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i ++)
    {
        int a,b;
        LL c;
        scanf("%d%d%lld",&a,&b,&c);
        build(a,b,c); build(b,a,c);
    }
    dfs(1);
    printf("%lld",gauss());
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值