C - Equalize
#include<bits/stdc++.h>
using namespace std;
using namespace std;
string a,b;
int main(){
int n;
cin>>n;
cin>>a>>b;
int ans=0,sum=0;
for(int j=0;j<n;j++){
if(a[j]!=b[j]) ans++;
}
for(int j=0;j<n-1;j++){
if(a[j]==b[j]) continue;
if(a[j]=='1'&&a[j+1]=='0'&&b[j]=='0'&&b[j+1]=='1'){
sum++;
swap(a[j],a[j+1]);
}else if(a[j]=='0'&&a[j+1]=='1'&&b[j]=='1'&&b[j+1]=='0'){
sum++;
swap(a[j],a[j+1]);
}
}
for(int j=0;j<n;j++){
if(a[j]!=b[j]) sum++;
}
cout<<min(sum,ans)<<endl;
return 0;
}
D - Valid BFS?
#include<bits/stdc++.h>
using namespace std;
#define maxn 300005
vector<int>q[maxn];
int de[maxn],fa[maxn],ff[maxn];
int a[maxn],vi[maxn],sz[maxn];
void dfs(int u,int f,int d){
de[u]=d;
fa[d]++;
vi[u]=f;
for(int j=0;j<q[u].size();j++){
int v=q[u][j];
if(v==f) continue;
sz[u]++;
dfs(v,u,d+1);
}
}
int main(){
memset(de,0,sizeof(de));
memset(fa,0,sizeof(fa));
memset(vi,0,sizeof(vi));
memset(sz,0,sizeof(sz));
memset(ff,0,sizeof(ff));
int n;
cin>>n;
for(int j=1;j<n;j++){
int u,v;
cin>>u>>v;
q[u].push_back(v);
q[v].push_back(u);
}
for(int j=1;j<=n;j++){
cin>>a[j];
}
dfs(1,0,1);
int l=0;
sz[0]=1;
for(int j=1;j<=n;j++){
int k=de[a[j]];
while((sz[a[l]]==0)) l++;
if(fa[k-1]!=0){
cout<<"No"<<endl;
return 0;
}
if(vi[a[j]]!=a[l]){
cout<<"No"<<endl;
return 0;
}
if(!ff[a[j]]){
ff[a[j]]=1;
}else{
cout<<"No"<<endl;
return 0;
}
sz[a[l]]--;
fa[k]--;
}
cout<<"Yes"<<endl;
return 0;
}