This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 conf[MAXN], ht[MAXN], pt[MAXN], n;
int solve1(){
int ans = 0;
for(int i = 1; i < n; i++){
if(pt[i] == 0){
grafo[i].push_back(ht[i]);
grafo[ht[i]].push_back(i);
mat[i][ht[i]] = mat[ht[i]][i] = 1;
}
if(pt[i] >= 1){
for(int v : grafo[ht[i]]){
grafo[i].push_back(v);
grafo[v].push_back(i);
mat[i][v] = mat[v][i] = 1;
}
if(pt[i] == 2){
grafo[i].push_back(ht[i]);
grafo[ht[i]].push_back(i);
mat[i][ht[i]] = mat[ht[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 += conf[j];
}
ans = max(ans, aq);
}
return ans;
}
int solve2(){
int ans = 0;
for(int i = 0; i < n; i++) ans += conf[i];
return ans;
}
int solve3(){
int ans = 0;
for(int i = 0; i < n; i++) ans = max(ans, conf[i]);
return ans;
}
int solve4(){
int ans = 0;
for(int i = 0; i < n; i++) v[i] = conf[i];
for(int i = 1; i < n; i++){
grafo[ht[i]].push_back(i);
grafo[i].push_back(ht[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(pt[i] == 0){
grafo[ht[i]].push_back(i);
grafo[i].push_back(ht[i]);
comp[i] = 1-comp[ht[i]];
qual[comp[i]].push_back(i);
}else{
for(int v : grafo[ht[i]]){
grafo[i].push_back(v);
grafo[v].push_back(i);
}
comp[i] = comp[ht[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++) conf[i] = c[i];
for(int i = 1; i < n; i++) ht[i] = h[i], pt[i] = p[i];
if(n <= 10){
return solve1();
}
int ok = 1;
for(int i = 1; i < n; i++) if(pt[i] != 1) ok = 0;
if(ok && n <= 1000){
return solve2();
}
ok = 1;
for(int i = 1; i < n; i++) if(pt[i] != 2) ok = 0;
if(ok && n <= 1000){
return solve3();
}
ok = 1;
for(int i = 1; i < n; i++) if(pt[i] != 0) ok = 0;
if(ok && n <= 1000){
return solve4();
}
for(int i = 1; i < n; i++) if(pt[i] == 2) ok = 0;
for(int i = 0; i < n; i++) if(conf[i] != 1) ok = 0;
if(ok && n <= 1000){
return solve5();
}
return 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... |