社区讨论

60pts求调,wa on #7#8#9#10

P2607[ZJOI2008] 骑士参与者 2已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lo1lof43
此快照首次捕获于
2023/10/22 23:04
2 年前
此快照最后确认于
2023/11/02 23:48
2 年前
查看原帖
也不知道为什么,总之就是wa了4个点,调了两月了没调出来,ma了已经,求助大佬
CPP
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
struct node {
	long long to, nxt;
}bian[2 * maxn];
long long head[maxn], cnt = 1;

long long n, v[maxn], dp[maxn][2], ans, pit1, pit2;
bool flag[maxn], f = 0;
void add(int x, int y) {
	bian[++cnt].nxt = head[x];
	bian[cnt].to = y;
	head[x] = cnt;
}
void dfs(int x, int from) {
	flag[x] = 1;
	//mem[++mem[0]]=x;
	for (int i = head[x]; i; i = bian[i].nxt) {
		int y = bian[i].to;
		if (y == from) continue;
		if (!flag[y]) dfs(y, x);
		else if (flag[y] && !f) {
			f = true;//find circle!!
			pit1 = x, pit2 = y;
		}
	}
}
void f_dp(int x, int from) {
	dp[x][0] = 0, dp[x][1] = v[x];
	for (int i = head[x]; i; i = bian[i].nxt) {
		if (bian[i].to == 0 || bian[i].to == from) continue;
		f_dp(bian[i].to, x);
		dp[x][0] += max(dp[bian[i].to][0], dp[bian[i].to][1]);
		dp[x][1] += dp[bian[i].to][0];
	}
}
int main() {
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	cin >> n;
	int x;
	for (int i = 1; i <= n; i++) {
		cin >> v[i] >> x;
		add(x, i), add(i, x);
	}
	for (int i = 1; i <= n; i++) {
		if (flag[i]) continue;
		f = 0;
		dfs(i, -1);
		if (!f) {
			f_dp(i, -1);
			ans += max(dp[i][0], dp[i][1]);
		}
		else {
			for (int i = head[pit1]; i; i = bian[i].nxt) {
				if (bian[i].to == pit2) {
					bian[i].to = 0;
					bian[i ^ 1].to = 0;
					break;
				}
			}
			f_dp(pit1, -1);
			long long mx = max(dp[pit1][0], dp[pit1][1]);
			f_dp(pit2, -1);
			ans += max(mx, max(dp[pit2][0], dp[pit2][1]));
		}
	}
	//cout<<ans;
	cout << ans;
	return 0;
}
/*
12 10 3
10 2
20 3
30 1
10 5
20 6
30 4
10 9
10 10
20 11
20 12
30 7
30 8
*/

回复

5 条回复,欢迎继续交流。

正在加载回复...