zoukankan      html  css  js  c++  java
  • CodeForces

    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 ≤ nui ≠ 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).

    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.

    Examples

    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
    Output
    2
    4
    7

    题意:给出一棵树,然后这个树,然后每个节点有个值,然后再给一个目标树,要求把树翻转成目标树,翻转规则是,翻转当前节点,他的儿子不变
    ,他的儿子的儿子要翻转,求翻转次数

    思路:因为他每翻一个下面的节点就要发生变化,翻多了节点后就不好判断当前要不要翻转,我就分别记录,奇数层的翻转次数,
    和偶数层的翻转次数还有就是判断当前层的奇偶,然后进行dfs
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    vector<int> mp[100001];
    int c1[100001],c2[100001];
    int n,x,y;
    int num[100001];
    int vis[100001];
    int cnt;
    void dfs(int x,int j,int o,int z,int d)
    {
        if(z==0)//第一个不同的点
        {
            if(c1[x]!=c2[x])
            {
                num[cnt++]=x;
                d=1;
                z=1;
                j=1; 
            }
        }
        else{//判断奇偶与层数
            d++;
            if(c1[x]==c2[x]&&d%2==1&&j%2==1) 
            {
                num[cnt++]=x;
                j++;
            }
            else if(c1[x]!=c2[x]&&d%2==1&&j%2==0) 
            {
                num[cnt++]=x;
                j++;
            }
            else if(c1[x]==c2[x]&&d%2==0&&o%2==1) 
            {
                num[cnt++]=x;
                o++;
            }
            else if(c1[x]!=c2[x]&&d%2==0&&o%2==0) 
            {
                num[cnt++]=x;
                o++;
            }
            
        }
        for(int i=0;i<mp[x].size();i++)//往下dfs
        {
            if(vis[mp[x][i]]==0)
            {
                vis[mp[x][i]]=1;
                dfs(mp[x][i],j,o,z,d);
            }
        }
    } 
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n-1;i++)//领接表建图
        {
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&c1[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&c2[i]);
        vis[1]=1;
        dfs(1,0,0,0,0);
        printf("%d
    ",cnt);
        for(int i=0;i<cnt;i++)
            printf("%d
    ",num[i]);
    }

     

  • 相关阅读:
    《SQL 基础教程》第五章:复杂查询
    Ruby on Rails Tutorial 第一章笔记
    《Practical Vim》第十章:复制和粘贴
    《Practical Vim》第五章:命令行模式
    《SQL 基础教程》第四章:数据更新
    用户的三次登录验证及进度条
    socket模块开启一个永久性服务
    TCP协议实现切换目录
    爬取好友微信信息
    TCP协议中传输系统命令及上传下载文件
  • 原文地址:https://www.cnblogs.com/Lis-/p/9826921.html
Copyright © 2011-2022 走看看