#include "catdog.h"
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
typedef long long int lld;
int x;
vector<int> tree[1000000];
int animal[1000000];
int n;
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n=N;
vector<int>nei[N];
for(int i=0;i<N-1;i++){A[i]--;B[i]--;
nei[A[i]].push_back(B[i]);
nei[B[i]].push_back(A[i]);
}
queue<int>q;
int dist[n];
for(int i=0;i<n;i++){
dist[i]=-1;
}
dist[0]=0;
q.push(0);
dist[0]=0;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<nei[u].size();i++){
int v=nei[u][i];
if(dist[v]==-1){
dist[v]=dist[u]+1;
q.push(v);
}
}
}
for(int i=0;i<n-1;i++){
if(dist[A[i]]>dist[B[i]]){//cout<<B[i]<<" "<<A[i]<<endl;
tree[B[i]].push_back(A[i]);
}else{//cout<<A[i]<<" "<<B[i]<<endl;
tree[A[i]].push_back(B[i]);
}
}
for(int i=0;i<n;i++)animal[i]=0;
}
lld DP[1000000][3];
lld trabalha(int node,int territory){
if(DP[node][territory]!=-1)return DP[node][territory];
if(animal[node]>0 && territory!=animal[node])return 1000000000;
if(tree[node].size()==0){
DP[node][territory]=0;
return 0;
}
DP[node][territory]=0;
if(territory==1){
for(int i=0;i<tree[node].size();i++){int v=tree[node][i];
DP[node][territory]+=min(1+trabalha(v,0),min(trabalha(v,1),1+trabalha(v,2)));
}
}
if(territory==2){
for(int i=0;i<tree[node].size();i++){int v=tree[node][i];
DP[node][territory]+=min(1+trabalha(v,0),min(trabalha(v,2),1+trabalha(v,1)));
}
}
if(territory==0){
for(int i=0;i<tree[node].size();i++){int v=tree[node][i];
DP[node][territory]+=min(trabalha(v,0),min(1+trabalha(v,1),1+trabalha(v,2)));
}
}
return DP[node][territory];
}
int cat(int v) {v--;
animal[v]=1;
for(int i=0;i<n;i++){
DP[i][0]=-1;
DP[i][1]=-1;
DP[i][2]=-1;
}
//cout<<trabalha(0,1)<<endl;
return min(trabalha(0,0),min(trabalha(0,1),trabalha(0,2)));
}
int dog(int v) {v--;
animal[v]=2;
for(int i=0;i<n;i++){
DP[i][0]=-1;
DP[i][1]=-1;
DP[i][2]=-1;
}
return min(trabalha(0,0),min(trabalha(0,1),trabalha(0,2)));
}
int neighbor(int v){v--;
animal[v]=0;
for(int i=0;i<n;i++){
DP[i][0]=-1;
DP[i][1]=-1;
DP[i][2]=-1;
}
return min(trabalha(0,0),min(trabalha(0,1),trabalha(0,2)));
}
Compilation message
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:28:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<nei[u].size();i++){
~^~~~~~~~~~~~~~
catdog.cpp: In function 'lld trabalha(int, int)':
catdog.cpp:57:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<tree[node].size();i++){int v=tree[node][i];
~^~~~~~~~~~~~~~~~~~
catdog.cpp:62:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<tree[node].size();i++){int v=tree[node][i];
~^~~~~~~~~~~~~~~~~~
catdog.cpp:67:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<tree[node].size();i++){int v=tree[node][i];
~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23908 KB |
Output is correct |
3 |
Correct |
22 ms |
23984 KB |
Output is correct |
4 |
Correct |
26 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
24064 KB |
Output is correct |
6 |
Correct |
24 ms |
24084 KB |
Output is correct |
7 |
Correct |
24 ms |
24084 KB |
Output is correct |
8 |
Correct |
24 ms |
24088 KB |
Output is correct |
9 |
Correct |
26 ms |
24088 KB |
Output is correct |
10 |
Correct |
23 ms |
24088 KB |
Output is correct |
11 |
Correct |
22 ms |
24088 KB |
Output is correct |
12 |
Correct |
24 ms |
24088 KB |
Output is correct |
13 |
Correct |
24 ms |
24088 KB |
Output is correct |
14 |
Correct |
24 ms |
24088 KB |
Output is correct |
15 |
Correct |
24 ms |
24244 KB |
Output is correct |
16 |
Correct |
24 ms |
24244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23908 KB |
Output is correct |
3 |
Correct |
22 ms |
23984 KB |
Output is correct |
4 |
Correct |
26 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
24064 KB |
Output is correct |
6 |
Correct |
24 ms |
24084 KB |
Output is correct |
7 |
Correct |
24 ms |
24084 KB |
Output is correct |
8 |
Correct |
24 ms |
24088 KB |
Output is correct |
9 |
Correct |
26 ms |
24088 KB |
Output is correct |
10 |
Correct |
23 ms |
24088 KB |
Output is correct |
11 |
Correct |
22 ms |
24088 KB |
Output is correct |
12 |
Correct |
24 ms |
24088 KB |
Output is correct |
13 |
Correct |
24 ms |
24088 KB |
Output is correct |
14 |
Correct |
24 ms |
24088 KB |
Output is correct |
15 |
Correct |
24 ms |
24244 KB |
Output is correct |
16 |
Correct |
24 ms |
24244 KB |
Output is correct |
17 |
Correct |
51 ms |
24244 KB |
Output is correct |
18 |
Correct |
57 ms |
24252 KB |
Output is correct |
19 |
Correct |
44 ms |
24264 KB |
Output is correct |
20 |
Correct |
25 ms |
24264 KB |
Output is correct |
21 |
Correct |
27 ms |
24264 KB |
Output is correct |
22 |
Correct |
34 ms |
24264 KB |
Output is correct |
23 |
Correct |
63 ms |
24340 KB |
Output is correct |
24 |
Correct |
60 ms |
24340 KB |
Output is correct |
25 |
Correct |
36 ms |
24340 KB |
Output is correct |
26 |
Correct |
32 ms |
24340 KB |
Output is correct |
27 |
Correct |
31 ms |
24340 KB |
Output is correct |
28 |
Correct |
34 ms |
24344 KB |
Output is correct |
29 |
Correct |
57 ms |
24352 KB |
Output is correct |
30 |
Correct |
28 ms |
24364 KB |
Output is correct |
31 |
Correct |
28 ms |
24448 KB |
Output is correct |
32 |
Correct |
38 ms |
24448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23908 KB |
Output is correct |
3 |
Correct |
22 ms |
23984 KB |
Output is correct |
4 |
Correct |
26 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
24064 KB |
Output is correct |
6 |
Correct |
24 ms |
24084 KB |
Output is correct |
7 |
Correct |
24 ms |
24084 KB |
Output is correct |
8 |
Correct |
24 ms |
24088 KB |
Output is correct |
9 |
Correct |
26 ms |
24088 KB |
Output is correct |
10 |
Correct |
23 ms |
24088 KB |
Output is correct |
11 |
Correct |
22 ms |
24088 KB |
Output is correct |
12 |
Correct |
24 ms |
24088 KB |
Output is correct |
13 |
Correct |
24 ms |
24088 KB |
Output is correct |
14 |
Correct |
24 ms |
24088 KB |
Output is correct |
15 |
Correct |
24 ms |
24244 KB |
Output is correct |
16 |
Correct |
24 ms |
24244 KB |
Output is correct |
17 |
Correct |
51 ms |
24244 KB |
Output is correct |
18 |
Correct |
57 ms |
24252 KB |
Output is correct |
19 |
Correct |
44 ms |
24264 KB |
Output is correct |
20 |
Correct |
25 ms |
24264 KB |
Output is correct |
21 |
Correct |
27 ms |
24264 KB |
Output is correct |
22 |
Correct |
34 ms |
24264 KB |
Output is correct |
23 |
Correct |
63 ms |
24340 KB |
Output is correct |
24 |
Correct |
60 ms |
24340 KB |
Output is correct |
25 |
Correct |
36 ms |
24340 KB |
Output is correct |
26 |
Correct |
32 ms |
24340 KB |
Output is correct |
27 |
Correct |
31 ms |
24340 KB |
Output is correct |
28 |
Correct |
34 ms |
24344 KB |
Output is correct |
29 |
Correct |
57 ms |
24352 KB |
Output is correct |
30 |
Correct |
28 ms |
24364 KB |
Output is correct |
31 |
Correct |
28 ms |
24448 KB |
Output is correct |
32 |
Correct |
38 ms |
24448 KB |
Output is correct |
33 |
Execution timed out |
3007 ms |
31664 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |