HDU 5544 Ba Gua Zhen (DFS找环+线性基)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5544


题目大意:给出一个无向连通图,求一个回路,使得该回路中所有路径权值的亦或最大(某条边经过几次,则亦或几次)


思路:首先找出图中所有的独立环,所谓独立环,就是不能由其它环线性表示出的环。易知,回路是由若干个独立环线性组合而成的。那么我们可以求出每个独立环的亦或值,存到数组中,那么问题就转化为了:给出若干元素,从中选出几个,使它们的总亦或最大。

       因为图是连通的,所以从1出发即可找到所有的环。另d[i]表示从1出发到点i的亦或值,当到达点u时,若u的孩子v已经访问过了,说明u和v在同一环中,并且该环的亦或为d[u]^d[v]^dist(u, v)。因为从1到u与从1到v有一段共同的路程,所以亦或后这一段变为0,剩下的是从v到u的路程,所以再加上边(u, v)即组成了一个环。

       可以用线性基来求解一个集合中若干元素亦或的最大值。关于线性基的介绍可参照https://blog.sengxian.com/algorithms/linear-basis。简单来说,对于线性基中的每个数,其二进制表示中最多只有1位为1,且线性基中每个数的二进制表示中1的位置都不相同。因此,在本题中,将线性基中所有元素亦或起来就是答案。

#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
using namespace std;
const int maxn = 1e5 + 10;
struct Edge {
  int from, to; LL cost;
  Edge(int u, int v, LL c):from(u),to(v),cost(c){}
};
LL d[maxn], circle[maxn];
int vis[maxn];
int cnt;
vector<int> G[maxn];
vector<Edge> edges;

void AddEdge(int from, int to, LL cost) {
  edges.push_back(Edge(from, to, cost));
  edges.push_back(Edge(to, from, cost));
  int m = edges.size();
  G[from].push_back(m-2);
  G[to].push_back(m-1);
}
void init(int n) {
  memset(d, 0, sizeof d);
  memset(vis, 0, sizeof vis);
  for(int i = 1; i <= n; i++) G[i].clear();
  edges.clear();
  cnt = 0;
}

void input_Edge(int m) {
  for(int i = 1; i <= m; i++) {
    int u, v; LL c;
    scanf("%d%d%lld", &u, &v, &c);
    AddEdge(u, v, c);
  }
}

void dfs(int u, int fa, int t) {
  vis[u] = t;

  for(int i = 0; i < G[u].size(); i++) {
    Edge& e = edges[G[u][i]];
    if(e.to == fa) continue;
    if(vis[e.to] && vis[e.to] <= t) {
      circle[cnt++] = d[u] ^ d[e.to] ^ e.cost;
    }
    else if(!vis[e.to]) {d[e.to] = d[u] ^ e.cost; dfs(e.to, u, t+1);}
  }

}

int Gauss(int n) { //传入一个n个元素的集合,求该集合的线性基,返回线性基的大小
  int row = 0;
  for(int i = 62; i >= 0; i--) {
    int j;
    for(j = row; j < n; j++)
      if(circle[j] & (1LL << i))
        break;
    if(j != n) {
      swap(circle[row], circle[j]);
      for(j = 0; j < n; j++) {
        if(j == row) continue;
        if(circle[j] & (1LL << i))
          circle[j] ^= circle[row];
      }
      row++;
    }
  }
  return row;
}

int main() {
  int T, kase = 0;
  scanf("%d", &T);
  while(T--) {
    int n, m;
    scanf("%d%d", &n, &m);
    init(n);
    input_Edge(m);
    dfs(1, -1, 1);
    int num = Gauss(cnt);
    LL ans = 0;
    for(int i = 0; i < num; i++)
      ans = max(ans, ans ^ circle[i]);
    printf("Case #%d: %lld\n", ++kase, ans);
  }
  return 0;
}


   


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值