Language: C++

#include <bits/stdc++.h>

using namespace std;

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;


signed main(){
    int n;
    cin >> n;
    vector<int> xd(n - 1), yd(n - 1);
    vector<vector<int>> ori(n);
    vector<int> ed_c(n, 0);
    for(int i = 0; i < n - 1; ++i){
        cin >> xd[i] >> yd[i];
        ori[--xd[i]].emplace_back(--yd[i]);
        ori[yd[i]].emplace_back(xd[i]);
        ++ed_c[xd[i]];
        ++ed_c[yd[i]];
    }
    string s;
    cin >> s;
    bitset<100007> bs, after, dfs_used;
    for(int i = 0; i < n; ++i)
        bs.set(i, s[i] == 'B');

    if(bs.count() == n){
        cout << 0 << endl;
        return 0;
    }

    // 使わない頂点をここに入れる
    unordered_set<int> deleted;

    queue<int> que;
    for(int i = 0; i < n; ++i)
        if(ed_c[i] == 1 && bs[i])
            que.emplace(i);
    while(!que.empty()){
        int t = que.front();
        deleted.emplace(t);
        que.pop();
        for(auto& to : ori[t]){
            if(bs[to] && --ed_c[to] == 1)
                que.emplace(to);
        }
    }

    int ed_aft_sum = 0;
    for(int i = 0; i < n; ++i){
        if(deleted.find(i) != deleted.end())
            continue;
        // 削除された後の頂点の次数
        int c = 0;
        for(auto& to : ori[i])
            if(deleted.find(to) == deleted.end())
                c++;
        // 削除後の木で全頂点を巡回した場合の色 黒なら1
        after.set(i, bs[i] ^ (c & 1));
        ed_aft_sum += c;
    }

    // 暫定的なスコア
    i64 pre_score = ed_aft_sum + (n - deleted.size() - after.count());
    i64 ans = pre_score;

    for(int i = 0; i < n; ++i)
        cout << after[i];
    cout << endl;
    cout << ans << endl;

    function<int(int)> f = [&](int x){
        dfs_used.set(x);
        vector<pair<int,int>> lis(1);
        for(auto& y : ori[x]){
            if(deleted.find(y) == deleted.end() && !dfs_used[y]){
                lis.emplace_back(f(y));
            }
        }
        sort(lis.begin(), lis.end(), greater<int>());
        // 端点のうち片方は移動回数が変わらない
        // after[x]が1なら黒で、その場合はあまり端点にしたくない
        // f(i): i以下の部分木ノードとiのパスでできる白の最大値
        ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
        ans = min(ans, pre_score - (2 * (lis[0] + !after[x]) - 1 - ));
        return lis[0] + !after[x];
    };


    for(int i = 0; i < n; ++i)
        if(deleted.find(i) == deleted.end()){
            f(i);
            break;
        }

    cout << ans << endl;

}


/*
白であるような葉はいらない => 削除する動作を繰り返していく
絶対に全ての頂点を渡らないといけないようになる(葉が全て黒になる)
ちょうど一周する場合、全ての頂点をその次数の数だけ訪れる事になる
全ての頂点をその次数の数だけ訪れた場合の色を求める

このままだとスコアは「辺数 * 2」 + 「反転後に黒になっているものの数」になる

使わないパスを決めた時、それをする事で減るスコアは
	「パスの長さ」 + 「パス上(閉区間)での白の数」 - 「パス上(開区間)での黒の数」
	頂点の個数をN、パス上の黒の数をKとして、それぞれ"N - 1", "K", "N - K"とできる
	したがって、"N - 1 + K - N + K" => "2K - 1"がスコアの改善値になる
	よってKを最大化したい

1,1,0,1 => 3回分の移動を節約できる

黒の数ができる限り大きくなるようなパスを求める
これは木DP(dp[i]:i番目を含みそれより子側でできるパスに含まれる黒の数の最大値)で解ける これで解けた
*/


Compiler output:
a.cc: In lambda function:
a.cc:88:49: error: no match for 'operator+' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} and 'bool')
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
In file included from /usr/include/c++/14/bits/stl_algobase.h:67,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:627:5: note: candidate: 'template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)'
  627 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'bool'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note: candidate: 'template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)'
 1798 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'bool'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/basic_string.h:3598:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3598 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3598:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3616:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3616 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   mismatched types 'const _CharT*' and 'std::pair<int, int>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3635:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3635 |     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'bool'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3652:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 3652 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3670:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 3670 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3682:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3682 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3689:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3689 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3696:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3696 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::value_type' {aka 'std::pair<int, int>'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3719:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3719 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   mismatched types 'const _CharT*' and 'std::pair<int, int>'
   88 |         ans = min(ans, pre_score - (2 * (lis[0] + !after[x] + (lis.size() > 1 ? lis[1] : 0)) - 1));
      |                                                           ^
/usr/include/c++/14/bits/basic_string.h:3726:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3726 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note:   template argument deduction/substitution failed:
a.cc:88:59: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'bool'
   88 |         ans = min(ans, pre_score - (2 * (lis[0]