이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "friend.h"
#pragma GCC optimize("O3")
using namespace std;
int st1(int n, int confidence[], int host[], int protocol[]){
vector<int> g(n);
for(int i = 1; i < n; i++){
if(protocol[i]){
g[i] |= g[host[i]];
for(int j = 0; j < i; j++) if(g[host[i]]&(1<<j)) g[j] |= (1<<i);
}
if(protocol[i] != 1){
g[i] |= (1<<host[i]);
g[host[i]] |= (1<<i);
}
}
int t, s = 0;
for(int i = 0; i < (1<<n); i++){
t = 0;
for(int j = 0; j < n; j++) if(i&(1<<j)) t += confidence[j];
for(int j = 0; j < n; j++) if((i&(1<<j)) && (i&(g[j]))) t = 0;
s = max(s, t);
}
return s;
}
int st2(int n, int confidence[]){
int s = 0;
for(int i = 0; i < n; i++) s += confidence[i];
return s;
}
int st3(int n, int confidence[]){
int mx = 0;
for(int i = 0; i < n; i++) mx = max(mx, confidence[i]);
return mx;
}
void dfs(int u, vector< vector<int> >& g, vector<int>& dp, vector<int>& a){
for(int v : g[u]){
dfs(v, g, dp, a);
a[u] += dp[v];
dp[u] += a[v];
}
dp[u] = max(dp[u], a[u]);
}
int st4(int n, int confidence[], int host[]){
vector< vector<int> > g(n);
vector<int> dp(n), a(n);
for(int i = 1; i < n; i++) g[host[i]].push_back(i);
for(int i = 0; i < n; i++) dp[i] = confidence[i];
dfs(0, g, dp, a);
return dp[0];
}
int st5(int n, int host[], int protocol[]){
vector< vector<int> > g(n);
for(int i = 1; i < n; i++){
if(protocol[i]){
for(int v : g[host[i]]) g[i].push_back(v), g[v].push_back(i);
}
else{
g[i].push_back(host[i]), g[host[i]].push_back(i);
}
}
int s = 0;
vector<int> b(n), no(n);
for(int i = 0; i < n; i++) b[i] = g[i].size();
priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > q;
for(int i = 0; i < n; i++) q.push({b[i], i});
while(!q.empty()){
int u, t;
tie(t, u) = q.top();
q.pop();
if(b[u] != t || no[u]) continue;
s++;
for(int v : g[u]) if(!no[v]){
no[v] = 1;
for(int w : g[v]) if(w != u){
b[w]--;
q.push({b[w], w});
}
}
}
return s;
}
int findSample(int n, int confidence[], int host[], int protocol[]){
if(n <= 10) return st1(n, confidence, host, protocol);
for(int i = 2; i < n; i++) if(protocol[i] != protocol[i-1])
return st5(n, host, protocol);
if(protocol[1] == 1) return st2(n, confidence);
if(protocol[1]) return st3(n, confidence);
return st4(n, confidence, host);
}
# | 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... |