meaning of the title
Given a tree with edge weights, each operation can make all edges of a chain different or the same number as the previous one. The edge weights of each edge are changed to 0 after the least number of operations.
N < = 100000, border weight < = 15
Analysis
We can set the point weight of a point as XOR sum of all the edges connected with it.
Then the point weight of all points is 0 and the edge weight of all edges is 0, which is a necessary and sufficient condition for each operation to change the point weight of two points.
Press dp one time.
Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define MIN(x,y) x=min(x,y)
using namespace std;
const int N=100005;
const int inf=1000000000;
int n,a[N],bin[20],cnt[70005],f[70005],w[20];
vector<int> vec[20];
int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void dp(int S)
{
for (int i=0;i<bin[16];i++) cnt[i]=cnt[i>>1]+(i&1),vec[cnt[i]].push_back(i),f[i]=inf;
f[S]=0;
for (int i=16;i>0;i--)
for (int j=0;j<vec[i].size();j++)
{
int s=vec[i][j];
if (f[s]==inf) continue;
for (int x=0;x<16;x++)
for (int y=0;y<16;y++)
{
if (!(s&bin[x])||!(s&bin[y])||x==y) continue;
MIN(f[s^bin[x]^bin[x^y]^bin[y]],f[s]+1+((s&bin[x^y])>0));
}
}
}
int main()
{
bin[0]=1;
for (int i=1;i<=16;i++) bin[i]=bin[i-1]*2;
n=read();
for (int i=1;i<n;i++)
{
int x=read(),y=read(),z=read();
a[x+1]^=z;a[y+1]^=z;
}
for (int i=1;i<=n;i++) w[a[i]]++;
int S=0,ans=0;
for (int i=1;i<=15;i++) ans+=w[i]/2,w[i]%=2,S+=bin[i]*w[i];
dp(S);
printf("%d",ans+f[0]);
return 0;
}