zoukankan      html  css  js  c++  java
  • [CF717E]Paint it really, really dark gray(dfs,构造)

    题目链接:http://codeforces.com/contest/717/problem/E

    题意:给一棵树 有两个颜色,一个人从节点1出发,每走到一个节点都会使对应节点变色。问怎么走可以让整棵树变成同一个颜色。

    很容易想到分子树去处理,假如一棵深度为2的子树,根节点有n个儿子。儿子里有k个是反色的,那么反色的点必然会被走一次,同时根节点也会跟着变色。当这棵子树搞定以后,这个节点就会变成上一棵子树的叶节点,同样的操作即可。所以关注两层节点,后序遍历后如果儿子未变成1,那么和父亲来回走一遍。注意根节点1,整棵树走完以后有可能根节点未变成1,只要再随便找一个点走一遍,最终落脚到那个点就行了。

      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 = 200200;
     72 int n;
     73 int color[maxn];
     74 vector<int> G[maxn];
     75 
     76 void dfs(int u, int p) {
     77     Rep(i, G[u].size()) {
     78         int v = G[u][i];
     79         if(v == p) continue;
     80         color[v] = -color[v];
     81         printf("%d ", v);
     82         dfs(v, u);
     83         color[u] = -color[u];
     84         printf("%d ", u);
     85         if(color[v] == -1) {
     86             color[v] = -color[v];
     87             printf("%d ", v);
     88             color[u] = -color[u];
     89             printf("%d ", u);
     90         }
     91     }
     92 }
     93 
     94 int main() {
     95     // FRead();
     96     int u, v;
     97     while(~Rint(n)) {
     98         Cls(color);
     99         For(i, 1, n+1) {
    100             Rint(color[i]);
    101             G[i].clear();
    102         }
    103         Rep(i, n-1) {
    104             Rint(u); Rint(v);
    105             G[u].push_back(v);
    106             G[v].push_back(u);
    107         }
    108         printf("1 ");
    109         dfs(1, 0);
    110         if(color[1] == -1) {
    111             int v = G[1][0];
    112             color[v] = -color[v];
    113             printf("%d ", v);
    114             color[1] = -color[1];
    115             printf("%d ", 1);
    116             color[v] = -color[v];
    117             printf("%d ", v);
    118         }
    119     }
    120     RT 0;
    121 }
  • 相关阅读:
    闯荡Linux帝国:nginx的创业故事
    一个HTTP数据包的奇幻之旅
    远去的传说:安全软件群雄混战史
    默认浏览器争霸传奇
    浏览器主页锁定之战——IE:我太难了
    产品vs程序员:你知道www是怎么来的吗?
    手把手教你从零开始搭建SpringBoot后端项目框架
    使用IntelliJ IDEA新建Java Web后端resfulAPI模板
    如何正确的在项目中接入微信JS-SDK
    html2canvas关于图片不能正常截取
  • 原文地址:https://www.cnblogs.com/kirai/p/5877115.html
Copyright © 2011-2022 走看看