이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int maxn = 1e5 + 5;
int arr[maxn], memo[maxn][2];
vector<int> graph[maxn];
void pdfs(int u) {
for (auto& v : graph[u]) {
graph[v].erase(find(begin(graph[v]), end(graph[v]), u));
pdfs(v);
}
}
int dp(int u, bool cur) {
int& ans = memo[u][cur];
if (ans != -1) {
return ans;
}
ans = cur * arr[u];
for (auto& v : graph[u]) {
if (cur) {
ans += dp(v, false);
} else {
ans += max(dp(v, false), dp(v, true));
}
}
return ans;
}
int findSample(int n, int _arr[], int host[], int protocol[]) {
copy(_arr, _arr + n, arr);
for (int i = 1; i < n; i++) {
if (protocol[i] == 0 || protocol[i] == 2) {
graph[i].push_back(host[i]);
}
if (protocol[i] == 1 || protocol[i] == 2) {
graph[i].insert(graph[i].end(), begin(graph[host[i]]),
end(graph[host[i]]));
}
for (auto& a : graph[i]) {
graph[a].push_back(i);
}
}
if (n <= 10) {
int ans = 0;
for (int i = 0; i < (1 << n); i++) {
int cans = 0;
for (int j = 0; j < n; j++) {
if ((i >> j) & 1) {
cans += arr[j];
for (auto& a : graph[j]) {
if ((i >> a) & 1) {
goto loop;
}
}
}
}
ans = max(ans, cans);
loop:;
}
return ans;
}
if (protocol[1] == 1) {
for (auto& a : graph) {
assert(!sz(a));
}
return accumulate(arr, arr + n, 0);
} else if (protocol[1] == 2) {
return *max_element(arr, arr + n);
}
pdfs(0);
return max(dp(0, false), dp(0, true));
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |