# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
98679 | polyfish | Uzastopni (COCI15_uzastopni) | C++14 | 778 ms | 19348 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.
//Pantyhose(black) + glasses = infinity
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"
const int MAX_N = 10002;
const int MAX_V = 102;
int n, maxV, T[MAX_N], sz[MAX_N];
vector<int> g[MAX_N];
bitset<MAX_V> F[MAX_N][MAX_V], f[MAX_N][MAX_V];
void readInput() {
cin >> n;
for (int i=1; i<=n; ++i)
cin >> T[i];
g[1].push_back(0);
for (int i=1; i<n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
}
int fixRoot(int u, int par) {
sz[u] = 1;
for (int i=0; i<g[u].size(); ++i) {
int v = g[u][i];
if (v!=par)
sz[u] += fixRoot(v, u);
else
swap(g[u][0], g[u][i]);
}
return sz[u];
}
void dp(int u) {
for (int i=1; i<g[u].size(); ++i)
dp(g[u][i]);
vector<int> p(1);
p.push_back(u);
for (int i=1; i<g[u].size(); ++i)
p.push_back(g[u][i]);
sort(p.begin(), p.end(), [](int x, int y) {
return T[x] < T[y];
});
int cur = 0;
for (int i=1; i<=maxV; ++i)
f[0][i][0] = true;
F[u][T[u]][1] = true;
for (int i=0; i+1<p.size(); ++i) {
int v = p[i+1];
for (int l=1; l<=T[u]; ++l) {
for (int x=0; x<=min(cur, maxV-l+1); ++x) {
for (int y=0; y<=(v!=u ? sz[v] : 1) && l+x+y-1<=maxV; ++y)
f[i+1][l][x+y] = false;
}
}
for (int l=1; l<=T[u]; ++l) {
for (int x=0; x<=min(cur, maxV-l+1); ++x) {
for (int y=0; y<=(v!=u ? sz[v] : 1) && l+x+y-1<=maxV; ++y)
f[i+1][l][x+y] = f[i+1][l][x+y] || (f[i][l][x] & F[v][l+x][y]);
}
}
cur += (v!=u ? sz[v] : 1);
}
for (int l=1; l<=maxV; ++l) {
for (int x=0; x<=min(cur, maxV-l+1); ++x) {
F[u][l][x] = F[u][l][x] || f[p.size()-1][l][x];
}
F[u][l][0] = true;
}
}
int main() {
#ifdef GLASSES_GIRL
freopen(FILE_NAME".in", "r", stdin);
//freopen(FILE_NAME".out", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0);
readInput();
fixRoot(1, 0);
maxV = *max_element(T+1, T+n+1);
//dp(3);
dp(1);
int res = 0;
for (int i=1; i<=maxV; ++i) {
for (int j=i; j<=maxV; ++j)
res += F[1][i][j-i+1];
}
cout << res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |