이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "friend.h"
using namespace std;
const int MN = 100001;
set<int> adj[MN];
void construct(int n, int host[], int protocol[]) {
for (int i=1; i<n; ++i) {
int u = host[i];
if (protocol[i] == 0 || protocol[i] == 2) {
adj[u].insert(i);
adj[i].insert(u);
}
if (protocol[i] == 1 || protocol[i] == 2) {
for (auto it=adj[u].begin(); it!=adj[u].end(); ++it) {
adj[*it].insert(i);
adj[i].insert(*it);
}
}
}
}
int solve1(int n, int confidence[], int host[], int protocol[]) {
construct(n, host, protocol);
int ans = 0;
for (int mask=0; mask<(1 << n); ++mask) {
bool ok = true;
for (int u=0; u<n; ++u) {
if (((mask >> u) & 1) == 0) continue;
for (int v=u+1; v<n; ++v) {
if (((mask >> v) & 1) == 0) continue;
if (adj[u].count(v)) {
ok = false;
break;
}
}
}
if (ok) {
int sum = 0;
for (int i=0; i<n; ++i) {
if ((mask >> i) & 1) {
sum += confidence[i];
}
}
ans = max(ans, sum);
//for (int i=0; i<n; ++i) { if ((mask >> i) & 1) { cerr << i << ' '; } }cerr << endl;
}
}
return ans;
}
int solve2(int n, int confidence[], int host[], int protocol[]) {
int sum = 0;
for (int i=0; i<n; ++i) {
sum += confidence[i];
}
return sum;
}
int solve3(int n, int confidence[], int host[], int protocol[]) {
int ans = 0;
for (int i=0; i<n; ++i) {
ans = max(ans, confidence[i]);
}
return ans;
}
bool visited[MN];
int dp[MN][2];
void dfs(int node, int par, int conf[]) {
visited[node] = true;
dp[node][0] = 0;
dp[node][1] = conf[node];
for (auto it=adj[node].begin(); it!=adj[node].end(); ++it) {
if (*it == par) continue;
dfs(*it, node, conf);
dp[node][0] += max(dp[*it][0], dp[*it][1]);
dp[node][1] += dp[*it][0];
}
}
int solve4(int n, int confidence[], int host[], int protocol[]) {
construct(n, host, protocol);
fill(visited, visited+n, false);
int ans = 0;
for (int i=0; i<n; ++i) {
if (!visited[i]) {
dfs(i, -1, confidence);
ans += max(dp[i][0], dp[i][1]);
}
}
return ans;
}
int mark[MN];
int solve5(int n, int confidence[], int host[], int protocol[]) {
fill(mark, mark+n, -1);
mark[0] = 0;
for (int i=1; i<n; ++i) {
int u = host[i];
if (protocol[i] == 0) {
mark[i] = !mark[u];
} else if (protocol[i] == 1) {
mark[i] = mark[u];
} else assert(false);
}
int ct[2] = {0,0};
for (int i=0; i<n; ++i) {
++ct[mark[i]];
}
return max(ct[0], ct[1]);
}
int findSample(int n, int confidence[], int host[], int protocol[]) {
int type[3] = {0, 0, 0};
for (int i=1; i<n; ++i) {
++type[protocol[i]];
}
if (n <= 10) {
return solve1(n, confidence, host, protocol);
} else if (type[1] == n-1) {
return solve2(n, confidence, host, protocol);
} else if (type[2] == n-1) {
return solve3(n, confidence, host, protocol);
} else if (type[3] == n-1) {
return solve4(n, confidence, host, protocol);
} else if (type[1] == 0) {
return solve5(n, confidence, host, protocol);
} else {
return 8;
}
}
컴파일 시 표준 에러 (stderr) 메시지
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:133:22: warning: array subscript 3 is above array bounds of 'int [3]' [-Warray-bounds]
133 | } else if (type[3] == n-1) {
| ~~~~~~^
friend.cpp:123:9: note: while referencing 'type'
123 | int type[3] = {0, 0, 0};
| ^~~~
# | 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... |