이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "friend.h"
using namespace std;
int Sub1(int n, int confidence[], int host[], int protocol[]) {
vector<vector<bool>> g(n, vector<bool>(n, false));
auto add_edge = [&g] (int u, int v) {
g[u][v] = g[v][u] = true;
};
for (int i = 1; i < n; i++) {
if (protocol[i] == 0 || protocol[i] == 2) {
add_edge(i, host[i]);
}
if (protocol[i] == 1 || protocol[i] == 2) {
for (int j = 0; j < n; j++) if (g[host[i]][j]) {
add_edge(j, i);
}
}
}
int ans = -1;
for (int mask = 0; mask < (1 << n); mask++) {
bool flag = true;
int now = 0;
for (int i = 0; i < n; i++) if (mask & (1 << i)) {
now += confidence[i];
for (int j = i + 1; j < n; j++) if (mask & (1 << j)) {
flag &= !g[i][j];
}
}
if (!flag)
continue;
ans = max(ans, now);
}
return ans;
}
int Sub2(int n, int confidence[], int host[], int protocol[]) {
int ans = confidence[0];
for (int i = 1; i < n; i++) {
if (protocol[i] != 1)
return -1;
ans += confidence[i];
}
return ans;
}
int Sub3(int n, int confidence[], int host[], int protocol[]) {
int ans = confidence[0];
for (int i = 1; i < n; i++) {
if (protocol[i] != 2)
return -1;
ans = max(ans, confidence[i]);
}
return ans;
}
int Sub4(int n, int confidence[], int host[], int protocol[]) {
for (int i = 1; i < n; i++) {
if (protocol[i] != 0)
return -1;
}
vector<vector<int>> adj(n), dp(n, vector<int>(2));
for (int i = 1; i < n; i++)
adj[host[i]].push_back(i);
for (int i = n; i--;) {
dp[i][1] = confidence[i];
for (int j : adj[i]) {
dp[i][0] += dp[j][1];
dp[i][1] += dp[j][0];
}
dp[i][1] = max(dp[i][1], dp[i][0]);
}
return dp[0][1];
}
int findSample(int n, int confidence[], int host[], int protocol[]){
int ans = -1;
if (n <= 10)
return Sub1(n, confidence, host, protocol);
if ((ans = Sub2(n, confidence, host, protocol)) != -1)
return ans;
if ((ans = Sub3(n, confidence, host, protocol)) != -1)
return ans;
if ((ans = Sub4(n, confidence, host, protocol)) != -1)
return ans;
return -1;
}
# | 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... |