#include "friend.h"
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1005;
int prot[3], on[MAXN], DP[MAXN][2], visDP[MAXN][2];
int ans, c[MAXN], vis[MAXN], already[MAXN], used[MAXN];
vi Graph[MAXN];
int solve(int node, int f, int p = 0)
{
if(visDP[node][f])
return DP[node][f];
int res1 = 0, res2 = 0;
for(auto i : Graph[node])
{
if(i == p)
continue;
res1 += solve(i, 1, node);
res2 += solve(i, 0, node);
}
visDP[node][f] = 1;
if(f)
return DP[node][f] = max(res1, res2 + c[node]);
else
return DP[node][f] = res1;
}
// Find out best sample
int findSample(int N, int _conf[], int host[], int protocol[])
{
if(N > 1000)
return 0;
for(int i = 0; i < N; i++)
{
c[i] = _conf[i];
if(i >= 1)
prot[protocol[i]]++;
}
//S1
if(N <= 10)
{
for(int i = 1; i < N; i++)
{
if(protocol[i] == 0)
{
Graph[host[i]].pb(i);
Graph[i].pb(host[i]);
}
else if(protocol[i] == 1)
{
for(auto v : Graph[host[i]])
{
Graph[v].pb(i);
Graph[i].pb(v);
}
}
else if(protocol[i] == 2)
{
for(auto v : Graph[host[i]])
{
Graph[v].pb(i);
Graph[i].pb(v);
}
Graph[host[i]].pb(i);
Graph[i].pb(host[i]);
}
}
int aux;
for(int i = 0; i < (1 << N); i++)
{
aux = 0;
for(int j = 0; j < N; j++)
{
on[j] = 0;
if(i & (1 << j))
{
on[j] = 1;
aux += c[j];
}
}
bool ind = 1;
for(int node = 0; node < N; node++)
if(on[node])
for(auto j : Graph[node])
if(on[j])
ind = 0;
if(!ind)
continue;
ans = max(ans, aux);
}
}
//S2
else if(prot[1] == N - 1)
{
for(int i = 0; i < N; i++)
ans += c[i];
}
//S3
else if(prot[2] == N - 1)
{
for(int i = 0; i < N; i++)
ans = max(ans, c[i]);
}
//S4
else if(prot[0] == N - 1)
{
for(int i = 1; i < N; i++)
{
if(protocol[i] == 0)
{
Graph[host[i]].pb(i);
Graph[i].pb(host[i]);
}
}
ans = solve(0, 1);
}
//S5
else if(prot[0] + prot[1] == N - 1)
{
for(int i = 1; i < N; i++)
{
if(protocol[i] == 0)
{
Graph[host[i]].pb(i);
Graph[i].pb(host[i]);
}
else if(protocol[i] == 1)
{
for(auto v : Graph[host[i]])
{
Graph[v].pb(i);
Graph[i].pb(v);
}
}
}
for(int i = 0; i < N; i++)
{
if(vis[i])
continue;
queue<int> Q;
Q.push(i);
vi ord;
vis[i] = 1;
int aux;
while(!Q.empty())
{
aux = Q.front();
Q.pop();
ord.pb(aux);
for(auto v : Graph[aux])
if(!vis[v])
{
Q.push(v);
vis[v] = 1;
}
}
reverse(all(ord));
for(auto node : ord)
{
int res1 = 0, res2 = 0;
for(auto j : Graph[node])
if(already[j] && !used[j])
{
used[j] = 1;
res1 += DP[j][1];
res2 += DP[j][0];
}
DP[node][1] = max(res1, res2 + c[node]);
DP[node][0] = res1;
already[node] = 1;
}
ans += DP[i][1];
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
360 KB |
Output is correct |
9 |
Correct |
5 ms |
396 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
544 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
404 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Incorrect |
40 ms |
1664 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |