Submission #645217

# Submission time Handle Problem Language Result Execution time Memory
645217 2022-09-26T12:50:52 Z TimDee Arboras (RMI20_arboras) C++17
11 / 100
14 ms 3512 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i,n) for (int i=0; i<n; ++i)
#define pi pair<int,int>
#define ppi pair<int,pi>
#define f first
#define s second
const int mod = 1e9+7;

vector<vector<int>> adj(1e5+3);
vector<int> d(1e5+3,0);

ppi dfs(int u, int p) {
	ppi ret={0,{0,0}};
	for (auto v:adj[u]) {
		if (v==p) continue;
		ppi k=dfs(v,u);
		ret.f+=k.f; ret.f%=mod;
		ret.s.f=max(ret.s.f,k.s.s+d[v]);
		if (ret.s.f>ret.s.s) swap(ret.s.f,ret.s.s);

	}
	ret.f+=ret.s.f+ret.s.s;
	ret.f%=mod;
	return ret;
}

void solve() {
	int n; cin>>n;
	if (n>1000) return;
	forn(i,n-1) {
		int v; cin>>v;
		adj[v].push_back(i+1);
	}
	forn(i,n-1) cin>>d[i+1];
	cout<<dfs(0,-1).f<<'\n';

	int Q; cin>>Q;
	forn(q,Q) {
		int i,x; cin>>i>>x;
		d[i]+=x;
		int ans=0;
		cout<<dfs(0,-1).f<<'\n';
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	solve();
	return 0;
}

Compilation message

arboras.cpp: In function 'void solve()':
arboras.cpp:43:7: warning: unused variable 'ans' [-Wunused-variable]
   43 |   int ans=0;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 3412 KB Output is correct
2 Correct 14 ms 3472 KB Output is correct
3 Correct 10 ms 3512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 3412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 3412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 3412 KB Output is correct
2 Correct 14 ms 3472 KB Output is correct
3 Correct 10 ms 3512 KB Output is correct
4 Incorrect 2 ms 3412 KB Output isn't correct
5 Halted 0 ms 0 KB -