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;
#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
typedef long long ll;
typedef pair<double, double> pd;
typedef pair<int, int> pi; typedef pair<ll, int> pli;
typedef pair<ll, ll> pll; typedef pair<ll, pi> plp;
typedef tuple<int, int, int> ti; typedef tuple<ll, int, int> tli;
const int INF = 0x3f2f1f0f;
const ll LINF = 1ll * INF * INF * 2;
const int MAX_N = 1e5 + 100;
int N, Nr[MAX_N], Cnt[3];
vector<int> Ed[MAX_N];
void makeG(int ho[], int pr[]) {
for(int i=1; i<N; i++) {
int h = ho[i];
if(pr[i] >= 1) for(int x : Ed[h]) Ed[x].push_back(i), Ed[i].push_back(x);
if(pr[i] % 2 == 0) Ed[h].push_back(i), Ed[i].push_back(h);
}
}
int Dy[MAX_N][2]; bool Vis[MAX_N];
void getDy(int v, int p) {
Vis[v] = true;
Dy[v][1] = Nr[v];
for(int w : Ed[v]) if(w != p) {
getDy(w, v);
Dy[v][0] += max(Dy[w][0], Dy[w][1]);
Dy[v][1] += Dy[w][0];
}
}
int findSample(int n_, int nr_[], int ho_[], int pr_[]) {
N = n_; for(int i=0; i<N; i++) Nr[i] = nr_[i];
if(N <= 10) {
int d[15][15] = {0, };
makeG(ho_, pr_);
for(int v=0; v<N; v++) for(int w : Ed[v]) d[v][w] = 1;
int ans = 0;
for(int s=0; s<(1<<N); s++) {
vector<int> list;
for(int i=0; i<N; i++) if(s & (1<<i)) list.push_back(i);
bool isCan = true;
for(int x : list) for(int y : list) if(d[x][y] == 1) {isCan = false; break;}
if(isCan) {
int sum = 0;
for(int x : list) sum += Nr[x];
ans = max(ans, sum);
}
}
return ans;
}
for(int i=1; i<N; i++) Cnt[pr_[i]]++;
if(Cnt[2] == N-1) {
int maxV = -1;
for(int i=0; i<N; i++) maxV = max(maxV, Nr[i]);
return maxV;
}else if(Cnt[1] == N-1) {
int sum = 0;
for(int i=0; i<N; i++) sum += Nr[i];
return sum;
}else if(Cnt[0] == N-1) {
makeG(ho_, pr_);
int sum = 0;
for(int i=0; i<N; i++) if(Vis[i] == false) {
getDy(i, -1);
sum += max(Dy[i][0], Dy[i][1]);
}
return sum;
}
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... |