http://codeforces.com/problemset/problem/845/G

从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从第一个环原路返回。
对每种备选方案通过x = min(x,x^v[i]) 保留备选方案中不存在的最高位,如果能由已经存在的备选方案组合得到则结果为零
最后对val[n]做一次类似的操作得到答案
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
vector<int> v;
struct EDGE{
int to,next,len;
EDGE(){
}
EDGE(int a,int b,int c){
to = a,next = b,len = c;
}
}e[maxn<<];
int vis[maxn],st,ed,cnt,head[maxn],val[maxn],len;
void add(int u,int v,int len){
e[++cnt] = EDGE(v,head[u],len);
head[u] = cnt;
}
void add(int x){
for(int i = ;i<(int)v.size();i++){
x = min(x,x^v[i]);
}
if(x) v.push_back(x);
}
void dfs(int x,int now){
vis[x] = ;
for(int i = head[x];i;i = e[i].next){
int to = e[i].to;
if(vis[to]) add(val[to]^now^e[i].len);
else{
val[to] = now^e[i].len;
dfs(to,val[to]);
}
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i = ;i<m;i++){
scanf("%d%d%d",&st,&ed,&len);
add(st,ed,len);
add(ed,st,len);
}
dfs(,);
for(int i = ;(int)i<v.size();i++){
val[n] = min(val[n],val[n]^v[i]);
}
return *printf("%d",val[n]);
}

8/28更新

https://csacademy.com/contest/archive/task/xor_cycle

一道类似的题,同样是找出所有环,最后组合出最大结果输出即可

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n,m;
vector<pair<int,long long> > node[];
int vis[],a,b;
long long val[];
long long _len;
vector<long long> base;
void add(long long x){
for(int i = ;i<base.size();i++){
x = min(x,x^base[i]);
}
if(x) base.push_back(x);
}
void dfs(int x,int fa){
for(int i = ;i<node[x].size();i++){
int to = node[x][i].first;
long long len = node[x][i].second;
if(to == fa) continue;
if(!vis[to]){
vis[to] = ;
val[to] = val[x]^len;
dfs(to,x);
}
else{
add(val[to]^len^val[x]);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>m;
for(int i = ;i<m;i++){
cin>>a>>b>>_len;
node[a].push_back(make_pair(b,(long long)_len));
node[b].push_back(make_pair(a,(long long)_len));
}
dfs(,-);
long long ans = ;
for(int i = ;i<base.size();i++){
ans = max(ans,ans^base[i]);
}
cout<<ans<<endl;
return ;
}

Codeforces 845G Shortest Path Problem?的更多相关文章

  1. Codefroces Educational Round 27 845G Shortest Path Problem?

    Shortest Path Problem? You are given an undirected graph with weighted edges. The length of some pat ...

  2. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  3. Codeforces 938G Shortest Path Queries [分治,线性基,并查集]

    洛谷 Codeforces 分治的题目,或者说分治的思想,是非常灵活多变的. 所以对我这种智商低的选手特别不友好 脑子不好使怎么办?多做题吧-- 前置知识 线性基是你必须会的,不然这题不可做. 推荐再 ...

  4. 【CF edu 27 G. Shortest Path Problem?】

    time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...

  5. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  6. 线性基【CF845G】Shortest Path Problem?

    Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...

  7. [CF845G]Shortest Path Problem?

    题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...

  8. AT [ABC177F] I hate Shortest Path Problem

    因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路.可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([L, ...

  9. Solve Longest Path Problem in linear time

    We know that the longest path problem for general case belongs to the NP-hard category, so there is ...

随机推荐

  1. Spring事务管理5-----声明式事务管理(3)

    声明式事务管理  基于注解 在配置文件中需要开启注解驱动<tx:annotation-driven transaction-manager="transactionManager&qu ...

  2. [转]js禁止微信浏览器下拉显示黑底查看网址,不影响内部Scroll

    原贴:https://www.cnblogs.com/jasonwang2y60/p/6848464.html 原贴:https://www.cnblogs.com/jasonwang2y60/p/6 ...

  3. process.env.NODE_ENV

    Node 随记 if (process.env.NODE_ENV === 'production') { module.exports = require('./prod.js') } else { ...

  4. windows7导入k8s用户证书

    通过浏览器访问 需要给浏览器生成一个 client 证书,访问 apiserver 的 6443 https 端口时使用 这里使用部署 kubectl 命令行工具时创建的 admin 证书.私钥和上面 ...

  5. spring注入注解

    常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Component. @Resource装配顺序 1. 如果同时指定了n ...

  6. c# 子窗体居中父窗体

    1.设置CenterParent不管用.只好用代码控制. frmRunning_ = new FrmRunning(); frmRunning_.StartPosition = FormStartPo ...

  7. 初识MyBatis之HelloWorld

    1.先下载MyBatis相关Jar包. 2. 创建数据库和表 CREATE TABLE tbl_employee( id ) PRIMARY KEY AUTO_INCREMENT, last_name ...

  8. 2019-10-17 李宗盛 spss作业

    开放数据库连接是为解决异构数据库之间的数据共享而产生的,现已成为Wosa cwindows开放系统体系结构主要部分和基于Windows环境的一种数据库访问接口标准ODBS被异构数据库访问提供统一接口, ...

  9. [CF991D]Bishwock_状压dp

    Bishwock 题目链接:http://codeforces.com/problemset/problem/991/D 数据范围:略. 题解: 一眼题. 首先,每个$L$最多只占用两列,而且行数特别 ...

  10. [转帖]linux下CPU、内存、IO、网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具

    linux下CPU.内存.IO.网络的压力测试,硬盘读写速度测试,Linux三个系统资源监控工具 https://blog.51cto.com/hao360/1587165 linux_python关 ...