QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#101720#3859. Organizing SWERCPetroTarnavskyi#RE 0ms0kbC++171.7kb2023-04-30 22:05:022023-04-30 22:05:04

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-30 22:05:04]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 0kb
  • [2023-04-30 22:05:02]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#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 MP make_pair
#define PB push_back
#define F first
#define S second

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

const int N = 1'000'047;

int n;
vector<int> g[N];
int sz[N];
int centr = -1;

int dfs(int v, int p = -1) 
{
	sz[v] = 1;
	int mx = 0;
	for (auto to : g[v])
	{
		if (to != p)
		{	
			sz[v] += dfs(to, v);
			mx = max(mx, sz[to]);
		}
	}
	if (2 * mx < n && (n - sz[v]) * 2 < n)
		centr = v;
	return sz[v];
	
}

LL ans = 0;

int dfs2(int v, int p, int l)
{
	ans += l;
	int s = 1;
	for (auto to : g[v])
		if (to != p)
			s += dfs2(to, v, l + 1);
	return s;
}

VI F(int x, int cnt)
{
	VI res;
	int c = 1;
	while (c <= cnt)
	{
		res.PB(x * c);
		cnt -= c;
		c *= 2;
	} 
	if (cnt)
		res.PB(cnt * x);
	return res;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n;
	FOR(i, 1, n) {
		int p;
		cin >> p;
		p--;
		g[p].push_back(i);
		g[i].push_back(p);
	}
	
	dfs(0);
	
	VI v;
	assert(centr != -1);
	
	for (auto to : g[centr])
	{
		v.PB(dfs2(to, centr, 2));
	}
	
	sort(ALL(v));
	bitset<N> b;
	b[0] = 1;
	FOR(i, 0, SZ(v))
	{
		int j = i + 1;
		while (j < SZ(v) && v[j] == v[i]) j++;
		VI vec = F(v[i], j - i);
		i = j - 1;
		for (auto x : vec)
			b |= b << x;
	}
	
	LL ans2 = 0;
	FOR (i, 0, n)
	{
		if (b[i])
		{
			ans2 = max(ans2, LL(i) * (n - 1 - i));
		}
	}
	ans += ans2 + 1;
	cout << ans << '\n';
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

2
3
8 4
9 3
6 7
12
3 10
10 1
10 2
10 3
10 4
3 10
10 5
10 6
10 7
10 8
10 9
1 10

output:


result: