码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 2115 [Wc2011] Xor

时间:2018-02-21 12:21:27      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:ios   路径   iostream   out   dfs   else   ted   str   def   

题解:xor最大路径一定是几个环加上从1到n的路径

环用tarjan处理

最大xor和用线性基处理

喵啊

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long Lint;
const int maxn=50007;

int n,m;
Lint ans;

int cntedge;
int head[maxn];
int to[maxn<<2],nex[maxn<<2];
Lint dist[maxn<<2];
void Addedge(int x,int y,Lint z){
	nex[++cntedge]=head[x];
	to[cntedge]=y;
	dist[cntedge]=z;
	head[x]=cntedge;
}

Lint p[maxn];
int dfsclock;
int pre[maxn],lowlink[maxn];
Lint depth[maxn];
void Dfs(int u,int fa){
	pre[u]=lowlink[u]=++dfsclock;
	
	for(int i=head[u];i;i=nex[i]){
		if(to[i]==fa)continue;
		if(!pre[to[i]]){
			depth[to[i]]=depth[u]^dist[i];
			Dfs(to[i],u);
			lowlink[u]=min(lowlink[u],lowlink[to[i]]);
		}else{
			lowlink[u]=min(lowlink[u],pre[to[i]]);
			Lint x=depth[u]^depth[to[i]]^dist[i];
			for(int j=62;j>=1;--j){
				if(x&(1LL<<(j-1))){
					if(p[j]){
						x^=p[j];
					}else{
						p[j]=x;break;
					}
				}
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	while(m--){
		int x,y;Lint z;
		scanf("%d%d%lld",&x,&y,&z);
		Addedge(x,y,z);Addedge(y,x,z);
	}
	
	Dfs(1,0);
	ans=depth[n];
	for(int j=62;j>=1;--j){
		if((ans^p[j])>ans)ans=(ans^p[j]);
	}
	cout<<ans<<endl;
	return 0;
}

  

BZOJ 2115 [Wc2011] Xor

标签:ios   路径   iostream   out   dfs   else   ted   str   def   

原文地址:https://www.cnblogs.com/zzyer/p/8456383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!