Submission #65538

#TimeUsernameProblemLanguageResultExecution timeMemory
65538BenqCats or Dogs (JOI18_catdog)C++11
38 / 100
3041 ms7776 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...