# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
404195 | bigg | 친구 (IOI14_friend) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "friend.h"
#include<bits/stdc++.h>
using namespace std;
// Find out best sample
const int MAXN = 1e5 + 10;
vector<int> grafo[MAXN];
int dp[MAXN][5], v[MAXN], comp[MAXN], maxc[MAXN];
int mat[15][15];
void dfsc(int x, int p, int c){
comp[x] = c;
for(int v : grafo[x]){
if(v == p) continue;
dfsc(v, x, c);
}
}
void dfs(int x, int p){
dp[x][0] = 0, dp[x][1] = v[x];
for(int v : grafo[x]){
if(v == p) continue;
dfs(v, x);
dp[x][0] += max(dp[v][0], dp[v][1]);
dp[x][1] += dp[v][0];
}
}
int confidence[MAXN], host[MAXN], protocol[MAXN], n;
int solve1(){
int ans = 0;
for(int i = 1; i < n; i++){
if(protocol[i] == 0){
grafo[i].push_back(host[i]);
grafo[host[i]].push_back(i);
mat[i][host[i]] = mat[host[i]][i] = 1;
}
if(protocol[i] >= 1){
for(int v : grafo[host[i]]){
grafo[i].push_back(v);
grafo[v].push_back(i);
mat[i][v] = mat[v][i] = 1;
}
if(protocol[i] == 2){
grafo[i].push_back(host[i]);
grafo[host[i]].push_back(i);
mat[i][host[i]] = mat[host[i]][i] = 1;
}
}
}
for(int i = 0; i < (1<<n); i++){
int aq = 0;
bool ok = 0;
for(int v1 = 0; v1 < n; v1++){
for(int v2 = v1 + 1; v2 < n; v2++){
if((1<<v1) & i && (1<<v2) & i && mat[v1][v2]) ok = 1;
}
}
if(ok) continue;
for(int j = 0; j < n; j++){
if((1<<j) & i) aq += confidence[j];
}
ans = max(ans, aq);
}
return ans;
}
int solve2(){
int ans = 0;
for(int i = 0; i < n; i++) ans += confidence[i];
return ans;
}
int solve3(){
for(int i = 0; i < n; i++) ans = max(ans, confidence[i]);
return ans;
}
int solve4(){
for(int i = 0; i < n; i++) v[i] = confidence[i];
for(int i = 1; i < n; i++){
grafo[host[i]].push_back(i);
grafo[i].push_back(host[i]);
}
int c = 0;
for(int i = 0; i < n; i++){
if(!comp[i]){
dfsc(i,i, ++c);
}
}
for(int i = 0; i < n; i++){
dfs(i, i);
maxc[comp[i]] = max(maxc[comp[i]], dp[i][1]);
}
for(int i = 1; i <= c; i++) ans += maxc[i];
return ans;
}
int marcb[MAXN], match[MAXN]
vector<int> qual[4];
bool dfsm(int x){
for(int v : grafo[x]){
if(marcb[v]) continue;
marcb[v] = 1;
if(match[v] == -1 || dfsm(match[v])){
match[v] = x;
match[x] = v;
return true;
}
}
return false;
}
int solve5(){
qual[0].push_back(0);
for(int i = 1; i < n; i++){
if(protocol[i] == 0){
grafo[host[i]].push_back(i);
grafo[i].push_back(host[i]);
comp[i] = 1-comp[host[i]];
qual[comp[i]].push_back(i);
}else{
for(int v : grafo[host[i]]){
grafo[i].push_back(v);
grafo[v].push_back(i);
}
comp[i] = comp[host[i]];
qual[comp[i]].push_back(i);
}
}
for(int i = 0; i < n; i++) match[i] = -1;
int ans = 0;
bool flag= 1;
while(flag){
flag = 0;
for(int i = 0; i < n; i++) marcb[i] = 0;
for(int x : qual[0]){
if(match[x] != -1) continue;
if(dfsm(x)){
flag = 1;
ans++;
}
}
}
return n - ans;
}
int findSample(int N,int c[],int h[],int p[]){
n = N;
for(int i = 0; i < n; i++) confidence[i] = c[i];
for(int i = 1; i < n; i++) host[i] = h[i], protocol[i] = p[i];
if(n <= 10){
return solve1();
}
int ok = 1;
for(int i = 1; i < n; i++) if(protocol[i] != 1) ok = 0;
if(ok){
return solve2();
}
ok = 1;
for(int i = 1; i < n; i++) if(protocol[i] != 2) ok = 0;
if(ok){
return solve3();
}
ok = 1;
for(int i = 1; i < n; i++) if(protocol[i] != 0) ok = 0;
if(ok){
return solve4();
}
return solve5();
}