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 <bits/stdc++.h>
#include "friend.h"
using namespace std;
typedef long long ll;
const int MAXN = 1e3 + 10;
int dp[MAXN][2];
bool visited[MAXN];
int con[MAXN][MAXN];
void build_graph(int n, int p[], int type[])
{
for(int i = 1; i < n; i++)
{
if(type[i] == 0)
{
con[i][p[i]]++;
con[p[i]][i]++;
}
else if(type[i] == 1)
{
for(int v = 0; v < n; v++)
{
if(p[i] == v || !con[p[i]][v])
continue;
con[v][i]++;
con[i][v]++;
}
}
else if(type[i] == 2)
{
for(int v = 0; v < n; v++)
{
if(p[i] == v || !con[p[i]][v])
continue;
con[v][i]++;
con[i][v]++;
}
con[i][p[i]]++;
con[p[i]][i]++;
}
}
}
int bruteforce(int n, int c[])
{
int ans = 0;
for(int mask = 1; mask < (1 << n); mask++)
{
bool lampa = false;
for(int i = 0; i < n; i++)
{
if(mask & (1 << i))
{
for(int v = 0; v < n; v++)
{
if(i == v)
continue;
if(con[i][v] && mask & (1 << v))
lampa = true;
}
}
}
if(lampa)
continue;
int sum = 0;
for(int i = 0; i < n; i++)
{
if(mask & (1 << i))
sum += c[i];
}
ans = max(ans, sum);
}
return ans;
}
void dfs(int u, int par, int c[], int n)
{
visited[u] = true;
dp[u][1] = c[u];
for(int v = 0; v < n; v++)
{
if(v == u || v == par || !con[u][v])
continue;
dfs(v, u, c, n);
dp[u][0] += max(dp[v][0], dp[v][1]);
dp[u][1] += dp[v][0];
}
}
int solve_dp(int n, int c[])
{
int ans = 0;
for(int i = 0; i < n; i++)
{
if(!visited[i])
{
dfs(i, i, c, n);
ans += max(dp[i][0], dp[i][1]);
}
}
return ans;
}
int weareyourfriend(int n, int c[])
{
int maxel = 0;
for(int i = 0; i < n; i++)
{
maxel = max(maxel, c[i]);
}
return maxel;
}
int findSample(int n, int c[], int p[], int type[])
{
bool lampa = true;
for(int i = 1; i < n; i++)
{
if(type[i] != 2)
lampa = false;
}
build_graph(n, p, type);
if(n <= 10)
return bruteforce(n, c);
if(!lampa)
return solve_dp(n, c);
return weareyourfriend(n, c);
}
# | 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... |