結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー eQeeQe
提出日時 2024-12-03 14:20:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 6,425 bytes
コンパイル時間 6,177 ms
コンパイル使用メモリ 327,024 KB
実行使用メモリ 65,052 KB
最終ジャッジ日時 2024-12-03 14:21:00
合計ジャッジ時間 13,022 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 113 ms
64,872 KB
testcase_12 AC 119 ms
65,012 KB
testcase_13 AC 101 ms
65,052 KB
testcase_14 AC 96 ms
65,040 KB
testcase_15 AC 127 ms
54,856 KB
testcase_16 AC 78 ms
38,376 KB
testcase_17 AC 77 ms
38,376 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 128 ms
57,380 KB
testcase_20 AC 140 ms
56,080 KB
testcase_21 AC 142 ms
56,036 KB
testcase_22 AC 99 ms
55,016 KB
testcase_23 AC 126 ms
55,764 KB
testcase_24 AC 132 ms
56,128 KB
testcase_25 AC 149 ms
55,480 KB
testcase_26 AC 119 ms
54,804 KB
testcase_27 AC 115 ms
57,552 KB
testcase_28 AC 102 ms
54,888 KB
testcase_29 AC 100 ms
57,660 KB
testcase_30 AC 123 ms
55,332 KB
testcase_31 AC 113 ms
57,032 KB
testcase_32 AC 119 ms
56,204 KB
testcase_33 AC 118 ms
56,148 KB
testcase_34 AC 132 ms
56,088 KB
testcase_35 AC 124 ms
55,304 KB
testcase_36 AC 121 ms
55,264 KB
testcase_37 AC 121 ms
56,008 KB
testcase_38 AC 121 ms
55,128 KB
testcase_39 AC 117 ms
57,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
namespace my{
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define VL(n,...) vec<ll>__VA_ARGS__;setsize({n},__VA_ARGS__);lin(__VA_ARGS__)
#define VV(n,...) vec<ll>__VA_ARGS__;setsize({n},__VA_ARGS__);vin(__VA_ARGS__)
#define FO(n) for(ll ij=n;ij--;)
#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
constexpr auto range(bool s,auto...a){array<ll,3>r{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;return r;}
constexpr char newline=10;
constexpr char space=32;

template<class A,class B>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  pair(const std::pair<A,B>&p):a(p.first),b(p.second){}
  auto operator<=>(const pair&)const=default;
  friend ostream&operator<<(ostream&o,const pair&p){return o<<p.a<<space<<p.b;}
};

auto&rev(auto&a){ranges::reverse(a);return a;}
auto pop_front(auto&a){assert(a.size());auto r=*a.begin();a.pop_front();return r;}

template<class T,class U>ostream&operator<<(ostream&o,const std::pair<T,U>&p){return o<<p.first<<space<<p.second;}

template<class T>struct deque:std::deque<T>{
  deque(const initializer_list<T>&a={}){fe(a,e)this->eb(e);}
  deque(const vector<T>&a){fe(a,e)this->eb(e);}
  T sum()const{T r{};fe(*this,e)r+=e;return r;}
  friend ostream&operator<<(ostream&o,const deque&q){fe(q,e)o<<e<<string(&e!=&q.back(),space);return o;}
};

template<class V>concept vectorial=is_base_of_v<vector<typename V::value_type>,V>;
template<class T>struct vec_attr{using core_type=T;static constexpr int d=0;};
template<vectorial V>struct vec_attr<V>{using core_type=typename vec_attr<typename V::value_type>::core_type;static constexpr int d=vec_attr<typename V::value_type>::d+1;};
template<class T>using core_t=vec_attr<T>::core_type;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<string(&e!=&v.back(),vectorial<V>?newline:space);return o;}

template<class V>struct vec:vector<V>{
  using vector<V>::vector;
  vec(const vector<V>&v){vector<V>::operator=(v);}

  vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
  vec operator^(const vec&u)const{return vec{*this}^=u;}
  vec&operator+=(const vec&u){vec&v=*this;fo(i,v.size())v[i]+=u[i];return v;}
  vec&operator-=(const vec&u){vec&v=*this;fo(i,v.size())v[i]-=u[i];return v;}
  vec operator+(const vec&u)const{return vec{*this}+=u;}
  vec operator-(const vec&u)const{return vec{*this}-=u;}
  vec&operator++(){fe(*this,e)++e;return*this;}
  vec&operator--(){fe(*this,e)--e;return*this;}
  vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;}

  auto scan(const auto&f)const{pair<core_t<V>,bool>r{};fe(*this,e)if constexpr(!vectorial<V>)r.b?f(r.a,e),r:r={e,1};else if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;return r;}
  auto sum()const{return scan([](auto&a,const auto&b){a+=b;}).a;}

  void pop_front(){this->erase(this->begin());}
};
template<class T=ll,size_t n,size_t i=0>auto make_vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)return vec<T>(s[i],x);else{auto X=make_vec<T,n,i+1>(s,x);return vec<decltype(X)>(s[i],X);}}
template<ll n,class...A>void setsize(const ll(&l)[n],A&...a){((a=make_vec(l,core_t<A>())),...);}

