#include "catdog.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int N = 111111;
int n;
vi adj[N];
int alien[N];
int dp[N][3];
const int INF = int(1e9);
void dfs(int u, int p=-1)
{
dp[u][0]=dp[u][1]=dp[u][2]=INF;
if(alien[u]==0) dp[u][0]=0;
if(alien[u]==0||alien[u]==1) dp[u][1]=0;
if(alien[u]==0||alien[u]==2) dp[u][2]=0;
for(int v:adj[u])
{
if(v==p) continue;
dfs(v,u);
if(alien[u]==0)
{
dp[u][0]+=min(dp[v][0],min(dp[v][1]+1,dp[v][2]+1));
}
if(alien[u]==0||alien[u]==1)
{
dp[u][1]+=min(dp[v][0],min(dp[v][1],dp[v][2]+1));
}
if(alien[u]==0||alien[u]==2)
{
dp[u][2]+=min(dp[v][0],min(dp[v][1]+1,dp[v][2]));
}
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B)
{
n=N;
for(int i=0;i<N-1;i++)
{
adj[A[i]-1].pb(B[i]-1); adj[B[i]-1].pb(A[i]-1);
}
dfs(0);
memset(alien,0,sizeof(alien));
}
int cat(int v)
{
v--; alien[v]=1;
dfs(0);
return min(dp[0][0],min(dp[0][1],dp[0][2]));
}
int dog(int v)
{
v--; alien[v]=2;
dfs(0);
return min(dp[0][0],min(dp[0][1],dp[0][2]));
}
int neighbor(int v)
{
v--; alien[v]=0;
dfs(0);
return min(dp[0][0],min(dp[0][1],dp[0][2]));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
5 ms |
3448 KB |
Output is correct |
3 |
Correct |
5 ms |
3492 KB |
Output is correct |
4 |
Correct |
5 ms |
3560 KB |
Output is correct |
5 |
Correct |
5 ms |
3608 KB |
Output is correct |
6 |
Correct |
7 ms |
3644 KB |
Output is correct |
7 |
Correct |
6 ms |
3644 KB |
Output is correct |
8 |
Correct |
8 ms |
3644 KB |
Output is correct |
9 |
Correct |
9 ms |
3644 KB |
Output is correct |
10 |
Correct |
7 ms |
3644 KB |
Output is correct |
11 |
Correct |
4 ms |
3644 KB |
Output is correct |
12 |
Correct |
6 ms |
3644 KB |
Output is correct |
13 |
Correct |
5 ms |
3644 KB |
Output is correct |
14 |
Correct |
6 ms |
3644 KB |
Output is correct |
15 |
Correct |
6 ms |
3732 KB |
Output is correct |
16 |
Correct |
6 ms |
3732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
5 ms |
3448 KB |
Output is correct |
3 |
Correct |
5 ms |
3492 KB |
Output is correct |
4 |
Correct |
5 ms |
3560 KB |
Output is correct |
5 |
Correct |
5 ms |
3608 KB |
Output is correct |
6 |
Correct |
7 ms |
3644 KB |
Output is correct |
7 |
Correct |
6 ms |
3644 KB |
Output is correct |
8 |
Correct |
8 ms |
3644 KB |
Output is correct |
9 |
Correct |
9 ms |
3644 KB |
Output is correct |
10 |
Correct |
7 ms |
3644 KB |
Output is correct |
11 |
Correct |
4 ms |
3644 KB |
Output is correct |
12 |
Correct |
6 ms |
3644 KB |
Output is correct |
13 |
Correct |
5 ms |
3644 KB |
Output is correct |
14 |
Correct |
6 ms |
3644 KB |
Output is correct |
15 |
Correct |
6 ms |
3732 KB |
Output is correct |
16 |
Correct |
6 ms |
3732 KB |
Output is correct |
17 |
Correct |
23 ms |
3796 KB |
Output is correct |
18 |
Correct |
27 ms |
3808 KB |
Output is correct |
19 |
Correct |
15 ms |
3836 KB |
Output is correct |
20 |
Correct |
6 ms |
3836 KB |
Output is correct |
21 |
Correct |
7 ms |
3868 KB |
Output is correct |
22 |
Correct |
8 ms |
3892 KB |
Output is correct |
23 |
Correct |
32 ms |
4048 KB |
Output is correct |
24 |
Correct |
21 ms |
4048 KB |
Output is correct |
25 |
Correct |
13 ms |
4048 KB |
Output is correct |
26 |
Correct |
10 ms |
4048 KB |
Output is correct |
27 |
Correct |
8 ms |
4048 KB |
Output is correct |
28 |
Correct |
9 ms |
4048 KB |
Output is correct |
29 |
Correct |
27 ms |
4048 KB |
Output is correct |
30 |
Correct |
8 ms |
4048 KB |
Output is correct |
31 |
Correct |
9 ms |
4048 KB |
Output is correct |
32 |
Correct |
10 ms |
4048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3320 KB |
Output is correct |
2 |
Correct |
5 ms |
3448 KB |
Output is correct |
3 |
Correct |
5 ms |
3492 KB |
Output is correct |
4 |
Correct |
5 ms |
3560 KB |
Output is correct |
5 |
Correct |
5 ms |
3608 KB |
Output is correct |
6 |
Correct |
7 ms |
3644 KB |
Output is correct |
7 |
Correct |
6 ms |
3644 KB |
Output is correct |
8 |
Correct |
8 ms |
3644 KB |
Output is correct |
9 |
Correct |
9 ms |
3644 KB |
Output is correct |
10 |
Correct |
7 ms |
3644 KB |
Output is correct |
11 |
Correct |
4 ms |
3644 KB |
Output is correct |
12 |
Correct |
6 ms |
3644 KB |
Output is correct |
13 |
Correct |
5 ms |
3644 KB |
Output is correct |
14 |
Correct |
6 ms |
3644 KB |
Output is correct |
15 |
Correct |
6 ms |
3732 KB |
Output is correct |
16 |
Correct |
6 ms |
3732 KB |
Output is correct |
17 |
Correct |
23 ms |
3796 KB |
Output is correct |
18 |
Correct |
27 ms |
3808 KB |
Output is correct |
19 |
Correct |
15 ms |
3836 KB |
Output is correct |
20 |
Correct |
6 ms |
3836 KB |
Output is correct |
21 |
Correct |
7 ms |
3868 KB |
Output is correct |
22 |
Correct |
8 ms |
3892 KB |
Output is correct |
23 |
Correct |
32 ms |
4048 KB |
Output is correct |
24 |
Correct |
21 ms |
4048 KB |
Output is correct |
25 |
Correct |
13 ms |
4048 KB |
Output is correct |
26 |
Correct |
10 ms |
4048 KB |
Output is correct |
27 |
Correct |
8 ms |
4048 KB |
Output is correct |
28 |
Correct |
9 ms |
4048 KB |
Output is correct |
29 |
Correct |
27 ms |
4048 KB |
Output is correct |
30 |
Correct |
8 ms |
4048 KB |
Output is correct |
31 |
Correct |
9 ms |
4048 KB |
Output is correct |
32 |
Correct |
10 ms |
4048 KB |
Output is correct |
33 |
Execution timed out |
3051 ms |
8768 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |