Submission #3923583


Source Code Expand

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-3;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;

int n;
vector<int> G[1 << 17];
bool exi[1 << 17];

bool used[1 << 17];
int root;
string c;
bool maketree(int id) {
	used[id] = true;
	bool res = false;
	rep(i, G[id].size()) {
		int to = G[id][i];
		if (used[to])continue;
		if (maketree(to))res = true;
	}
	if (c[id] == 'W')res = true;
	if (!res)exi[id] = false;
	return res;
}
int mostfar(int s) {
	fill(used, used + n, false);
	queue<int> q; used[s] = true; q.push(s);
	int res = s;
	while (!q.empty()) {
		int x = q.front(); q.pop(); res = x;
		rep(i,G[x].size()) {
			int to = G[x][i];
			if (used[to])continue;
			if (!exi[to])continue;
			used[to] = true; q.push(to);
		}
	}
	return res;
}
vector<int> road;
bool ontheroad[1 << 17];
int l, r;
int d[1 << 17];
void searchroad() {
	fill(used, used + n, false);
	queue<int> q; used[l] = true; q.push(l);
	int cnt = 1;
	while (!q.empty()) {
		int len = q.size();
		rep(j, len) {
			int x = q.front(); q.pop();
			d[x] = cnt;
			rep(i, G[x].size()) {
				int to = G[x][i];
				if (used[to])continue;
				if (!exi[to])continue;
				used[to] = true; q.push(to);
			}
		}
		cnt++;
	}
	int cur = r; road.push_back(r); ontheroad[r] = true;
	while (cur != l) {
		rep(i, G[cur].size()) {
			int to = G[cur][i];
			if (d[to] == d[cur] - 1) {
				road.push_back(to);
				ontheroad[to] = true;
				cur = to;
				break;
			}
		}
	}
	
}
int dfs(int id) {
	int res = 0;
	int needed = (c[id] == 'W');
	needed ^= 1;
	used[id] = true;
	rep(j, G[id].size()) {
		int to = G[id][j];
		if (used[to])continue;
		if (!exi[to])continue;
		res += 2 + dfs(to); needed ^= 1;
	}
	if (needed)res++;
	return res;
}
int calc() {
	fill(used, used + n, false);
	int res = 1;
	rep(i, road.size()) {
		int x = road[i]; used[x] = true;
		int needed = (c[x] == 'W');
		needed ^= 1;
		rep(j, G[x].size()) {
			int to = G[x][j];
			if (!exi[to])continue;
			if (ontheroad[to])continue;
			res += 2 + dfs(to); needed ^= 1;
		}
		if (needed)res++;
	}
	res += road.size() - 1;
	return res;
}
int main() {
	int num = 0;
	cin >> n; fill(exi, exi + n, true);
	rep(i, n - 1) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	cin >> c;
	root = -1;
	rep(i, n) {
		if (c[i] == 'W') {
			root = i; num++;
		}
	}
	if (root < 0) {
		cout << 0 << endl; return 0;
	}
	else if (num == 1) {
		cout << 1 << endl; return 0;
	}
	maketree(root);
	l = mostfar(root), r = mostfar(l);
	searchroad();
	cout << calc() << endl;

	//stop
	return 0;
}

Submission Info

Submission Time
Task F - Monochrome Cat
User heno239
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3486 Byte
Status WA
Exec Time 121 ms
Memory 12668 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 800
Status
AC × 4
AC × 102
WA × 16
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 3 ms 3328 KB
0_001.txt AC 3 ms 3328 KB
0_002.txt AC 3 ms 3328 KB
0_003.txt AC 3 ms 3328 KB
1_003.txt AC 3 ms 3328 KB
1_004.txt AC 3 ms 3328 KB
1_005.txt AC 3 ms 3328 KB
1_006.txt AC 3 ms 3328 KB
1_007.txt AC 3 ms 3328 KB
1_008.txt AC 3 ms 3328 KB
1_009.txt AC 3 ms 3328 KB
1_010.txt AC 3 ms 3328 KB
1_011.txt AC 3 ms 3328 KB
1_012.txt AC 3 ms 3328 KB
1_013.txt AC 3 ms 3328 KB
1_014.txt AC 3 ms 3328 KB
1_015.txt AC 3 ms 3328 KB
1_016.txt AC 3 ms 3328 KB
1_017.txt AC 3 ms 3328 KB
1_018.txt AC 3 ms 3328 KB
1_019.txt AC 3 ms 3328 KB
1_020.txt AC 3 ms 3328 KB
1_021.txt AC 3 ms 3328 KB
1_022.txt AC 3 ms 3328 KB
1_023.txt AC 3 ms 3328 KB
1_024.txt AC 3 ms 3328 KB
1_025.txt AC 3 ms 3328 KB
1_026.txt AC 3 ms 3328 KB
1_027.txt AC 3 ms 3328 KB
1_028.txt AC 3 ms 3328 KB
1_029.txt AC 3 ms 3328 KB
1_030.txt AC 3 ms 3328 KB
1_031.txt AC 3 ms 3328 KB
1_032.txt AC 3 ms 3328 KB
1_033.txt AC 3 ms 3328 KB
1_034.txt AC 3 ms 3328 KB
1_035.txt AC 3 ms 3328 KB
1_036.txt AC 3 ms 3328 KB
1_037.txt AC 3 ms 3328 KB
1_038.txt AC 3 ms 3328 KB
1_039.txt AC 3 ms 3328 KB
1_040.txt AC 3 ms 3328 KB
1_041.txt AC 3 ms 3328 KB
1_042.txt AC 3 ms 3328 KB
1_043.txt AC 3 ms 3328 KB
1_044.txt AC 3 ms 3328 KB
1_045.txt AC 38 ms 6016 KB
1_046.txt AC 47 ms 6656 KB
1_047.txt AC 77 ms 6784 KB
1_048.txt AC 58 ms 5888 KB
1_049.txt AC 79 ms 10108 KB
1_050.txt AC 69 ms 8192 KB
1_051.txt AC 61 ms 6904 KB
1_052.txt AC 16 ms 4224 KB
1_053.txt AC 63 ms 6648 KB
1_054.txt AC 27 ms 4732 KB
1_055.txt AC 61 ms 6776 KB
1_056.txt AC 37 ms 5116 KB
1_057.txt AC 4 ms 3456 KB
1_058.txt AC 48 ms 6400 KB
1_059.txt AC 28 ms 4608 KB
1_060.txt AC 59 ms 5888 KB
1_061.txt AC 91 ms 8832 KB
1_062.txt AC 46 ms 6272 KB
1_063.txt WA 56 ms 5888 KB
1_064.txt WA 40 ms 5120 KB
1_065.txt AC 7 ms 3584 KB
1_066.txt AC 62 ms 6268 KB
1_067.txt AC 18 ms 4096 KB
1_068.txt WA 45 ms 5504 KB
1_069.txt WA 27 ms 4352 KB
1_070.txt WA 47 ms 5248 KB
1_071.txt AC 67 ms 6272 KB
1_072.txt AC 41 ms 5120 KB
1_073.txt AC 11 ms 3712 KB
1_074.txt WA 8 ms 3584 KB
1_075.txt WA 43 ms 5120 KB
1_076.txt AC 102 ms 7296 KB
1_077.txt AC 65 ms 6144 KB
1_078.txt AC 24 ms 4352 KB
1_079.txt AC 43 ms 5120 KB
1_080.txt WA 47 ms 5248 KB
1_081.txt AC 121 ms 10492 KB
1_082.txt AC 115 ms 11260 KB
1_083.txt AC 77 ms 6784 KB
1_084.txt AC 77 ms 6784 KB
1_085.txt AC 115 ms 12668 KB
1_086.txt AC 117 ms 12412 KB
1_087.txt AC 78 ms 7928 KB
1_088.txt AC 80 ms 7800 KB
1_089.txt AC 74 ms 7160 KB
1_090.txt AC 71 ms 7160 KB
1_091.txt AC 78 ms 7928 KB
1_092.txt AC 71 ms 7160 KB
1_093.txt AC 109 ms 8960 KB
1_094.txt AC 109 ms 9216 KB
1_095.txt AC 78 ms 6784 KB
1_096.txt AC 78 ms 6784 KB
1_097.txt AC 110 ms 8960 KB
1_098.txt AC 107 ms 9088 KB
1_099.txt WA 93 ms 7676 KB
1_100.txt WA 93 ms 7548 KB
1_101.txt AC 75 ms 7036 KB
1_102.txt AC 75 ms 7036 KB
1_103.txt AC 94 ms 7676 KB
1_104.txt WA 89 ms 7548 KB
1_105.txt WA 106 ms 7424 KB
1_106.txt WA 105 ms 7424 KB
1_107.txt AC 78 ms 6784 KB
1_108.txt AC 78 ms 6784 KB
1_109.txt AC 106 ms 7424 KB
1_110.txt WA 101 ms 7424 KB
1_111.txt WA 105 ms 7424 KB
1_112.txt WA 105 ms 7424 KB
1_113.txt AC 78 ms 6784 KB
1_114.txt AC 78 ms 6784 KB
1_115.txt AC 106 ms 7424 KB
1_116.txt AC 101 ms 7424 KB