zoukankan      html  css  js  c++  java
  • Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree

    Time Limit: 1 Sec  Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/430/problem/C

    Description

    Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees.

    The game is played on a tree having n nodes, numbered from 1 to n. Each node i has an initial value initi, which is either 0 or 1. The root of the tree is node 1.

    One can perform several (possibly, zero) operations on the tree during the game. The only available type of operation is to pick a node x. Right after someone has picked node x, the value of node x flips, the values of sons of x remain the same, the values of sons of sons of x flips, the values of sons of sons of sons of x remain the same and so on.

    The goal of the game is to get each node i to have value goali, which can also be only 0 or 1. You need to reach the goal of the game by using minimum number of operations.

    Input

    The first line contains an integer n (1 ≤ n ≤ 105). Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) meaning there is an edge between nodes ui and vi.

    The next line contains n integer numbers, the i-th of them corresponds to initi (initi is either 0 or 1). The following line also contains n integer numbers, the i-th number corresponds to goali (goali is either 0 or 1).


    1000000000.

    Output

    In the first line output an integer number cnt, representing the minimal number of operations you perform. Each of the next cnt lines should contain an integer xi, representing that you pick a node xi.

    Sample Input

    10
    2 1
    3 1
    4 2
    5 1
    6 2
    7 5
    8 6
    9 8
    10 5
    1 0 1 1 0 1 0 1 0 1
    1 0 1 0 0 1 1 1 0 1

    Sample Output

    2
    4
    7

    HINT


    题意

    给你一棵以1为根节点的树,然后每个点都是1或者0,
    你可以使得一个点和他的儿子们全部都翻转
    然后问你最少翻转多少次,且翻转的是哪些 点

    题解:

      很明显的一个dfs

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    //const int inf=0x7fffffff;   //无限大
    const int inf=0x3f3f3f3f;
    /*
    
    int buf[10];
    inline void write(int i) {
      int p = 0;if(i == 0) p++;
      else while(i) {buf[p++] = i % 10;i /= 10;}
      for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
      printf("
    ");
    }
    */
    //**************************************************************************************
    inline ll 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;
    }
    struct edge
    {
        int x,y,z;
    };
    vector<int> e[maxn];
    void add_edge(int a,int b)
    {
        e[a].push_back(b);
    }
    int a[maxn];
    int dp[maxn];
    vector<int> ans;
    int flag[maxn];
    void dfs(int x,int c,int d)
    {
        if(flag[x])
            return;
        flag[x]=1;
        if(a[x]^c==dp[x])
        {
            for(int i=0;i<e[x].size();i++)
            {
                int v=e[x][i];
                dfs(v,d,c);
            }
        }
        else
        {
            ans.push_back(x);
            for(int i=0;i<e[x].size();i++)
            {
                int v=e[x][i];
                dfs(v,d,!c);
            }
        }
    }
    int main()
    {
        int n;
        cin>>n;
        for(int i=0;i<n-1;i++)
        {
            int u=read(),v=read();
            add_edge(v,u);
            add_edge(u,v);
        }
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
            cin>>dp[i];
        dfs(1,0,0);
        cout<<ans.size()<<endl;
        for(int i=0;i<ans.size();i++)
            cout<<ans[i]<<endl;
    }
  • 相关阅读:
    Python 学习
    Linux学习
    java 学习
    jvm学习
    [转]根据时间(NSDate)获取具体的信息:月份、星期、天等
    详解http和https的区别
    一些题
    NSNotification的用法 (转自CSDN:ReyZhang的博客)
    关于File's Owner
    UI设计
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4428305.html
Copyright © 2011-2022 走看看