Submission #1883031


Source Code Expand

#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N;
int S[MAX_N];
int X[MAX_N];
vector<int> G[MAX_N];

bool loop(int v, int p) {
	vector<vi> dp(2, vi(X[v] + 1, inf));
	int now = 0, next = 1;
	dp[0][0] = 0;
	rep(i, 0, sz(G[v])) {
		fill(dp[next].begin(), dp[next].begin() + X[v] + 1, inf);
		int n = G[v][i];
		loop(n, v);
		rep(w, 0, X[v] + 1) {
			if(dp[now][w] == inf) continue;
			if(w + X[n] <= X[v]) MIN(dp[next][w + X[n]], dp[now][w] + S[n]);
			if(w + S[n] <= X[v]) MIN(dp[next][w + S[n]], dp[now][w] + X[n]);
		}
		swap(now, next);
	}
	S[v] = inf;
	rep(i, 0, X[v] + 1) MIN(S[v], dp[now][i]);
	if(S[v] != inf) return true;
	else return false;
}
		

void solve() {
	cin >> N;
	fill(S, S + N, inf);
	rep(i, 0, N - 1) {
		int a; cin >> a; a--;
		G[a].pb(i + 1);
	}
	rep(i, 0, N) cin >> X[i];
	if(loop(0, -1)) cout << "POSSIBLE\n";
	else cout << "IMPOSSIBLE\n";
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
#ifdef LOCAL
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

Submission Info

Submission Time
Task E - Bichrome Tree
User omochana2
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2685 Byte
Status AC
Exec Time 19 ms
Memory 17320 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 44
Set Name Test Cases
Sample subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
Case Name Status Exec Time Memory
01.txt AC 4 ms 6400 KB
02.txt AC 10 ms 6744 KB
03.txt AC 19 ms 17320 KB
04.txt AC 4 ms 6400 KB
05.txt AC 4 ms 6400 KB
06.txt AC 5 ms 6400 KB
07.txt AC 4 ms 6400 KB
08.txt AC 6 ms 6528 KB
09.txt AC 6 ms 6528 KB
10.txt AC 12 ms 13564 KB
11.txt AC 8 ms 6716 KB
12.txt AC 4 ms 6528 KB
13.txt AC 6 ms 8704 KB
14.txt AC 4 ms 6400 KB
15.txt AC 6 ms 8576 KB
16.txt AC 4 ms 6400 KB
17.txt AC 6 ms 8576 KB
18.txt AC 6 ms 8448 KB
19.txt AC 4 ms 6400 KB
20.txt AC 4 ms 6400 KB
21.txt AC 9 ms 6756 KB
22.txt AC 13 ms 13636 KB
23.txt AC 6 ms 6528 KB
24.txt AC 6 ms 6528 KB
25.txt AC 5 ms 6400 KB
26.txt AC 6 ms 8576 KB
27.txt AC 6 ms 8576 KB
28.txt AC 6 ms 8448 KB
29.txt AC 6 ms 8576 KB
30.txt AC 6 ms 8576 KB
31.txt AC 6 ms 8576 KB
32.txt AC 6 ms 8576 KB
33.txt AC 6 ms 8576 KB
34.txt AC 6 ms 8576 KB
35.txt AC 6 ms 8576 KB
36.txt AC 6 ms 8576 KB
37.txt AC 6 ms 8576 KB
38.txt AC 6 ms 8576 KB
39.txt AC 6 ms 8576 KB
40.txt AC 6 ms 8704 KB
subtask0_0.txt AC 3 ms 6400 KB
subtask0_1.txt AC 3 ms 6400 KB
subtask0_2.txt AC 3 ms 6400 KB
subtask0_3.txt AC 3 ms 6400 KB