#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
// #define LOCAL
#ifdef LOCAL
#else
#include "catdog.h"
#endif
int N, st[MX];
vi adj[MX];
pi get(int x, int p) {
pi bes = {0,0};
for (int i: adj[x]) if (i != p) {
pi t = get(i,x);
bes.f += t.f, bes.s += t.s;
}
if (st[x] == 1) bes.s = bes.f+1;
else if (st[x] == 2) bes.f = bes.s+1;
else {
bes.f = min(bes.f,bes.s+1);
bes.s = min(bes.s,bes.f+1);
}
return bes;
}
int query() {
pi x = get(1,0);
return min(x.f,x.s);
}
void initialize(int n, std::vector<int> A, std::vector<int> B) {
N = n;
F0R(i,N-1) adj[A[i]].pb(B[i]), adj[B[i]].pb(A[i]);
}
int cat(int v) {
st[v] = 1;
return query();
}
int dog(int v) {
st[v] = 2;
return query();
}
int neighbor(int v) {
st[v] = 0;
return query();
}
#ifdef LOCAL
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;
}
#endif
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2724 KB |
Output is correct |
4 |
Correct |
4 ms |
2888 KB |
Output is correct |
5 |
Correct |
4 ms |
2888 KB |
Output is correct |
6 |
Correct |
5 ms |
2888 KB |
Output is correct |
7 |
Correct |
5 ms |
2888 KB |
Output is correct |
8 |
Correct |
4 ms |
2888 KB |
Output is correct |
9 |
Correct |
5 ms |
2928 KB |
Output is correct |
10 |
Correct |
6 ms |
2928 KB |
Output is correct |
11 |
Correct |
5 ms |
3072 KB |
Output is correct |
12 |
Correct |
5 ms |
3080 KB |
Output is correct |
13 |
Correct |
5 ms |
3080 KB |
Output is correct |
14 |
Correct |
3 ms |
3080 KB |
Output is correct |
15 |
Correct |
5 ms |
3080 KB |
Output is correct |
16 |
Correct |
4 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2724 KB |
Output is correct |
4 |
Correct |
4 ms |
2888 KB |
Output is correct |
5 |
Correct |
4 ms |
2888 KB |
Output is correct |
6 |
Correct |
5 ms |
2888 KB |
Output is correct |
7 |
Correct |
5 ms |
2888 KB |
Output is correct |
8 |
Correct |
4 ms |
2888 KB |
Output is correct |
9 |
Correct |
5 ms |
2928 KB |
Output is correct |
10 |
Correct |
6 ms |
2928 KB |
Output is correct |
11 |
Correct |
5 ms |
3072 KB |
Output is correct |
12 |
Correct |
5 ms |
3080 KB |
Output is correct |
13 |
Correct |
5 ms |
3080 KB |
Output is correct |
14 |
Correct |
3 ms |
3080 KB |
Output is correct |
15 |
Correct |
5 ms |
3080 KB |
Output is correct |
16 |
Correct |
4 ms |
3152 KB |
Output is correct |
17 |
Correct |
13 ms |
3152 KB |
Output is correct |
18 |
Correct |
15 ms |
3152 KB |
Output is correct |
19 |
Correct |
9 ms |
3152 KB |
Output is correct |
20 |
Correct |
4 ms |
3152 KB |
Output is correct |
21 |
Correct |
6 ms |
3160 KB |
Output is correct |
22 |
Correct |
6 ms |
3160 KB |
Output is correct |
23 |
Correct |
16 ms |
3232 KB |
Output is correct |
24 |
Correct |
13 ms |
3232 KB |
Output is correct |
25 |
Correct |
8 ms |
3232 KB |
Output is correct |
26 |
Correct |
7 ms |
3232 KB |
Output is correct |
27 |
Correct |
6 ms |
3232 KB |
Output is correct |
28 |
Correct |
9 ms |
3324 KB |
Output is correct |
29 |
Correct |
17 ms |
3332 KB |
Output is correct |
30 |
Correct |
6 ms |
3332 KB |
Output is correct |
31 |
Correct |
6 ms |
3352 KB |
Output is correct |
32 |
Correct |
8 ms |
3352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2724 KB |
Output is correct |
4 |
Correct |
4 ms |
2888 KB |
Output is correct |
5 |
Correct |
4 ms |
2888 KB |
Output is correct |
6 |
Correct |
5 ms |
2888 KB |
Output is correct |
7 |
Correct |
5 ms |
2888 KB |
Output is correct |
8 |
Correct |
4 ms |
2888 KB |
Output is correct |
9 |
Correct |
5 ms |
2928 KB |
Output is correct |
10 |
Correct |
6 ms |
2928 KB |
Output is correct |
11 |
Correct |
5 ms |
3072 KB |
Output is correct |
12 |
Correct |
5 ms |
3080 KB |
Output is correct |
13 |
Correct |
5 ms |
3080 KB |
Output is correct |
14 |
Correct |
3 ms |
3080 KB |
Output is correct |
15 |
Correct |
5 ms |
3080 KB |
Output is correct |
16 |
Correct |
4 ms |
3152 KB |
Output is correct |
17 |
Correct |
13 ms |
3152 KB |
Output is correct |
18 |
Correct |
15 ms |
3152 KB |
Output is correct |
19 |
Correct |
9 ms |
3152 KB |
Output is correct |
20 |
Correct |
4 ms |
3152 KB |
Output is correct |
21 |
Correct |
6 ms |
3160 KB |
Output is correct |
22 |
Correct |
6 ms |
3160 KB |
Output is correct |
23 |
Correct |
16 ms |
3232 KB |
Output is correct |
24 |
Correct |
13 ms |
3232 KB |
Output is correct |
25 |
Correct |
8 ms |
3232 KB |
Output is correct |
26 |
Correct |
7 ms |
3232 KB |
Output is correct |
27 |
Correct |
6 ms |
3232 KB |
Output is correct |
28 |
Correct |
9 ms |
3324 KB |
Output is correct |
29 |
Correct |
17 ms |
3332 KB |
Output is correct |
30 |
Correct |
6 ms |
3332 KB |
Output is correct |
31 |
Correct |
6 ms |
3352 KB |
Output is correct |
32 |
Correct |
8 ms |
3352 KB |
Output is correct |
33 |
Execution timed out |
3041 ms |
7776 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |