Codeforces 845G Shortest Path Problem?
http://codeforces.com/problemset/problem/845/G
#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?的更多相关文章
- 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 ...
- 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码
00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...
- Codeforces 938G Shortest Path Queries [分治,线性基,并查集]
洛谷 Codeforces 分治的题目,或者说分治的思想,是非常灵活多变的. 所以对我这种智商低的选手特别不友好 脑子不好使怎么办?多做题吧-- 前置知识 线性基是你必须会的,不然这题不可做. 推荐再 ...
- 【CF edu 27 G. Shortest Path Problem?】
time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...
- node搜索codeforces 3A - Shortest path of the king
发一下牢骚和主题无关: 搜索,最短路都可以 每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...
- 线性基【CF845G】Shortest Path Problem?
Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...
- [CF845G]Shortest Path Problem?
题目大意:同这道题,只是把最大值变成了最小值 题解:略 卡点:无 C++ Code: #include <cstdio> #define maxn 100010 #define maxm ...
- AT [ABC177F] I hate Shortest Path Problem
因为每行只有一个区域不能往下走,因此我们可以来分析一下从起点到整个矩形每个位置的最短路.可以发现每一行的最短路只与上一行的最短路有关,假设我们知道上一行的最短路,上一行不能往下走的区间在 \([L, ...
- 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 ...
随机推荐
- 通过js获取本机的IP地址
参考链接:https://blog.csdn.net/qq_39327418/article/details/90052668
- SpringBoot异步编程
异步调用:当我们执行一个方法时,假如这个方法中有多个耗时的任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务.当然你也可以选择等这些任务都执行完了,再返回给客户端 ...
- 本机sshd服务开启但是其他机器不能登陆
问题现象: 发现本机sshd服务开启但是其他机器不能登陆 查看sshd服务 [root@localhost ~]# systemctl status sshd.service ● sshd.servi ...
- Spring mybatis源码篇章-Mybatis的XML文件加载
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis主文件加载 前话 前文主要讲解了Mybatis的主文件加载方式,本文则分析不使用主文件加载方式 ...
- CodeBlocks 配置
CodeBlocks 配置 Code::Blocks 17.12 时间:2019.6 下载网址 http://www.codeblocks.org/downloads/26 ,这里选择的是 mingw ...
- RMI(远程方法调用)
Remote Method Invocation 跨虚拟机间调用 使用 RMI 技术可轻松将 服务提供者(Service Provider)与 服务消费者(Service Consumer)进行分离 ...
- spring mvc 简单实现及相关配置实现
配置文件 actio.xml <?xml version="1.0" encoding="UTF-8"?> <controller> & ...
- error LNK2001: unresolved external symbol __imp__closesocket@4
环境:Visual C++6.0 问题:链接错误 描述: Linking... NetSrv.obj : error LNK2001: unresolvedexternal symbol __imp_ ...
- 剑指offer34:第一个只出现一次的字符的位置
1 题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 2 思路和方法 ch[ ...
- axios拦截器的介绍
interceptors 拦截器 拦截器一般做什么? 1. 修改请求头的一些配置项 2. 给请求的过程添加一些请求的图标 3. 给请求添加参数 拦截器的基本语法: 拦截器分为全局拦截器和局部拦截器 全 ...