2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

探讨了一个由无向图构成的八卦阵谜题,目标是寻找所有回路中边权异或和的最大值。通过DFS遍历并利用高斯消元法解决此问题。

Ba Gua Zhen

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

Description

During the Three-Kingdom period, there was a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he was stuck in the Ba Gua Zhen from Liang Zhuge. The puzzle could be considered as an undirected graph with N vertexes and M edges. Each edge in the puzzle connected two vertexes which were ui and vi with a length of wi. Liang Zhuge had great interests in the beauty of his puzzle, so there were no self-loops and between each pair of vertexes, there would be at most one edge in the puzzle. And it was also guaranteed that there was at least one path to go between each pair of vertexes.

Fortunately, there was an old man named Chengyan Huang who was willing to help Xun Lu to hack the puzzle. Chengyan told Xun Lu that he had to choose a vertex as the start point, then walk through some of the edges and return to the start point at last. During his walk, he could go through some edges any times. Since Liang Zhuge had some mysterious magic, Xun Lu could hack the puzzle if and only if he could find such a path with the maximum XOR sum of all the edges length he has passed. If the he passed some edge multiple times, the length would also be calculated by multiple times. Now, could you tell Xun Lu which is the maximum XOR circuit path in this puzzle to help him hack the puzzle?



Input

The first line of the input gives the number of test cases, T(1≤T≤30). T test cases follow.

Each test case begins with two integers N(2≤N≤5×104) and M(1≤M≤105) in one line. Then M lines follow. Each line contains three integers ui, vi and wi(1≤ui,vi≤N, 0≤wi≤260−1) to describe all the edges in the puzzle.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum XOR sum of one circuit path in the puzzle.

Sample Input

2
3 3
1 2 1
1 3 2
2 3 0
6 7
1 2 1
1 3 1
2 3 1
3 4 4
4 5 2
4 6 2
5 6 2

Sample Output

Case #1: 3
Case #2: 3

HINT

 A XOR takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same.

题意

有一个n(<=50000)个顶点m(<=100000)条边的无向图,每条边有一个边权(0<=边权<2^60),求所有回路中边权xor和的最大值。

题解:

首先dfs,跑出所有环的xor值,比如里面有k个环

然后我们就可以转化为,给你k个数,让你选择任意多的数,使得异或值最大

这个要用高斯消元做:http://wenku.baidu.com/link?url=BFic5zoh7tkGkLTgrRta5OtFliMsghlACqlx-XyjMFPgLh14ujAo33SDtLbFhHYN6JoGt2b1d9XsxMP97Degfpb8QzUs_eZJNEwgbhPVScO

代码:

#include<iostream>
#include<math.h>
#include<vector>
#include<stdio.h>
#include<cstring>
using namespace std;
#define maxn 50005
vector<pair<int,long long> >G[maxn];
vector<long long> ans;
long long Xor[maxn];
int vis[maxn];
void dfs(int x,int pre,long long Ans)
{
    if(vis[x])
    {
        long long p = Ans ^ Xor[x];
        ans.push_back(p);
        return;
    }
    vis[x]=1;
    for(int i=0;i<G[x].size();i++)
    {
        int v = G[x][i].first;
        if(pre == v)continue;
        if(!vis[v])Xor[v]=Ans^G[x][i].second;
        dfs(v,x,Ans ^ G[x][i].second);
    }
}
int main()
{
    int t;scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        ans.clear();
        memset(vis,0,sizeof(vis));
        memset(Xor,0,sizeof(Xor));
        int n,m;scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            G[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;long long z;
            scanf("%d%d%lld",&x,&y,&z);
            G[x].push_back(make_pair(y,z));
            G[y].push_back(make_pair(x,z));
        }
        dfs(1,-1,0);
        int k=0;
        for(int i=60;i>=0;i--)
        {
            int j;
            for(j=k;j<ans.size();j++)
            if((ans[j]&(1LL<<i))!=0)
            {
                break;
            }
            if (j==ans.size()) continue;
            if (j!=k)
            swap(ans[k],ans[j]);
          //cout<<d[j]<<endl;
            for (j=k+1;j<ans.size();j++)
                if ((ans[j]&(1LL<<i))!=0)
                    ans[j]^=ans[k];
            k++;
        }
        long long Ans = 0;
        for(int i=0;i<k;i++)
            Ans = max(Ans,Ans ^ ans[i]);
        printf("Case #%d: %lld\n",cas,Ans);
    }
}

 

### CCPC 2023 H题 解析 关于CCPC 2023 H题的具体题目描述尚未公开,但从以往的比赛惯例以及类似的题目解析可以推测其可能涉及的内容和技术要点。以下是基于已有参考资料和专业知识对该类问题的解答框架。 #### 1. **问题背景** CCPC(Chinese Collegiate Programming Contest)作为国内重要的编程竞赛之一,通常会设计具有挑战性的算法问题来测试参赛者的逻辑思维能力和编码技巧。H题通常是比赛中的难点之一,往往涉及到复杂的算法模型或数据结构的应用[^2]。 #### 2. **潜在的技术方向** 根据过往的经验,H题可能会覆盖以下几个方面: - 动态规划 (Dynamic Programming)[^1] - 构造性问题 (Construction Problems)[^4] - 数学优化 (Mathematical Optimization) 假设该题属于动态规划类别,则需关注状态转移方程的设计;如果是构造性问题,则重点在于如何通过有限操作达到目标条件。 #### 3. **通用解题策略** 无论具体主题为何种类型,在面对高难度赛题时可遵循如下方法论: ##### (1)深入理解题目需求 仔细阅读并反复确认输入输出的要求及其约束条件,确保不会遗漏任何细节信息[^3]。 ##### (2)选取合适的算法工具箱 依据实际场景挑选最匹配的方法论,比如当存在重叠子问题且具备最优子结构性质时优先选用DP技术[^1]。 ##### (3)编写清晰易懂的代码实现 采用模块化的方式分步完成整个功能开发流程,并辅以充分注释说明每一部分的作用机制。 ```cpp // 示例伪代码片段展示基本框架布局 #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; // 输入规模参数 vector<long long> dp(n+1, INF); // 初始化dp数组,默认极大值表示未访问过 dp[0]=0; for(int i=1;i<=n;i++){ for(auto &coin : coins){ if(i >= coin && dp[i - coin]+costs[coin]<dp[i]){ dp[i]=dp[i - coin]+costs[coin]; } } } cout << (dp[n]==INF ? -1 : dp[n])<< "\n"; } ``` 上述例子仅作示意用途,真实情况下应严格依照官方给定的数据范围调整变量类型及边界处理方式。 #### 4. **复杂度考量** 对于大规模实例而言,效率至关重要。因此除了正确率之外还需兼顾运行时间和内存消耗指标。一般建议尽可能降低渐近时间开销至O(NlogN)甚至更低级别。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值