Codeforces Round #245 (Div. 2) C. Xor-tree DFS

题目链接:

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

题意:

一棵以1为根节点的树,每个点都是1或者0,
选定一个节点x,当前值取反,x的孙子,孙子的孙子。。。均取反
然后问你最少翻转多少次可以到达目标位置,且翻转的是哪些 点

题解一:

深度浅的点一定是受影响最小的(根节点只受自己的影响),所以从根依次向下递推处理

代码一:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll 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;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;

std::vector<int> G[maxn],ans;
int n;
int s[maxn],e[maxn];

void dfs(int x,int f,int s1, int s2){ // s1表示当前点是否翻转,s2表示当前点的所有儿子们是否翻转
    if(s[x]^s1 != e[x]){ 
        ans.PB(x);
        s1 ^= 1; // 如果不行, 必转
    }

    for(auto v : G[x]){
        if(v!=f)
            dfs(v,x,s2,s1); // 走到下一层, 因为当前层的上一层的翻转状态和下一层的一样
    }
}

int main(){
    n = read();
    for(int i=1; i<n; i++){
        int u=read(),v=read();
        G[u].PB(v);
        G[v].PB(u);
    }

    for(int i=1; i<=n; i++)
        s[i] = read();
    for(int i=1; i<=n; i++)
        e[i] = read();

    dfs(1,-1,0,0);

    cout << ans.size() << endl;
    for(auto x : ans)
        cout << x << endl;

    return 0;
}

题解二:

http://blog.csdn.net/qq_26122039/article/details/52914241
节点初始标志记在d1[]中,节点目标标志记在d2[]中,k1为1表示奇数节点标志改变,k2为1表示偶数节点标志改变.h表示节点深度
从根节点开始深搜,遇到节点j先通过k1, k2改变d1[j]的值,改变后若d1[j] != d2[j]则d1[j] ^= 1, 同时若该节点深度h为奇数,则该节点孩子中奇数深度的节点都要改变,所以k1 ^= 1,同理若h为偶数,则该节点孩子中偶数深度的节点都要改变,所以k2 ^= 1;

代码二:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll 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;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;

std::vector<int> G[maxn],ans;
int n;
int s[maxn],e[maxn];

void dfs(int x,int f,int k1,int k2,int d){

    if(k1 && (d&1)){
        s[x] ^= 1;
    }
    if(k2 && !(d&1)){
        s[x] ^= 1;
    }

    if(s[x] != e[x]){
        ans.PB(x);
        if(d&1) k1 ^= 1;
        else k2 ^= 1;
        s[x] ^= 1;
    }

    for(auto v : G[x]){
        if(v!=f)
            dfs(v,x,k1,k2,d+1);
    }
}

int main(){
    n = read();
    for(int i=1; i<n; i++){
        int u=read(),v=read();
        G[u].PB(v);
        G[v].PB(u);
    }

    for(int i=1; i<=n; i++)
        s[i] = read();
    for(int i=1; i<=n; i++)
        e[i] = read();

    dfs(1,-1,0,0,0);

    cout << ans.size() << endl;
    for(auto i : ans)
        cout << i << endl;

    return 0;
}

今天打了BC#92 看题解才知道auto的用法= =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值