zoukankan      html  css  js  c++  java
  • [CF430C]Xor-tree(DFS,贪心)

    题目链接:http://codeforces.com/contest/430/problem/c

    题意:一棵树上的节点为0或1,希望把节点上的值变为目标值。选择几个点,这几个点会翻转,同时它的孙子…也会翻转,问最少需要修改几个节点。

    任选一点开始DFS,判断某点翻转后是否与目标值相同,相同的话则不翻转,不相同则翻转,并且记录该点。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%I64d", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onecnt(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 const int maxn = 100100;
     72 int n;
     73 int init[maxn], goal[maxn];
     74 bool vis[maxn];
     75 vector<int> edge[maxn];
     76 vector<int> ans;
     77 
     78 void dfs(int u, int x, int y) {
     79     if((init[u] ^ x) != goal[u]) {
     80         ans.pb(u);
     81         x = !x;
     82     }
     83     Rep(i, edge[u].size()) {
     84         int v = edge[u][i];
     85         if(vis[v]) continue;
     86         vis[v] = 1;
     87         dfs(v, y, x);
     88     }
     89 }
     90 
     91 int main() {
     92     // FRead();
     93     int u, v;
     94     Rint(n);
     95     Rep(i, n-1) {
     96         Rint(u); Rint(v);
     97         edge[u].pb(v);
     98         edge[v].pb(u);
     99     }
    100     For(i, 1, n+1) Rint(init[i]);
    101     For(i, 1, n+1) Rint(goal[i]);
    102     vis[1] = 1;
    103     dfs(1, 0, 0);
    104     printf("%d
    ", ans.size());
    105     Rep(i, ans.size()) printf("%d
    ", ans[i]);
    106     RT 0;
    107 }
  • 相关阅读:
    hystrix总结之缓存
    python3列表
    hystrix总结之多返回值命令
    hystrix总结之限流
    hystrix(5) 延时检测
    redis-start
    设计模式-4建造者模式
    设计模式-3原型模式
    设计模式-2工厂设计模式
    设计模式-七大设计原则
  • 原文地址:https://www.cnblogs.com/kirai/p/5802575.html
Copyright © 2011-2022 走看看