QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129343#4406. 独立集问题complexor0 52ms72676kbC++141.6kb2023-07-22 15:45:272023-07-22 15:45:29

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-22 15:45:29]
  • 评测
  • 测评结果:0
  • 用时:52ms
  • 内存:72676kb
  • [2023-07-22 15:45:27]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <bitset>
#include <vector>
#include <set>
#include <cstring>
typedef long long LL;
typedef std::pair<int, int> pii;
typedef std::pair<LL, LL> pll;
#define MP std::make_pair
#define fi first 
#define se second 
int read()
{
	int s = 0, f = 1;
	char c = getchar();
	while (!(c >= '0' && c <= '9'))
		f ^= c == '-', c = getchar();
	while (c >= '0' && c <= '9')
		s = s * 10 + (c - '0'), c = getchar();
	return f ? s : -s;
}
const int MAXN = 400005, INF = 2e9;
int n, a[MAXN], f[MAXN][3][2];
std::vector<int> e[MAXN];
void dfs(int x, int fat)
{
	int ap = 0, bp = 0, dp = -INF;
	int am = 0, bm = 0, dm = -INF;
	for (int y : e[x])
	{
		dfs(y, x);
		ap += std::max({f[y][0][0] - a[y], f[y][1][0], f[y][2][0]});
		am += std::max({f[y][0][0] + a[y], f[y][1][0], f[y][2][0]});
		bp += std::max(f[y][0][0] - a[y], f[y][1][0]);
		bm += std::max(f[y][0][0] + a[y], f[y][1][0]);
		dp = std::max(dp, std::max(f[y][1][1], f[y][2][1]) - std::max({f[y][0][0] - a[y], f[y][1][0], f[y][2][0]}));
		dm = std::max(dp, std::max(f[y][1][1], f[y][2][1]) - std::max({f[y][0][0] + a[y], f[y][1][0], f[y][2][0]}));
	}
	f[x][0][0] = std::max(ap, am);
	f[x][1][0] = std::max(ap + dp, am + dm);
	f[x][1][1] = std::max(ap + dp - a[fat], am + dm + a[fat]);
	f[x][2][0] = std::max(bp + a[x], bm - a[x]);
	f[x][2][1] = std::max(bp + a[x] - a[fat], bm - a[x] + a[fat]);
}
int main()
{
	n = read();
	for (int i = 1; i <= n; i++)
		a[i] = read();
	for (int i = 2; i <= n; i++)
		e[read()].push_back(i);
	dfs(1, 0);
	printf("%d\n", std::max(f[1][1][0], f[1][2][0]));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 39ms
memory: 72676kb

input:

351493
-641495974 -401868912 -864400429 35718599 -579296290 767835327 149511992 -246228754 649472235 893048442 -292675192 -788090312 -578172296 -62289673 196890164 -120059197 -452848333 -216788131 -604555771 -240241020 376984486 -407661514 -107574590 -461679558 -470560314 -583391402 -991686079 76956...

output:

1422947353

result:

wrong answer 1st numbers differ - expected: '175477002777567', found: '1422947353'

Subtask #2:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 52ms
memory: 31640kb

input:

351493
915594742 688454502 456077914 208864399 625937959 102483811 538999077 529481828 400375958 387560315 83710527 83424975 330114353 684812426 68052931 28849220 295907801 535129167 967891325 124069427 644256858 757666812 558755455 178666038 177054898 795236216 848264282 669310447 328353372 3681163...

output:

79794045

result:

wrong answer 1st numbers differ - expected: '175766699890054', found: '79794045'

Subtask #3:

score: 0
Wrong Answer

Test #41:

score: 0
Wrong Answer
time: 1ms
memory: 16096kb

input:

7
247522519 398923496 -976223527 806215585 -937536479 -130552271 90576461
1 2 1 2 5 5

output:

1506590053

result:

wrong answer 1st numbers differ - expected: '3587550338', found: '1506590053'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%