Submission #1691367
Source Code Expand
#include "iostream" #include "climits" #include "list" #include "queue" #include "stack" #include "set" #include "functional" #include "algorithm" #include "string" #include "map" #include "iomanip" #include "random" using namespace std; const long long int MOD = 1000000007; const long double EPS = 0.00000001; const long double PI = 3.1415926535897932384626433; long long int N, M, K, H, W, L, R; vector<vector<int>>edge; vector<pair<int,int>>num; vector<int>value; bool fin = false; pair<int, int> Search(int n) { if (fin) { num[n] = { 0,0 }; return num[n]; } if (num[n].first != -1) { return num[n]; } if (edge[n].empty()) { num[n] = { value[n],0 }; return num[n]; } vector<vector<bool>>flag(edge[n].size() + 1, vector<bool>(value[n] + 1, false)); flag[0][0] = true; int box = 1; int sum = 0; for (auto i : edge[n]) { auto bag = Search(i); L = bag.first; R = bag.second; sum += L; sum += R; for (int j = value[n]; j >= L; j--) { flag[box][j] = flag[box - 1][j - L]; } for (int j = value[n]; j >= R; j--) { if (!flag[box][j]) { flag[box][j] = flag[box - 1][j - R]; } } box++; } for (int i = value[n]; i >= 0; i--) { if (flag[edge[n].size()][i]) { //cout << n << " " << i << endl; num[n] = { value[n],sum - i }; return num[n]; } } fin = true; num[n] = { 0,0 }; return num[n]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; edge.resize(N + 1); num.resize(N + 1, { -1,-1 }); value.resize(N + 1); for (int i = 2; i <= N; i++) { cin >> K; edge[K].push_back(i); } for (int i = 1; i <= N; i++) { cin >> value[i]; } Search(1); if (fin) { cout << "IMPOSSIBLE\n"; } else { cout << "POSSIBLE\n"; } return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Bichrome Tree |
User | olphe |
Language | C++14 (GCC 5.4.1) |
Score | 700 |
Code Size | 1819 Byte |
Status | AC |
Exec Time | 10 ms |
Memory | 896 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 700 / 700 | ||||
Status |
|
|
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 | 2 ms | 384 KB |
02.txt | AC | 2 ms | 384 KB |
03.txt | AC | 8 ms | 896 KB |
04.txt | AC | 2 ms | 256 KB |
05.txt | AC | 3 ms | 384 KB |
06.txt | AC | 5 ms | 384 KB |
07.txt | AC | 4 ms | 384 KB |
08.txt | AC | 10 ms | 384 KB |
09.txt | AC | 8 ms | 384 KB |
10.txt | AC | 2 ms | 384 KB |
11.txt | AC | 2 ms | 384 KB |
12.txt | AC | 5 ms | 384 KB |
13.txt | AC | 3 ms | 512 KB |
14.txt | AC | 2 ms | 384 KB |
15.txt | AC | 3 ms | 512 KB |
16.txt | AC | 2 ms | 384 KB |
17.txt | AC | 2 ms | 384 KB |
18.txt | AC | 2 ms | 384 KB |
19.txt | AC | 2 ms | 384 KB |
20.txt | AC | 2 ms | 384 KB |
21.txt | AC | 2 ms | 384 KB |
22.txt | AC | 3 ms | 384 KB |
23.txt | AC | 9 ms | 384 KB |
24.txt | AC | 9 ms | 384 KB |
25.txt | AC | 6 ms | 384 KB |
26.txt | AC | 3 ms | 512 KB |
27.txt | AC | 3 ms | 512 KB |
28.txt | AC | 3 ms | 512 KB |
29.txt | AC | 3 ms | 512 KB |
30.txt | AC | 3 ms | 512 KB |
31.txt | AC | 3 ms | 512 KB |
32.txt | AC | 3 ms | 512 KB |
33.txt | AC | 2 ms | 384 KB |
34.txt | AC | 2 ms | 384 KB |
35.txt | AC | 2 ms | 384 KB |
36.txt | AC | 2 ms | 384 KB |
37.txt | AC | 2 ms | 384 KB |
38.txt | AC | 2 ms | 384 KB |
39.txt | AC | 2 ms | 384 KB |
40.txt | AC | 2 ms | 384 KB |
subtask0_0.txt | AC | 1 ms | 256 KB |
subtask0_1.txt | AC | 1 ms | 256 KB |
subtask0_2.txt | AC | 1 ms | 256 KB |
subtask0_3.txt | AC | 1 ms | 256 KB |