void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
template<char c=space>void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<string(--n>0,c)),...);cout<<newline;}

void dec(auto&...a){((--a),...);}

template<class WT>struct edge{
  ll from,to;
  WT wt;
  ll id;
  edge()=default;
  edge(ll from,ll to,WT wt=1,ll id=-1):from(from),to(to),wt(wt),id(id){}
  auto operator<=>(const edge&e)const{return wt<=>e.wt;}
  friend ostream&operator<<(ostream&o,const edge&e){return o<<"(to "<<e.to<<", wt "<<e.wt<<")";}
};

template<class WT>struct graph{
  vec<vec<edge<WT>>>edges;

  graph()=default;
  graph(ll n):edges(n){}

  decltype(auto)operator[](ll i){return edges[i];}
  decltype(auto)operator[](ll i)const{return edges[i];}

  ll size()const{return edges.size();}

  friend ostream&operator<<(ostream&o,const graph&g){
    fo(u,g.size()){
      o<<"from "<<u<<": ";
      fo(j,g[u].size())o<<g[u][j]<<string(j!=g[u].size()-1,space);
      o<<newline;
    }
    return o;
  }

  template<class...A>void add_edge(ll a,A&&...b){edges[a].eb(a,std::forward<A>(b)...);}
  void add_edge(const edge<WT>&e){edges[e.from].eb(e);}
  void add_edges(const vec<ll>&a,const vec<ll>&b){fo(i,a.size())edges[a[i]].eb(a[i],b[i],1,i);}
  void add_edges(const vec<ll>&a,const vec<ll>&b,const vec<WT>&w){fo(i,a.size())edges[a[i]].eb(a[i],b[i],w[i],i);}
  void add_edges(const vec<edge<WT>>&es){fe(es,e)edges[e.from].eb(e);}
};

template<class WT>struct rooted_tree:graph<WT>{
  vec<edge<WT>>rev_edge;
  vec<ll>dep;
  vec<WT>dist;
  vec<ll>topological;
  ll r;
  rooted_tree(ll n,ll r):graph<WT>(n),rev_edge(n,{-1,-1,WT(),-1}),dep(n),dist(n),r(r){}

  ll par(ll u)const{return rev_edge[u].from;}
  ll root()const{return r;}

  void add_edge(const edge<WT>&e){
    if(e.from!=-1){
      graph<WT>::add_edge(e);
      dep[e.to]=dep[e.from]+1;
      dist[e.to]=dist[e.from]+e.wt;
    }
    rev_edge[e.to]=e;
    topological.eb(e.to);
  }
};

template<class WT>auto bfs_tree(const graph<WT>&g,ll s=0){
  rooted_tree<WT>bfs_tree(g.size(),s);

  vec<ll>used(g.size());
  deque<edge<WT>>q{{-1,s,WT(),-1}};
  while(q.size()){
    auto pu=q.front();q.pop_front();
    auto[p,u,w,_]=pu;
    if(used[u])continue;
    used[u]=1;

    bfs_tree.add_edge(pu);

    fe(g[u],e)if(!used[e.to])q.eb(e);
  }
  return bfs_tree;
}
template<class WT>auto bfs_tree(const rooted_tree<WT>&g){return bfs_tree(g,g.root());}

single_testcase
void solve(){
  LL(N);
  VV(N-1,a,b);dec(a,b);
  VL(N,c);
  graph<ll>g(N);
  g.add_edges(a,b);
  g.add_edges(b,a);

  auto tree=bfs_tree(g);
  auto o=tree.topological;
  rev(o);

  ll ans=0;
  fe(o,v){
    if(v==0)continue;
    if(c[v]==0){
      c[v]^=1;
      c[tree.par(v)]^=1;
      ans++;
    }
  }
  pp(c.sum()==N?ans:-1);
}}
0