430C codeforces - Xor-Tree


430C codeforces - Xor-Tree





Solution in C++


#include <bits/stdc++.h>
using namespace std;

int n,sol,t1,t2;
vector<int> bas(100005),goal(100005);
vector<int> adj[100005],solu;
int viz[100005];
int nn[2];

void dfs(int x,int rem)
{
    int lol=0;
    if(viz[x])return;
        viz[x]=1;
    if(nn[rem%2]%2)bas[x]=(bas[x]+1)%2;
    if(bas[x]!=goal[x])nn[rem%2]++,
        sol++,bas[x]=(bas[x]+1)%2,
        solu.push_back(x),lol=1;
    for(auto u : adj[x])dfs(u, (rem+1)%2 );
    if(lol)nn[rem%2]--;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=0;i<n-1&& cin>>t1>>t2;i++)
        adj[t1].push_back(t2),
        adj[t2].push_back(t1);
    for(int i=1;i<=n;i++)cin>>bas[i];
    for(int i=1;i<=n;i++)cin>>goal[i];
    dfs(1,0);
    cout<<sol<<"\n";
    for(auto u : solu)cout<<u<<"\n";
    return 0;
}

Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

1 التعليقات:

  1. Hotel Near Bryson City Casino, Bryson City, United States
    Get reviews, opening hours and a map of 논산 출장안마 Hotel Near Bryson 속초 출장샵 City Casino, 김해 출장샵 Bryson 경기도 출장안마 City, 양주 출장샵 United States - find the reviews and ratings for this property.

    ردحذف