Submission #2504609


Source Code Expand

#include <utility>
#include <cstddef>
#include <numeric>
#include <stack>
#include <iostream>
#include <functional>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
namespace loquat {
using vertex_t = size_t;
}
namespace loquat {
namespace edge_param {
struct to_ {
	vertex_t to;
	explicit to_(vertex_t t = 0)
		: to(t)
	{ }
};
}
namespace detail {
template <typename T, typename... Params>
struct edge_param_wrapper : public T, edge_param_wrapper<Params...> {
};
template <typename T>
struct edge_param_wrapper<T> : public T {
	template <typename U>
	explicit edge_param_wrapper(U&& x)
		: T(std::forward<U>(x))
	{ }
};
}
template <typename... Params>
struct edge : public detail::edge_param_wrapper<edge_param::to_, Params...> {
	template <typename... Args>
	explicit edge(Args&&... args)
		: detail::edge_param_wrapper<edge_param::to_, Params...>(
			std::forward<Args>(args)...)
	{ }
};
}
namespace loquat {
template <typename EdgeType>
class adjacency_list {
public:
	using edge_type = EdgeType;
	using edge_list = std::vector<edge_type>;
private:
	std::vector<std::vector<EdgeType>> m_edges;
public:
	explicit adjacency_list(size_t n)
		: m_edges(n)
	{ }
	size_t size() const {
		return m_edges.size();
	}
	const edge_list& operator[](vertex_t u) const {
		return m_edges[u];
	}
	edge_list& operator[](vertex_t u){
		return m_edges[u];
	}
	template <typename... Args>
	void add_edge(vertex_t from, Args&&... args){
		m_edges[from].emplace_back(std::forward<Args>(args)...);
	}
};
}
namespace loquat {
template <typename EdgeType, typename Behavior, typename... Args>
std::vector<typename Behavior::state_type>
undirected_tree_dynamic_programming(
	const adjacency_list<EdgeType>& graph,
	Behavior behavior,
	Args&&... args)
{
	using state_type = typename Behavior::state_type;
	using frame_type = std::pair<vertex_t, size_t>;
	const size_t n = graph.size();
	std::vector<state_type> temporary(n), result(n);
	for(vertex_t v = 0; v < n; ++v){
		temporary[v] = behavior.initial(v, args...);
	}
	std::stack<frame_type> frames;
	frames.emplace(0, 0);
	while(!frames.empty()){
		const auto frame = frames.top();
		frames.pop();
		const auto u = frame.first;
		const auto i = frame.second;
		const vertex_t p = (frames.empty() ? u : frames.top().first);
		if(i == graph[u].size()){
			for(const auto& e : graph[u]){
				if(e.to == p){ continue; }
				const auto& x = temporary[e.to];
				const auto& y = temporary[u];
				temporary[u] = behavior.merge(y, x, u, e, args...);
			}
		}else{
			const auto& e = graph[u][i];
			frames.emplace(u, i + 1);
			if(e.to != p){ frames.emplace(e.to, 0); }
		}
	}
	frames.emplace(0, 0);
	result[0] = temporary[0];
	while(!frames.empty()){
		const auto frame = frames.top();
		frames.pop();
		const auto u = frame.first;
		const auto i = frame.second;
		const vertex_t p = (frames.empty() ? u : frames.top().first);
		if(i == 0){
			for(const auto& e : graph[u]){
				if(e.to == p){
					const auto& x = temporary[e.to];
					const auto& y = temporary[u];
					result[u] = behavior.merge(y, x, u, e, args...);
					break;
				}
			}
		}
		if(i != graph[u].size()){
			const auto& e = graph[u][i];
			frames.emplace(u, i + 1);
			if(e.to != p){
				const auto& x = temporary[e.to];
				const auto& y = result[u];
				temporary[u] = behavior.purge(y, x, u, e, args...);
				frames.emplace(e.to, 0);
			}
		}
	}
	return result;
}
}
namespace loquat {
template <typename T, size_t K, typename Comparator = std::less<T>>
class top_k {
public:
	using value_type = T;
private:
	Comparator m_comparator;
	size_t m_size;
	std::array<T, K> m_data;
public:
	top_k()
		: m_comparator()
		, m_size(0)
		, m_data()
	{ }
	bool empty() const {
		return m_size == 0;
	}
	size_t size() const {
		return m_size;
	}
	const value_type& operator[](size_t i) const {
		return m_data[i];
	}
	void push(const value_type& x){
		if(m_size == K && !m_comparator(x, m_data[m_size - 1])){ return; }
		if(m_size == K){
			m_data[m_size - 1] = x;
		}else{
			m_data[m_size++] = x;
		}
		for(size_t i = m_size - 1; i > 0; --i){
			if(m_comparator(m_data[i], m_data[i - 1])){
				std::swap(m_data[i], m_data[i - 1]);
			}
		}
	}
};
}
using namespace std;
using edge = loquat::edge<>;
struct behavior {
	using state_type = loquat::top_k<int, 2, std::greater<int>>;
	using edge_type = edge;
	state_type initial(loquat::vertex_t v, const vector<int>& c) const {
		state_type s;
		s.push(c[v]);
		return s;
	}
	state_type merge(
		state_type y,
		const state_type& x,
		loquat::vertex_t u,
		const edge_type& e,
		const vector<int>& c) const
	{
		if(!x.empty()){ y.push(x[0] + c[u]); }
		return y;
	}
	state_type purge(
		const state_type& y,
		const state_type& x,
		loquat::vertex_t u,
		const edge_type& e,
		const vector<int>& c) const
	{
		if(!x.empty() && !y.empty() && y[0] == x[0] + c[u]){
			state_type z;
			if(y.size() > 1){ z.push(y[1]); }
			return z;
		}else{
			return y;
		}
	}
};
int main(){
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	loquat::adjacency_list<edge> g(n);
	for(int i = 1; i < n; ++i){
		int a, b;
		cin >> a >> b;
		--a; --b;
		g.add_edge(a, b);
		g.add_edge(b, a);
	}
	string s;
	cin >> s;
	vector<int> deg(n);
	for(int i = 0; i < n; ++i){ deg[i] = g[i].size(); }
	for(int i = 0; i < n; ++i){
		int u = i;
		while(deg[u] == 1 && s[u] == 'B'){
			int v = -1;
			for(const auto& e : g[u]){
				v = e.to;
				if(deg[v] > 0){ break; }
			}
			--deg[u];
			--deg[v];
			u = v;
		}
	}
	vector<int> c(n);
	for(int i = 0; i < n; ++i){
		const int x = (s[i] == 'W' ? 0 : 1);
		c[i] = 1 - (x ^ (deg[i] & 1));
	}
	const auto dp = loquat::undirected_tree_dynamic_programming(g, behavior(), c);
	int shortcut = 0;
	for(const auto& p : dp){
		if(!p.empty()){ shortcut = max(shortcut, p[0]); }
	}
	const int d = shortcut * 2 - accumulate(c.begin(), c.end(), 0);
	int k = 0;
	for(int i = 0; i < n; ++i){
		if(deg[i] > 0){ ++k; }
	}
	if(k == 0){
		cout << (find(s.begin(), s.end(), 'W') != s.end() ? 1 : 0) << endl;
	}else{
		const int answer = (k - 1) * 2 - d;
		cout << answer << endl;
	}
	return 0;
}

