#include "catdog.h"
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n;
vector<int> con[100005];
int dp[100005][3];
int tip[100005];
void dfs(int nod, int par)
{
dp[nod][0]=0;
dp[nod][1]=0;
dp[nod][2]=0;
for(auto adj:con[nod])
{
if(adj!=par)
{
dfs(adj,nod);
if(tip[nod]==0)
{
dp[nod][0] += min({dp[adj][0],dp[adj][1]+1,dp[adj][2]+1});
dp[nod][1] += min({dp[adj][0],dp[adj][1],dp[adj][2]+1});
dp[nod][2] += min({dp[adj][0],dp[adj][1]+1,dp[adj][2]});
}
else if(tip[nod]==1)
{
dp[nod][0] += min({dp[adj][0],dp[adj][1],dp[adj][2]+1});
dp[nod][1] += min({dp[adj][0],dp[adj][1],dp[adj][2]+1});
}
else
{
dp[nod][0] += min({dp[adj][0],dp[adj][1]+1,dp[adj][2]});
dp[nod][2] += min({dp[adj][0],dp[adj][1]+1,dp[adj][2]});
}
}
}
if(tip[nod]==0)
{
}
else if(tip[nod]==1)
{
dp[nod][0]++;
dp[nod][2]=INF;
}
else
{
dp[nod][0]++;
dp[nod][1]=INF;
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B)
{
n=N;
for(int i=0;i<n-1;i++)
{
con[A[i]].push_back(B[i]);
con[B[i]].push_back(A[i]);
}
}
int cat(int v)
{
tip[v]=1;
dfs(1,0);
return min({dp[1][0],dp[1][1],dp[1][2]});
}
int dog(int v)
{
tip[v]=2;
dfs(1,0);
return min({dp[1][0],dp[1][1],dp[1][2]});
}
int neighbor(int v)
{
tip[v]=0;
dfs(1,0);
return min({dp[1][0],dp[1][1],dp[1][2]});
}
/**
5
1 2
2 3
2 4
4 5
5
1 3
2 5
1 2
2 1
3 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
3 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3928 KB |
Output is correct |
9 |
Correct |
2 ms |
3928 KB |
Output is correct |
10 |
Correct |
2 ms |
3928 KB |
Output is correct |
11 |
Correct |
2 ms |
4176 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3980 KB |
Output is correct |
14 |
Correct |
2 ms |
3928 KB |
Output is correct |
15 |
Correct |
1 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
4168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
3 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3928 KB |
Output is correct |
9 |
Correct |
2 ms |
3928 KB |
Output is correct |
10 |
Correct |
2 ms |
3928 KB |
Output is correct |
11 |
Correct |
2 ms |
4176 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3980 KB |
Output is correct |
14 |
Correct |
2 ms |
3928 KB |
Output is correct |
15 |
Correct |
1 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
4168 KB |
Output is correct |
17 |
Correct |
8 ms |
4184 KB |
Output is correct |
18 |
Correct |
12 ms |
4188 KB |
Output is correct |
19 |
Correct |
7 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
4 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
13 ms |
4188 KB |
Output is correct |
24 |
Correct |
11 ms |
4188 KB |
Output is correct |
25 |
Correct |
6 ms |
4184 KB |
Output is correct |
26 |
Correct |
5 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4188 KB |
Output is correct |
28 |
Correct |
4 ms |
4188 KB |
Output is correct |
29 |
Correct |
16 ms |
4292 KB |
Output is correct |
30 |
Correct |
3 ms |
4188 KB |
Output is correct |
31 |
Correct |
2 ms |
4184 KB |
Output is correct |
32 |
Correct |
3 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
3 ms |
3932 KB |
Output is correct |
3 |
Correct |
2 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
3932 KB |
Output is correct |
8 |
Correct |
2 ms |
3928 KB |
Output is correct |
9 |
Correct |
2 ms |
3928 KB |
Output is correct |
10 |
Correct |
2 ms |
3928 KB |
Output is correct |
11 |
Correct |
2 ms |
4176 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3980 KB |
Output is correct |
14 |
Correct |
2 ms |
3928 KB |
Output is correct |
15 |
Correct |
1 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
4168 KB |
Output is correct |
17 |
Correct |
8 ms |
4184 KB |
Output is correct |
18 |
Correct |
12 ms |
4188 KB |
Output is correct |
19 |
Correct |
7 ms |
4188 KB |
Output is correct |
20 |
Correct |
2 ms |
4172 KB |
Output is correct |
21 |
Correct |
4 ms |
4188 KB |
Output is correct |
22 |
Correct |
3 ms |
4188 KB |
Output is correct |
23 |
Correct |
13 ms |
4188 KB |
Output is correct |
24 |
Correct |
11 ms |
4188 KB |
Output is correct |
25 |
Correct |
6 ms |
4184 KB |
Output is correct |
26 |
Correct |
5 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4188 KB |
Output is correct |
28 |
Correct |
4 ms |
4188 KB |
Output is correct |
29 |
Correct |
16 ms |
4292 KB |
Output is correct |
30 |
Correct |
3 ms |
4188 KB |
Output is correct |
31 |
Correct |
2 ms |
4184 KB |
Output is correct |
32 |
Correct |
3 ms |
4188 KB |
Output is correct |
33 |
Execution timed out |
3029 ms |
8396 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |