# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
520915 | peti1234 | Friend (IOI14_friend) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int findSample(int n, int con[], int h[], int p[]) {
// 1
if (n<=10) {
bool el[10][10];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
el[i][j]=0;
}
}
for (int i=1; i<=n-1; i++) {
if (p[i]==1 || p[i]==2) {
for (int j=0; j<n; j++) {
if (el[j][h[i]]) {
//cout << "el " << i << " " << j << "\n";
el[i][j]=1, el[j][i]=1;
}
}
}
if (p[i]==0 || p[i]==2) {
//cout << "el " << i << " " << h[i] << "\n";
el[i][h[i]]=1, el[h[i]][i]=1;
}
}
int valasz=0;
for (int mask=0; mask<(1<<n); mask++) {
int ert=0;
for (int i=0; i<n; i++) {
if (mask & (1<<i)) {
ert+=con[i];
}
}
bool jo=1;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if ((mask & (1<<i)) && (mask & (1<<j)) && el[i][j]) {
jo=0;
}
}
}
if (jo) {
valasz=max(valasz, ert);
}
}
return valasz;
}
// 2
if (p[1]==2) {
int valasz=0;
for (int i=0; i<n; i++) {
valasz=max(valasz, con[i]);
}
return valasz;
}
// 3
if (p[1]==3) {
int valasz=0;
for (int i=0; i<n; i++) {
valasz+=con[i];
}
return valasz;
}
}
int n, con[100], h[100], p[100];
int main() {
cin >> n;
for (int i=0; i<n; i++) {
cin >> con[i];
}
for (int i=1; i<n; i++) {
cin >> h[i];
}
for (int i=1; i<n; i++) {
cin >> p[i];
}
cout << findSample(n, con, h, p);
}
/*
6
13 3 6 20 10 15
0 0 1 2 0
1 2 3 2 1
*/