Submission Info

Submission Time
Task F - Monochrome Cat
User logicmachine
Language C++14 (GCC 5.4.1)
Score 800
Code Size 6319 Byte
Status AC
Exec Time 67 ms
Memory 12952 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 118
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt, 1_036.txt, 1_037.txt, 1_038.txt, 1_039.txt, 1_040.txt, 1_041.txt, 1_042.txt, 1_043.txt, 1_044.txt, 1_045.txt, 1_046.txt, 1_047.txt, 1_048.txt, 1_049.txt, 1_050.txt, 1_051.txt, 1_052.txt, 1_053.txt, 1_054.txt, 1_055.txt, 1_056.txt, 1_057.txt, 1_058.txt, 1_059.txt, 1_060.txt, 1_061.txt, 1_062.txt, 1_063.txt, 1_064.txt, 1_065.txt, 1_066.txt, 1_067.txt, 1_068.txt, 1_069.txt, 1_070.txt, 1_071.txt, 1_072.txt, 1_073.txt, 1_074.txt, 1_075.txt, 1_076.txt, 1_077.txt, 1_078.txt, 1_079.txt, 1_080.txt, 1_081.txt, 1_082.txt, 1_083.txt, 1_084.txt, 1_085.txt, 1_086.txt, 1_087.txt, 1_088.txt, 1_089.txt, 1_090.txt, 1_091.txt, 1_092.txt, 1_093.txt, 1_094.txt, 1_095.txt, 1_096.txt, 1_097.txt, 1_098.txt, 1_099.txt, 1_100.txt, 1_101.txt, 1_102.txt, 1_103.txt, 1_104.txt, 1_105.txt, 1_106.txt, 1_107.txt, 1_108.txt, 1_109.txt, 1_110.txt, 1_111.txt, 1_112.txt, 1_113.txt, 1_114.txt, 1_115.txt, 1_116.txt
Case Name Status Exec Time Memory
0_000.txt AC 1 ms 256 KB
0_001.txt AC 1 ms 256 KB
0_002.txt AC 1 ms 256 KB
0_003.txt AC 1 ms 256 KB
1_003.txt AC 1 ms 256 KB
1_004.txt AC 1 ms 256 KB
1_005.txt AC 1 ms 256 KB
1_006.txt AC 1 ms 256 KB
1_007.txt AC 1 ms 256 KB
1_008.txt AC 1 ms 256 KB
1_009.txt AC 1 ms 256 KB
1_010.txt AC 1 ms 256 KB
1_011.txt AC 1 ms 256 KB
1_012.txt AC 1 ms 256 KB
1_013.txt AC 1 ms 256 KB
1_014.txt AC 1 ms 256 KB
1_015.txt AC 1 ms 256 KB
1_016.txt AC 1 ms 256 KB
1_017.txt AC 1 ms 256 KB
1_018.txt AC 1 ms 256 KB
1_019.txt AC 1 ms 256 KB
1_020.txt AC 1 ms 256 KB
1_021.txt AC 1 ms 256 KB
1_022.txt AC 1 ms 256 KB
1_023.txt AC 1 ms 256 KB
1_024.txt AC 1 ms 256 KB
1_025.txt AC 1 ms 256 KB
1_026.txt AC 1 ms 256 KB
1_027.txt AC 1 ms 256 KB
1_028.txt AC 1 ms 256 KB
1_029.txt AC 1 ms 256 KB
1_030.txt AC 1 ms 256 KB
1_031.txt AC 1 ms 256 KB
1_032.txt AC 1 ms 256 KB
1_033.txt AC 1 ms 256 KB
1_034.txt AC 1 ms 256 KB
1_035.txt AC 1 ms 256 KB
1_036.txt AC 1 ms 256 KB
1_037.txt AC 1 ms 256 KB
1_038.txt AC 1 ms 256 KB
1_039.txt AC 3 ms 256 KB
1_040.txt AC 1 ms 256 KB
1_041.txt AC 1 ms 256 KB
1_042.txt AC 1 ms 256 KB
1_043.txt AC 1 ms 256 KB
1_044.txt AC 1 ms 256 KB
1_045.txt AC 21 ms 4664 KB
1_046.txt AC 25 ms 5792 KB
1_047.txt AC 63 ms 12288 KB
1_048.txt AC 48 ms 9600 KB
1_049.txt AC 42 ms 8880 KB
1_050.txt AC 38 ms 8064 KB
1_051.txt AC 33 ms 9332 KB
1_052.txt AC 9 ms 2556 KB
1_053.txt AC 38 ms 10484 KB
1_054.txt AC 16 ms 4344 KB
1_055.txt AC 32 ms 9204 KB
1_056.txt AC 20 ms 5752 KB
1_057.txt AC 2 ms 512 KB
1_058.txt AC 28 ms 5760 KB
1_059.txt AC 22 ms 4480 KB
1_060.txt AC 49 ms 9216 KB
1_061.txt AC 56 ms 10240 KB
1_062.txt AC 27 ms 5760 KB
1_063.txt AC 31 ms 7292 KB
1_064.txt AC 23 ms 5244 KB
1_065.txt AC 4 ms 896 KB
1_066.txt AC 45 ms 9720 KB
1_067.txt AC 10 ms 2432 KB
1_068.txt AC 28 ms 6268 KB
1_069.txt AC 15 ms 3328 KB
1_070.txt AC 28 ms 5760 KB
1_071.txt AC 54 ms 9984 KB
1_072.txt AC 32 ms 6272 KB
1_073.txt AC 6 ms 1280 KB
1_074.txt AC 4 ms 896 KB
1_075.txt AC 25 ms 5248 KB
1_076.txt AC 56 ms 11648 KB
1_077.txt AC 51 ms 9728 KB
1_078.txt AC 18 ms 3712 KB
1_079.txt AC 24 ms 5120 KB
1_080.txt AC 29 ms 5888 KB
1_081.txt AC 61 ms 12240 KB
1_082.txt AC 60 ms 12264 KB
1_083.txt AC 67 ms 12952 KB
1_084.txt AC 65 ms 12240 KB
1_085.txt AC 55 ms 12752 KB
1_086.txt AC 56 ms 12880 KB
1_087.txt AC 41 ms 12148 KB
1_088.txt AC 42 ms 12148 KB
1_089.txt AC 44 ms 12148 KB
1_090.txt AC 45 ms 12148 KB
1_091.txt AC 42 ms 12148 KB
1_092.txt AC 45 ms 12148 KB
1_093.txt AC 63 ms 12236 KB
1_094.txt AC 63 ms 12620 KB
1_095.txt AC 67 ms 12364 KB
1_096.txt AC 62 ms 12592 KB
1_097.txt AC 62 ms 12460 KB
1_098.txt AC 64 ms 12512 KB
1_099.txt AC 52 ms 12024 KB
1_100.txt AC 51 ms 12024 KB
1_101.txt AC 57 ms 12024 KB
1_102.txt AC 57 ms 12024 KB
1_103.txt AC 52 ms 12024 KB
1_104.txt AC 54 ms 12024 KB
1_105.txt AC 59 ms 11984 KB
1_106.txt AC 61 ms 11980 KB
1_107.txt AC 60 ms 11984 KB
1_108.txt AC 67 ms 11980 KB
1_109.txt AC 56 ms 11980 KB
1_110.txt AC 62 ms 11980 KB
1_111.txt AC 60 ms 11984 KB
1_112.txt AC 61 ms 11980 KB
1_113.txt AC 65 ms 11980 KB
1_114.txt AC 65 ms 11980 KB
1_115.txt AC 58 ms 11984 KB
1_116.txt AC 58 ms 11984 KB