社区讨论
妹子刚学OI,p4551 80pts玄关求条
P4551最长异或路径参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhjh0pag
- 此快照首次捕获于
- 2025/11/04 02:25 4 个月前
- 此快照最后确认于
- 2025/11/04 02:25 4 个月前
帮Lovelace_qwq发的
CPP#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<climits>
#include<vector>
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define PII pair<int,int>
#define itn int
#define brk break
#define cte continue
#define faii fa[i]=i
using namespace std;
inline int read(){
int t=0,flag=1;
char a=getchar();
while(!isdigit(a)){
if(a=='-'){
flag=-1;
}
// a=getchar_unlocked();
a=getchar();
}
while(isdigit(a)){
t=t*10+a-'0';
// a=getchar_unlocked();
a=getchar();
}
return t*flag;
}
inline void put(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x<10){
putchar(x+'0');
}else{
put(x/10);
putchar(x%10+'0');
}
}
struct node{
int son[2];
}e[5000005];
struct nodee{
int to,w,nex;
}a[5000005];
int n,m,cnt,cnt2;
int dist[5000005],head[5000005];
inline void add(int u,int v,int w){
++cnt2;
a[cnt2].to=v;
a[cnt2].w=w;
a[cnt2].nex=head[u];
head[u]=cnt2;
}
inline void insert(int x){
int rt=0;
for(int i=30;i>=0;i--){
int c=(x>>i)&1;
if(!e[rt].son[c]){
e[rt].son[c]=++cnt;
}
rt=e[rt].son[c];
}
}
inline int query(int x){
int rt=0,ans=0;
for(int i=30;i>=0;i--){
int c=(x>>i)&1;
if(e[rt].son[c^1]){
ans^=(1<<i);
rt=e[rt].son[c^1];
}else{
rt=e[rt].son[c];
}
}
return ans;
}
void dfs(int x,int fa){
insert(dist[x]);
for(itn i=head[x];i;i=a[i].nex){
itn v=a[i].to;
if(v==fa){
cte;
}
dist[v]=dist[x]^a[i].w;
dfs(v,x);
}
}
main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<n;i++){
int u,v,w;
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
dfs(1,0);
int ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,query(i));
}
cout<<ans;
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...