#include "catdog.h"
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,tune=native")
//huhu
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
#define ull unsigned long long
const int mxn = 1e5 + 5, inf = 1e9 + 5;
int n;
vt<int>adj[mxn + 1];
int dp[mxn + 1][3], sign[mxn + 1];
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
for(int i = 0; i < n; i++){
int u = A[i], v = B[i];
adj[u].pb(v); adj[v].pb(u);
}
}
void dfs(int s, int pre){
if(sign[s]){
dp[s][sign[s]] = 0;
for(auto i: adj[s]){
if(i != pre){
dfs(i, s); int cand = inf;
for(int j = 0; j < 3; j++){
if(!j || j == sign[s])cand = min(cand, dp[i][j]);
else cand = min(cand, dp[i][j] + 1);
}
dp[s][sign[s]] += cand;
}
}
}else{
dp[s][0] = dp[s][1] = dp[s][2] = 0;
for(auto i: adj[s]){
if(i != pre){
dfs(i, s);
for(int j = 0; j < 3; j++){
int cand = inf;
for(int k = 0; k < 3; k++){
if(!k || k == j)cand = min(cand, dp[i][k]);
else cand = min(cand, dp[i][k] + 1);
}
dp[s][j] += cand;
}
}
}
}
}
int solve(){
for(int i = 1; i <= n; i++){
dp[i][0] = dp[i][1] = dp[i][2] = inf;
}
dfs(1, -1);
return(min(min(dp[1][0], dp[1][1]), dp[1][2]));
}
int cat(int v) {
sign[v] = 1;
return(solve());
}
int dog(int v) {
sign[v] = 2;
return(solve());
}
int neighbor(int v) {
sign[v] = 0;
return(solve());
}
/*
#include <cstdio>
#include <cassert>
#include <vector>
#include <stdlib.h>
int readInt(){
int i;
if(scanf("%d",&i)!=1){
fprintf(stderr,"Error while reading input\n");
exit(1);
}
return i;
}
int main(){
int N=readInt();
std::vector<int> A(N-1),B(N-1);
for(int i=0;i<N-1;i++)
{
A[i]=readInt();
B[i]=readInt();
}
int Q;
assert(scanf("%d",&Q)==1);
std::vector <int> T(Q),V(Q);
for(int i=0;i<Q;i++)
{
T[i]=readInt();
V[i]=readInt();
}
initialize(N,A,B);
std::vector<int> res(Q);
for(int j=0;j<Q;j++)
{
if(T[j]==1) res[j]=cat(V[j]);
else if(T[j]==2) res[j]=dog(V[j]);
else res[j]=neighbor(V[j]);
}
for(int j=0;j<Q;j++)
printf("%d\n",res[j]);
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
1 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
1 ms |
3932 KB |
Output is correct |
15 |
Correct |
1 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
1 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
1 ms |
3932 KB |
Output is correct |
15 |
Correct |
1 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
17 |
Correct |
11 ms |
4188 KB |
Output is correct |
18 |
Correct |
14 ms |
4188 KB |
Output is correct |
19 |
Correct |
7 ms |
4184 KB |
Output is correct |
20 |
Correct |
1 ms |
4172 KB |
Output is correct |
21 |
Correct |
3 ms |
4192 KB |
Output is correct |
22 |
Correct |
4 ms |
4188 KB |
Output is correct |
23 |
Correct |
16 ms |
4236 KB |
Output is correct |
24 |
Correct |
13 ms |
4188 KB |
Output is correct |
25 |
Correct |
6 ms |
4184 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4176 KB |
Output is correct |
28 |
Correct |
5 ms |
4188 KB |
Output is correct |
29 |
Correct |
20 ms |
4188 KB |
Output is correct |
30 |
Correct |
3 ms |
4184 KB |
Output is correct |
31 |
Correct |
3 ms |
4184 KB |
Output is correct |
32 |
Correct |
5 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
3932 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
3932 KB |
Output is correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Correct |
1 ms |
3932 KB |
Output is correct |
9 |
Correct |
1 ms |
3932 KB |
Output is correct |
10 |
Correct |
1 ms |
3932 KB |
Output is correct |
11 |
Correct |
1 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
1 ms |
3932 KB |
Output is correct |
14 |
Correct |
1 ms |
3932 KB |
Output is correct |
15 |
Correct |
1 ms |
3932 KB |
Output is correct |
16 |
Correct |
2 ms |
3932 KB |
Output is correct |
17 |
Correct |
11 ms |
4188 KB |
Output is correct |
18 |
Correct |
14 ms |
4188 KB |
Output is correct |
19 |
Correct |
7 ms |
4184 KB |
Output is correct |
20 |
Correct |
1 ms |
4172 KB |
Output is correct |
21 |
Correct |
3 ms |
4192 KB |
Output is correct |
22 |
Correct |
4 ms |
4188 KB |
Output is correct |
23 |
Correct |
16 ms |
4236 KB |
Output is correct |
24 |
Correct |
13 ms |
4188 KB |
Output is correct |
25 |
Correct |
6 ms |
4184 KB |
Output is correct |
26 |
Correct |
3 ms |
4188 KB |
Output is correct |
27 |
Correct |
3 ms |
4176 KB |
Output is correct |
28 |
Correct |
5 ms |
4188 KB |
Output is correct |
29 |
Correct |
20 ms |
4188 KB |
Output is correct |
30 |
Correct |
3 ms |
4184 KB |
Output is correct |
31 |
Correct |
3 ms |
4184 KB |
Output is correct |
32 |
Correct |
5 ms |
4184 KB |
Output is correct |
33 |
Execution timed out |
3100 ms |
8560 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |