이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "friend.h"
#include <bits/stdc++.h>
using namespace std;
const int IAmYourFriend = 0;
const int MyFriendsAreYourFriends = 1;
const int WeAreYourFriends = 2;
int solve1(int n, vector<int> values, vector<int> host, vector<int> type) {
vector<vector<int> > graph(1 + n);
for(int i = 1; i <= n; i++) {
if(type[i] == IAmYourFriend) {
graph[i].push_back(host[i]);
graph[host[i]].push_back(i);
}
if(type[i] == MyFriendsAreYourFriends) {
for(int nei : graph[host[i]]) {
graph[i].push_back(nei);
graph[nei].push_back(i);
}
}
if(type[i] == WeAreYourFriends) {
for(int nei : graph[host[i]]) {
graph[i].push_back(nei);
graph[nei].push_back(i);
}
graph[i].push_back(host[i]);
graph[host[i]].push_back(i);
}
}
int result = 0;
for(int mask = 0; mask < (1 << (1 + n)); mask++) {
int cur_result = 0;
vector<bool> selected(1 + n, false);
for(int i = 0; i <= n; i++) {
if(mask & (1 << i)) {
selected[i] = true;
cur_result += values[i];
}
}
bool ok = true;
for(int i = 0; i <= n; i++) {
if(selected[i]) {
for(int nei : graph[i]) {
if(selected[nei]) {
ok = false;
}
}
}
}
if(ok) {
result = max(result, cur_result);
}
}
return result;
}
int solve2(int n, vector<int> values, vector<int> host, vector<int> type) {
int result = 0;
for(int cur : values) {
result += cur;
}
return result;
}
int solve3(int n, vector<int> values, vector<int> host, vector<int> type) {
int result = 0;
for(int cur : values) {
result = max(result, cur);
}
return result;
}
vector<vector<int> > graph;
vector<int> parent;
vector<int> dp_chosen;
vector<int> dp_not_chosen;
void dfs(int cur, vector<int> &values) {
for(int nei : graph[cur]) {
dfs(nei, values);
dp_chosen[cur] += dp_not_chosen[nei];
dp_not_chosen[cur] += max(dp_chosen[nei], dp_not_chosen[nei]);
}
dp_chosen[cur] += values[cur];
}
int solve4(int n, vector<int> values, vector<int> host, vector<int> type) {
graph.resize(1 + n);
parent.assign(1 + n, 0);
dp_chosen.assign(1 + n, 0);
dp_not_chosen.assign(1 + n, 0);
for(int i = 1; i <= n; i++) {
graph[host[i]].push_back(i);
parent[i] = host[i];
}
dfs(0, values);
return max(dp_chosen[0], dp_not_chosen[0]);
}
int findSample(int n,int confidence[],int host[],int protocol[]){
int _n = n - 1;
vector<int> _values(1 + _n, 0);
vector<int> _host(1 + _n, 0);
vector<int> _type(1 + _n, 0);
_values[0] = confidence[0];
vector<int> cnt(3, 0);
for(int i = 1; i <= _n; i++) {
_values[i] = confidence[i];
_host[i] = host[i];
_type[i] = protocol[i];
cnt[_type[i]]++;
}
if(n <= 10) {
return solve1(_n, _values, _host, _type);
} else if(cnt[MyFriendsAreYourFriends] == _n) {
return solve2(_n, _values, _host, _type);
} else if(cnt[WeAreYourFriends] == _n) {
return solve3(_n, _values, _host, _type);
} else if(cnt[IAmYourFriend] == _n) {
return solve4(_n, _values, _host, _type);
}
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... |