Submission #2503812
Source Code Expand
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <limits> #include <ctime> #include <cassert> #include <map> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <stack> #include <queue> #include <numeric> #include <iterator> #include <bitset> #include <unordered_map> #include <unordered_set> using namespace std; #define FOR(i,n) for(int i = 0; i < (n); i++) #define sz(c) ((int)c.size()) #define ten(n) ((int)1e##n) using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; template<typename ...> static inline int getchar_unlocked(void) { return getchar(); } template<typename ...> static inline void putchar_unlocked(int c) { putchar(c); } #define mygc(c) (c)=getchar_unlocked() #define mypc(c) putchar_unlocked(c) void reader(int& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; } void reader(ll& x) { int k, m = 0; x = 0; for (;;) { mygc(k); if (k == '-') { m = 1; break; }if ('0' <= k&&k <= '9') { x = k - '0'; break; } }for (;;) { mygc(k); if (k<'0' || k>'9')break; x = x * 10 + k - '0'; }if (m) x = -x; } int reader(char c[]) { int i, s = 0; for (;;) { mygc(i); if (i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF) break; }c[s++] = i; for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c[s++] = i; }c[s] = '\0'; return s; } int reader(string& c) { int i; for (;;) { mygc(i); if (i != ' '&&i != '\n'&&i != '\r'&&i != '\t'&&i != EOF) break; }c.push_back(i); for (;;) { mygc(i); if (i == ' ' || i == '\n' || i == '\r' || i == '\t' || i == EOF) break; c.push_back(i); }; return sz(c); } template <class T, class S> void reader(T& x, S& y) { reader(x); reader(y); } template <class T, class S, class U> void reader(T& x, S& y, U& z) { reader(x); reader(y); reader(z); } template <class T, class S, class U, class V> void reader(T& x, S& y, U& z, V & w) { reader(x); reader(y); reader(z); reader(w); } void writer(int x, char c) { int s = 0, m = 0; char f[10]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); } void writer(ll x, char c) { int s = 0, m = 0; char f[20]; if (x<0)m = 1, x = -x; while (x)f[s++] = x % 10, x /= 10; if (!s)f[s++] = 0; if (m)mypc('-'); while (s--)mypc(f[s] + '0'); mypc(c); } void writer(const char c[]) { int i; for (i = 0; c[i] != '\0'; i++)mypc(c[i]); } void writer(const string& x, char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); } void writer(const char x[], char c) { int i; for (i = 0; x[i] != '\0'; i++)mypc(x[i]); mypc(c); } template<class T> void writerLn(T x) { writer(x, '\n'); } template<class T, class S> void writerLn(T x, S y) { writer(x, ' '); writer(y, '\n'); } template<class T, class S, class U> void writerLn(T x, S y, U z) { writer(x, ' '); writer(y, ' '); writer(z, '\n'); } template<class T, class S, class U, class V> void writerLn(T x, S y, U z, V v) { writer(x, ' '); writer(y, ' '); writer(z, ' '); writer(v, '\n'); } template<class T> void writerArr(T x[], int n) { if (!n) { mypc('\n'); return; }FOR(i, n - 1)writer(x[i], ' '); writer(x[n - 1], '\n'); } template<class T> void writerArr(vector<T>& x) { writerArr(x.data(), (int)x.size()); } template<class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template<class T> void chmax(T& a, const T& b) { if (a < b) a = b; } template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; } ll mod_inv(ll a, ll m) { ll x, y; extgcd<ll>(a, m, x, y); return (m + x % m) % m; } bool black[ten(5)]; vector<int> e[ten(5)]; bool hasW[ten(5)]; bool dfs1(int v, int par) { bool ret = !black[v]; for (auto to : e[v]) { if (to == par) continue; ret |= dfs1(to, v); } return hasW[v] = ret; } bool misMatch[ten(5)]; int memo2[ten(5)]; int maxDfs2Res = 0; void dfs2(int v, int par) { if (!hasW[v]) { return ; } for (auto to : e[v]) { if (to == par) continue; if (!hasW[to]) continue; dfs2(to, v); } static vector<int> childs; childs.clear(); for (auto to : e[v]) { if (to == par) continue; if (!hasW[to]) continue; childs.push_back(memo2[to]); } int longest = misMatch[v]; if (sz(childs) >= 1) { sort(childs.rbegin(), childs.rend()); longest += childs[0]; int ans = longest; if (sz(childs) >= 2) { ans += childs[1]; } chmax(maxDfs2Res, ans); } memo2[v] = longest; return; } int main() { int n; reader(n); FOR(i, n - 1) { int a, b; reader(a, b); a--; b--; e[a].push_back(b); e[b].push_back(a); } int cnt = 0; { string s; reader(s); FOR(i, n) black[i] = (s[i] == 'B'); FOR(i, n) cnt += !black[i]; } if (cnt <= 1) { writerLn(cnt); return 0; } int root; FOR(i, n) if (!black[i]) { root = i; break; } dfs1(root, -1); int eTourPath = 0; FOR(i, n) if (hasW[i]) { int aEdges = 0; for (auto to : e[i]) { if (hasW[to]) aEdges++; } eTourPath += aEdges; if (black[i] && aEdges % 2 != 0) { misMatch[i] = true; } if (!black[i] && aEdges % 2 == 0) { misMatch[i] = true; } eTourPath += misMatch[i]; } dfs2(root, -1); int ans = eTourPath - maxDfs2Res * 2; writerLn(ans); return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Monochrome Cat |
User | math |
Language | C++14 (GCC 5.4.1) |
Score | 800 |
Code Size | 5958 Byte |
Status | AC |
Exec Time | 43 ms |
Memory | 17312 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 800 / 800 | ||||
Status |
|
|
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 | 2 ms | 2560 KB |
0_001.txt | AC | 2 ms | 2560 KB |
0_002.txt | AC | 2 ms | 2560 KB |
0_003.txt | AC | 2 ms | 2560 KB |
1_003.txt | AC | 2 ms | 2560 KB |
1_004.txt | AC | 2 ms | 2560 KB |
1_005.txt | AC | 2 ms | 2560 KB |
1_006.txt | AC | 2 ms | 2560 KB |
1_007.txt | AC | 2 ms | 2560 KB |
1_008.txt | AC | 2 ms | 2560 KB |
1_009.txt | AC | 2 ms | 2560 KB |
1_010.txt | AC | 2 ms | 2560 KB |
1_011.txt | AC | 2 ms | 2560 KB |
1_012.txt | AC | 2 ms | 2560 KB |
1_013.txt | AC | 2 ms | 2560 KB |
1_014.txt | AC | 2 ms | 2560 KB |
1_015.txt | AC | 2 ms | 2560 KB |
1_016.txt | AC | 2 ms | 2560 KB |
1_017.txt | AC | 2 ms | 2560 KB |
1_018.txt | AC | 2 ms | 2560 KB |
1_019.txt | AC | 2 ms | 2560 KB |
1_020.txt | AC | 2 ms | 2560 KB |
1_021.txt | AC | 2 ms | 2560 KB |
1_022.txt | AC | 2 ms | 2560 KB |
1_023.txt | AC | 2 ms | 2560 KB |
1_024.txt | AC | 2 ms | 2560 KB |
1_025.txt | AC | 2 ms | 2560 KB |
1_026.txt | AC | 2 ms | 2560 KB |
1_027.txt | AC | 2 ms | 2560 KB |
1_028.txt | AC | 2 ms | 2560 KB |
1_029.txt | AC | 2 ms | 2560 KB |
1_030.txt | AC | 2 ms | 2560 KB |
1_031.txt | AC | 2 ms | 2560 KB |
1_032.txt | AC | 2 ms | 2560 KB |
1_033.txt | AC | 2 ms | 2560 KB |
1_034.txt | AC | 2 ms | 2560 KB |
1_035.txt | AC | 2 ms | 2560 KB |
1_036.txt | AC | 2 ms | 2560 KB |
1_037.txt | AC | 2 ms | 2560 KB |
1_038.txt | AC | 2 ms | 2560 KB |
1_039.txt | AC | 2 ms | 2560 KB |
1_040.txt | AC | 2 ms | 2560 KB |
1_041.txt | AC | 2 ms | 2560 KB |
1_042.txt | AC | 2 ms | 2560 KB |
1_043.txt | AC | 2 ms | 2560 KB |
1_044.txt | AC | 2 ms | 2560 KB |
1_045.txt | AC | 14 ms | 6912 KB |
1_046.txt | AC | 18 ms | 8960 KB |
1_047.txt | AC | 20 ms | 6016 KB |
1_048.txt | AC | 15 ms | 5248 KB |
1_049.txt | AC | 29 ms | 13112 KB |
1_050.txt | AC | 24 ms | 10368 KB |
1_051.txt | AC | 15 ms | 6520 KB |
1_052.txt | AC | 5 ms | 3456 KB |
1_053.txt | AC | 12 ms | 5880 KB |
1_054.txt | AC | 6 ms | 3964 KB |
1_055.txt | AC | 15 ms | 6392 KB |
1_056.txt | AC | 7 ms | 4348 KB |
1_057.txt | AC | 3 ms | 2688 KB |
1_058.txt | AC | 18 ms | 6656 KB |
1_059.txt | AC | 8 ms | 3840 KB |
1_060.txt | AC | 15 ms | 5248 KB |
1_061.txt | AC | 30 ms | 9008 KB |
1_062.txt | AC | 16 ms | 6528 KB |
1_063.txt | AC | 16 ms | 5248 KB |
1_064.txt | AC | 12 ms | 4480 KB |
1_065.txt | AC | 3 ms | 2816 KB |
1_066.txt | AC | 14 ms | 5500 KB |
1_067.txt | AC | 7 ms | 3456 KB |
1_068.txt | AC | 13 ms | 4736 KB |
1_069.txt | AC | 9 ms | 3712 KB |
1_070.txt | AC | 15 ms | 4480 KB |
1_071.txt | AC | 17 ms | 5504 KB |
1_072.txt | AC | 11 ms | 4352 KB |
1_073.txt | AC | 5 ms | 2944 KB |
1_074.txt | AC | 4 ms | 2816 KB |
1_075.txt | AC | 14 ms | 4352 KB |
1_076.txt | AC | 33 ms | 6560 KB |
1_077.txt | AC | 17 ms | 5376 KB |
1_078.txt | AC | 7 ms | 3584 KB |
1_079.txt | AC | 14 ms | 4352 KB |
1_080.txt | AC | 15 ms | 4608 KB |
1_081.txt | AC | 40 ms | 12448 KB |
1_082.txt | AC | 43 ms | 16032 KB |
1_083.txt | AC | 21 ms | 6016 KB |
1_084.txt | AC | 20 ms | 6016 KB |
1_085.txt | AC | 42 ms | 17312 KB |
1_086.txt | AC | 41 ms | 16288 KB |
1_087.txt | AC | 20 ms | 7416 KB |
1_088.txt | AC | 19 ms | 7160 KB |
1_089.txt | AC | 14 ms | 6392 KB |
1_090.txt | AC | 14 ms | 6392 KB |
1_091.txt | AC | 20 ms | 7416 KB |
1_092.txt | AC | 14 ms | 6392 KB |
1_093.txt | AC | 37 ms | 9756 KB |
1_094.txt | AC | 38 ms | 11804 KB |
1_095.txt | AC | 21 ms | 6016 KB |
1_096.txt | AC | 20 ms | 6016 KB |
1_097.txt | AC | 38 ms | 11424 KB |
1_098.txt | AC | 36 ms | 11164 KB |
1_099.txt | AC | 27 ms | 6936 KB |
1_100.txt | AC | 27 ms | 6812 KB |
1_101.txt | AC | 18 ms | 6268 KB |
1_102.txt | AC | 18 ms | 6268 KB |
1_103.txt | AC | 27 ms | 6908 KB |
1_104.txt | AC | 25 ms | 6808 KB |
1_105.txt | AC | 34 ms | 6556 KB |
1_106.txt | AC | 33 ms | 6560 KB |
1_107.txt | AC | 21 ms | 6144 KB |
1_108.txt | AC | 21 ms | 6044 KB |
1_109.txt | AC | 33 ms | 6556 KB |
1_110.txt | AC | 32 ms | 6560 KB |
1_111.txt | AC | 33 ms | 6556 KB |
1_112.txt | AC | 33 ms | 6556 KB |
1_113.txt | AC | 21 ms | 6044 KB |
1_114.txt | AC | 20 ms | 6144 KB |
1_115.txt | AC | 33 ms | 6428 KB |
1_116.txt | AC | 32 ms | 6556 KB |