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 ...
随机推荐
- jquery mCustomScrollbar使用
$(".content .desc").mCustomScrollbar({ axis: "y", theme: 'dark', mouseWheel: { e ...
- CockroachDB学习笔记——[译]Scaling Raft
原文链接:https://www.cockroachlabs.com/blog/scaling-raft/ 原作者:Ben Darnell 原文日期:Jun 11, 2015 译:zifeiy 在Co ...
- 当微信小程序遇到AR(三)
当微信小程序遇到AR,会擦出怎么样的火花?期待与激动...... 通过该教程,可以从基础开始打造一个微信小程序的AR框架,所有代码开源,提供大家学习. 本课程需要一定的基础:微信开发者工具,JavaS ...
- 日常小节----unity小坑记(射线检测固定层级)
unity中射线检测需设定所需层级时,必须加上距离!!! //一条从主相机到屏幕点击点的射线 Ray ray = Camera.Main.ScreenPointToRay(Input.mousePos ...
- 《精通并发与Netty》学习笔记(11 - 详解NIO (二) 分散/聚集 Scatter/Gather、Selector)
一.分散/聚集 Scatter/Gather scatter/gather指的在多个缓冲区上实现一个简单的I/O操作,比如从通道中读取数据到多个缓冲区,或从多个缓冲区中写入数据到通道:scatter( ...
- Introduction - Welcome
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第一章<绪论:初识机器学习>中第1课时<欢迎参加机器学习课程>的视频原文字幕.为本人在视频学习过程 ...
- vue v-slot
参考链接:https://www.jianshu.com/p/7020636d68a5
- Python openCV基础操作
1.图片加载.显示和保存 import cv2 # 读取图片 img = cv2.imread("img1.jpg") # 生成灰色图片 imgGrey = cv2.imread( ...
- poj3304(叉积判断直线和线段相交)
题目链接:https://vjudge.net/problem/POJ-3304 题意:求是否能找到一条直线,使得n条线段在该直线的投影有公共点. 思路: 如果存在这样的直线,那么在公共投影点作直线的 ...
- 【AtCoder】ARC079
ARC079题解 C - Cat Snuke and a Voyage #include <bits/stdc++.h> #define fi first #define se secon ...