#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];
int parent[100005];
void calcdp(int nod)
{
dp[nod][0]=0;
dp[nod][1]=0;
dp[nod][2]=0;
for(auto adj:con[nod])
{
if(adj!=parent[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 dfs(int nod, int par)
{
parent[nod]=par;
for(auto adj:con[nod])
if(adj!=par)
dfs(adj,nod);
calcdp(nod);
}
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]);
}
dfs(1,0);
}
int cat(int v)
{
tip[v]=1;
int cur=v;
while(cur!=0)
{
calcdp(cur);
cur=parent[cur];
}
return min({dp[1][0],dp[1][1],dp[1][2]});
}
int dog(int v)
{
tip[v]=2;
int cur=v;
while(cur!=0)
{
calcdp(cur);
cur=parent[cur];
}
return min({dp[1][0],dp[1][1],dp[1][2]});
}
int neighbor(int v)
{
tip[v]=0;
int cur=v;
while(cur!=0)
{
calcdp(cur);
cur=parent[cur];
}
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 |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4692 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4536 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4692 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4536 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4584 KB |
Output is correct |
24 |
Correct |
2 ms |
4584 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
4 ms |
4440 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4592 KB |
Output is correct |
32 |
Correct |
2 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4692 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4536 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4584 KB |
Output is correct |
24 |
Correct |
2 ms |
4584 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
4 ms |
4440 KB |
Output is correct |
30 |
Correct |
2 ms |
4444 KB |
Output is correct |
31 |
Correct |
2 ms |
4592 KB |
Output is correct |
32 |
Correct |
2 ms |
4696 KB |
Output is correct |
33 |
Correct |
184 ms |
8640 KB |
Output is correct |
34 |
Correct |
72 ms |
8796 KB |
Output is correct |
35 |
Correct |
306 ms |
8264 KB |
Output is correct |
36 |
Correct |
325 ms |
11464 KB |
Output is correct |
37 |
Correct |
10 ms |
6456 KB |
Output is correct |
38 |
Correct |
349 ms |
12308 KB |
Output is correct |
39 |
Correct |
365 ms |
12352 KB |
Output is correct |
40 |
Correct |
450 ms |
12308 KB |
Output is correct |
41 |
Correct |
444 ms |
12356 KB |
Output is correct |
42 |
Correct |
499 ms |
12352 KB |
Output is correct |
43 |
Correct |
404 ms |
12312 KB |
Output is correct |
44 |
Correct |
434 ms |
12312 KB |
Output is correct |
45 |
Correct |
627 ms |
12312 KB |
Output is correct |
46 |
Correct |
426 ms |
12352 KB |
Output is correct |
47 |
Correct |
330 ms |
12352 KB |
Output is correct |
48 |
Execution timed out |
3026 ms |
10192 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |