Submission #1833638


Source Code Expand

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <ctime>
#include<complex>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;


const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 2 * 1000 * 100 + 47;
const int MOD = 1000 * 1000 * 1000 + 7;

vector<int> g[MAX];
vector<int> SMIN[MAX];
vector<int> SMAX[MAX];
string s;
int PMIN[MAX];
int PMAX[MAX];
int P[MAX];
int CNT[MAX];
int H[MAX];

void dfs(int v, int p)
{
	int ind = -1;
	FOR(i, 0, SZ(g[v]))
	{
		if (g[v][i] == p)
		{
			ind = i;
			break;
		}
	}

	if (ind != -1)
	{
		g[v].erase(g[v].begin() + ind);
	}
	
	CNT[v] = s[v] == '1';
	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		dfs(to, v);
		H[v] = max(H[v], H[to] + 1);
		CNT[v] += CNT[to];
	}
}

void dfs1(int v)
{
	int pmax = -INF;
	int pmin = INF;

	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		PMIN[to] = min(pmin + 1, PMIN[v] + 1);
		if (i + 1 != SZ(g[v])) PMIN[to] = min(PMIN[to], SMIN[v][i + 1] + 1);

		PMAX[to] = max(pmax + 1, PMAX[v] + 1);
		if (i + 1 != SZ(g[v])) PMAX[to] = max(PMAX[to], SMAX[v][i + 1] + 1);

		pmax = max(pmax, H[to] + 1);
		pmin = min(pmin, H[to] + 1);
	}

	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		dfs1(to);
	}
}

vector<int> A;
int getMax2(int v)
{
	A.clear();
	A.push_back(PMAX[v]);
	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		A.push_back(H[to] + 1);
	}

	sort(ALL(A));
	reverse(ALL(A));
	if (SZ(g[v]) == 0)
	{
		if (A[0] > 1) return 1;
		return 0;
	}

	int ans = A[1] + 1;
	while (ans >= A[0]) ans--;

	return ans;
}

int getMin(int v)
{
	int ans = INF;
	if (CNT[0] != CNT[v])
	{
		ans = min(ans, PMIN[v]);
	}

	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		if (CNT[to] != 0)
		{
			ans = min(ans, H[to] + 1);
		}
	}


	return ans;
}

//#define DEBUG
int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	int n;
	cin >> n;
	FOR(i, 0, n - 1)
	{
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	cin >> s;
	dfs(0, 0);
#ifdef DEBUG
	cout << "H=" << endl;
	FOR(i, 0, n)
	{
		cout << i + 1 << " " << H[i] << endl;
	}
#endif
	FOR(v, 0, n)
	{
		SMIN[v].assign(SZ(g[v]), INF);
		SMAX[v].assign(SZ(g[v]), -INF);

		RFOR(i, SZ(g[v]), 0)
		{
			int to = g[v][i];
			if (i + 1 != SZ(g[v])) SMIN[v][i] = SMIN[v][i + 1];
			SMIN[v][i] = min(SMIN[v][i], H[to] + 1);

			if (i + 1 != SZ(g[v])) SMAX[v][i] = SMAX[v][i + 1];
			SMAX[v][i] = max(SMAX[v][i], H[to] + 1);
		}
	}

#ifdef DEBUG
	cout << "SMAX" << endl;
	FOR(i, 0, n)
	{
		cout << i + 1 << ": ";
		FOR(j, 0, SZ(g[i]))
		{
			cout << SMAX[i][j] << " ";
		}
		cout << endl;
	}
#endif
	PMIN[0] = 0;
	PMAX[0] = 0;
	dfs1(0);
	LL ans = 0;
#ifdef DEBUG
	cout << "PMAX=" << endl;
	FOR(i, 0, n)
	{
		cout << i + 1 << " " << PMAX[i] << endl;
	}

	cout << "2max" << endl;
	FOR(i, 0, n)
	{
		cout << i + 1 << " " << getMax2(i) << endl;
	}
#endif

	FOR(v, 0, SZ(s))
	{
		if (s[v] == '1')
		{
			ans += getMax2(v) + 1;
			continue;
		}

		ans += max(getMax2(v) - getMin(v) + 1, 0);
	}
	
	cout << ans + 1 << endl;
}

Submission Info

Submission Time
Task F - Black Radius
User philologist
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4044 Byte
Status RE
Exec Time 223 ms
Memory 16256 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:158:31: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
  freopen("in.txt", "r", stdin);
                               ^
./Main.cpp:159:33: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
  freopen("out.txt", "w", stdout);
                                 ^

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 0 / 1300 0 / 600
Status
RE × 3
RE × 31
RE × 81
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
Subtask 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt, 2_44.txt, 2_45.txt, 2_46.txt, 2_47.txt
Case Name Status Exec Time Memory
0_00.txt RE 104 ms 16256 KB
0_01.txt RE 104 ms 16256 KB
0_02.txt RE 101 ms 16256 KB
1_00.txt RE 102 ms 16256 KB
1_01.txt RE 102 ms 16256 KB
1_02.txt RE 100 ms 16256 KB
1_03.txt RE 102 ms 16256 KB
1_04.txt RE 102 ms 16256 KB
1_05.txt RE 104 ms 16256 KB
1_06.txt RE 101 ms 16256 KB
1_07.txt RE 100 ms 16256 KB
1_08.txt RE 101 ms 16256 KB
1_09.txt RE 101 ms 16256 KB
1_10.txt RE 100 ms 16256 KB
1_11.txt RE 100 ms 16256 KB
1_12.txt RE 100 ms 16256 KB
1_13.txt RE 100 ms 16256 KB
1_14.txt RE 100 ms 16256 KB
1_15.txt RE 100 ms 16256 KB
1_16.txt RE 101 ms 16256 KB
1_17.txt RE 101 ms 16256 KB
1_18.txt RE 101 ms 16256 KB
1_19.txt RE 101 ms 16256 KB
1_20.txt RE 101 ms 16256 KB
1_21.txt RE 100 ms 16256 KB
1_22.txt RE 101 ms 16256 KB
1_23.txt RE 100 ms 16256 KB
1_24.txt RE 101 ms 16256 KB
1_25.txt RE 101 ms 16256 KB
1_26.txt RE 100 ms 16256 KB
1_27.txt RE 100 ms 16256 KB
1_28.txt RE 100 ms 16256 KB
1_29.txt RE 100 ms 16256 KB
2_00.txt RE 100 ms 16256 KB
2_01.txt RE 100 ms 16256 KB
2_02.txt RE 100 ms 16256 KB
2_03.txt RE 100 ms 16256 KB
2_04.txt RE 101 ms 16256 KB
2_05.txt RE 101 ms 16256 KB
2_06.txt RE 100 ms 16256 KB
2_07.txt RE 101 ms 16256 KB
2_08.txt RE 100 ms 16256 KB
2_09.txt RE 101 ms 16256 KB
2_10.txt RE 102 ms 16256 KB
2_11.txt RE 102 ms 16256 KB
2_12.txt RE 101 ms 16256 KB
2_13.txt RE 101 ms 16256 KB
2_14.txt RE 101 ms 16256 KB
2_15.txt RE 104 ms 16256 KB
2_16.txt RE 104 ms 16256 KB
2_17.txt RE 100 ms 16256 KB
2_18.txt RE 103 ms 16256 KB
2_19.txt RE 105 ms 16256 KB
2_20.txt RE 223 ms 16256 KB
2_21.txt RE 100 ms 16256 KB
2_22.txt RE 100 ms 16256 KB
2_23.txt RE 101 ms 16256 KB
2_24.txt RE 100 ms 16256 KB
2_25.txt RE 100 ms 16256 KB
2_26.txt RE 101 ms 16256 KB
2_27.txt RE 101 ms 16256 KB
2_28.txt RE 101 ms 16256 KB
2_29.txt RE 100 ms 16256 KB
2_30.txt RE 100 ms 16256 KB
2_31.txt RE 100 ms 16256 KB
2_32.txt RE 101 ms 16256 KB
2_33.txt RE 101 ms 16256 KB
2_34.txt RE 100 ms 16256 KB
2_35.txt RE 100 ms 16256 KB
2_36.txt RE 101 ms 16256 KB
2_37.txt RE 101 ms 16256 KB
2_38.txt RE 99 ms 16256 KB
2_39.txt RE 101 ms 16256 KB
2_40.txt RE 100 ms 16256 KB
2_41.txt RE 100 ms 16256 KB
2_42.txt RE 101 ms 16256 KB
2_43.txt RE 100 ms 16256 KB
2_44.txt RE 101 ms 16256 KB
2_45.txt RE 100 ms 16256 KB
2_46.txt RE 101 ms 16256 KB
2_47.txt RE 100 ms 16256 KB