Submission #64088292


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/all>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define PI acos(-1)
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define mpa make_pair
#define mpt make_tuple
#define emb emplace_back
#define endll "\n"
using namespace std;
using namespace atcoder;
using ll = long long;
std::ostream& operator<<(std::ostream& os,const modint n){
  return os << n.val();
}
std::ostream& operator<<(std::ostream& os,const modint998244353 n){
  return os << n.val();
}
std::ostream& operator<<(std::ostream& os,const modint1000000007 n){
  return os << n.val();
}
template<class T> constexpr inline void input(vector<T> &v){
  for(int i=0;i<v.size();i++) cin >> v[i];
}
template<class T,class S> constexpr inline void input(vector<T> &v,vector<S> &u){
  for(int i=0;i<v.size();i++) cin >> v[i] >> u[i];
}
template<class T,class S,class R> constexpr inline void input(vector<T> &v,vector<S> &u,vector<R> &t){
  for(int i=0;i<v.size();i++) cin >> v[i] >> u[i] >> t[i];
}
template<class T> constexpr inline void input(vector<vector<T>> &v){
  for(int i=0;i<v.size();i++){
    for(int j=0;j<v[i].size();j++) cin >> v[i][j];
  }
}
template<class T> constexpr inline void input_graph(vector<vector<T>> &G,int inputcount = -1,const bool isdirect = false,const bool indexed = 1){
  if(inputcount == -1) inputcount = G.size()-1;
  T a,b;
  for(int i=0;i<inputcount;i++){
    cin >> a >> b;a -= indexed;b -= indexed;
    G[a].emb(b);
    if(!isdirect) G[b].emb(a);
  }
}
template<class T> constexpr inline void output(vector<T> &v,bool space = true){
  if(v.size() == 0){
    cout << endll;return;
  }
  if(space){
    for(int i=0;i<v.size()-1;i++) cout << v[i] << " ";
    cout << v.back() << endll;
  }
  else{
    for(int i=0;i<v.size();i++) cout << v[i] << endll;
  }
}
template<class T,class S> constexpr inline void output(vector<T> &v,vector<S> &u){
  for(int i=0;i<v.size();i++) cout << v[i] << " " << u[i] << endll;
}
template<class T,class S,class R> constexpr inline void output(vector<T> &v,vector<S> &u,vector<R> &t){
  for(int i=0;i<v.size();i++) cout << v[i] << " " << u[i] << " " << t[i] << endll;
}
template<class T> constexpr inline void output(vector<vector<T>> &v){
  for(int i=0;i<v.size();i++){
    if(v.size() == 0){
      cout << endll;continue;
    }
    for(int j=0;j<v[i].size()-1;j++) cout << v[i][j] << " ";
    cout << v[i].back() << endll;
  }
}
template<class T> constexpr inline bool on(T n,T i){
  return n&(1LL<<i);
}
template<class T,class S> constexpr inline T ceil(T x,S y){
  return (x+y-1)/y;
}
template<class T> constexpr bool isprime(T x){
  if(x <= 1) return false;
  for(T i=2;i*i<=x;i++){
    if(x%i == 0) return false; 
  }
  return true;
}
vector<bool> isprime_format(int n){
  vector<bool> P(n+1,1);P[0] = P[1] = 1;
  for(int i=2;i*i<=n;i++){
    if(!P[i]) continue;
    for(int j=i+i;j<=n;j+=i) P[j] = false;
  }
  return P;
}
vector<int> prime_format(int n){
  vector<bool> P = isprime_format(n);
  vector<int> ans;
  for(int i=2;i<=n;i++){
    if(P[i]) ans.emb(i);
  }
  return ans;
}
template<class T> vector<T> topo_sort(T N,const vector<vector<T>> &G){
  T i,j,f;
  vector<int> cnt(N);
  for(i=0;i<N;i++){
    for(j=0;j<G[i].size();j++) cnt[G[i][j]]++;
  }
  vector<T> q;
  for(i=0;i<N;i++){
    if(cnt[i] == 0) q.emb(i);
  }
  for(f=0;f<q.size();f++){
    for(i=0;i<G[q[f]].size();i++){
      cnt[G[q[f]][i]]--;
      if(cnt[G[q[f]][i]] == 0){
        q.emb(G[q[f]][i]);
      }
    }
  }
  return q;
}
template<class T> vector<T> dijkstra(T N,vector<T> &st,vector<vector<pair<T,T>>> &G,const T inf = -1){
  T fn,fp,i;
  priority_queue<pair<T,T>,vector<pair<T,T>>,greater<>> q;
  vector<T> D(N,inf);
  for(i=0;i<st.size();i++){
    D[st[i]] = 0;
    q.push(mpa(0,st[i]));
  }
  while(!q.empty()){
    fn = q.top().fi;fp = q.top().se;q.pop();
    if(D[fp] < fn) continue;
    for(i=0;i<G[fp].size();i++){
      if(D[G[fp][i].fi] == -1 || D[G[fp][i].fi] > D[fp]+G[fp][i].se){
        D[G[fp][i].fi] = D[fp]+G[fp][i].se;
        q.push(mpa(D[G[fp][i].fi],G[fp][i].fi));
      }
    }
  }
  return D;
}
template<class T> vector<T> dijkstra(T N,T st,vector<vector<pair<T,T>>> &G,const T inf = -1){
  vector<T> st_vec({st});
  return dijkstra(N,st_vec,G,inf);
}
template<class T> class WarshallFloyd{
  T N,inf;
  vector<vector<T>> D;
  vector<vector<T>> prev;
  bool isdirect;
  void setting(){
    for(T k=0;k<N;k++){
      for(T i=0;i<N;i++){
        for(T j=0;j<N;j++){
          if(D[i][k] == inf || D[k][j] == inf) continue;
          if(D[i][j] > D[i][k]+D[k][j]){
            D[i][j] > D[i][k]+D[k][j];
            prev[i][j] = prev[k][j];
          } 
        }
      }
    }
  }
public:
  WarshallFloyd(T N,vector<T> &u,vector<T> &v,vector<T> &c,T inf,bool isdirect){
    this->N = N;
    this->inf = inf;
    this->isdirect = isdirect;
    assert(u.size() == v.size());
    vector<vector<T>>(N,vector<T>(N,inf)).swap(D);
    vector<vector<T>>(N,vector<T>(N)).swap(prev);
    for(T i=0;i<N;i++){
      for(T j=0;j<N;j++) prev[i][j] = i;
    }
    for(T i=0;i<N;i++) D[i][i] = 0;
    for(T i=0;i<u.size();i++){
      D[u[i]][v[i]] = min(D[u[i]][v[i]],c[i]);
      if(!isdirect) D[v[i]][u[i]] = min(D[v[i]][u[i]],c[i]);
    }
    setting();
  }
  WarshallFloyd(vector<vector<T>> D,T inf,bool isdirect){
    this->N = D.size();
    this->inf = inf;
    this->D = D;
    this->isdirect = isdirect;
    vector<vector<T>>(N,vector<T>(N)).swap(prev);
    for(T i=0;i<N;i++){
      for(T j=0;j<N;j++) prev[i][j] = i;
    }
    setting();
  }
  void append(T s,T t,T c){
    for(T i=0;i<N;i++){
      for(T j=0;j<N;j++){
        if(D[i][s] != inf && D[t][c] != inf){
          if(D[i][j] > D[i][s]+c+D[t][j]){
            D[i][j] = D[i][s]+c+D[t][j];
            prev[i][j] = prev[t][j];
          }
        }
        if(!isdirect && D[i][t] != inf && D[s][j] != inf){
          if(D[i][j] > D[i][t]+c+D[s][j]){
            D[i][j] = D[i][t]+c+D[s][j];
            prev[i][j] = prev[s][j];
          }
        }
      }
    }
  }
  T at(T i,T j){
    return D[i][j];
  }
  vector<T> Path(T s,T t){
    vector<T> ret;
    ret.emb(t);
    while(t != s) ret.emb(t=prev[s][t]);
    reverse(all(ret));
    return ret;
  }
  bool negative_cycle(){
    for(T i=0;i<N;i++){
      if(D[i][i] < 0) return true;
    }
    return false;
  }
  vector<vector<T>> Graph(){
    return D;
  }
};
template<class T> class mat{
  vector<vector<T>> V;
public:
  constexpr mat(){}
  constexpr mat(int N,int M){
    vector<vector<T>>(N,vector<T>(M)).swap(this->V);
  }
  constexpr mat(vector<vector<T>> &v){
    this->V = v;
  }
  constexpr int height(){return V.size();}
  constexpr int width(){return V[0].size();}
  constexpr T &val(int a,int b){return V[a][b];}
  constexpr vector<T> &val(int a){return V[a];}
  constexpr vector<vector<T>> &val(){return V;}
  //ret(mat[i][j],elem(a[i][k],b[k][j]))
  constexpr mat calc(mat &b,function<T(T,T)> ret = [](T x,T y){return x+y;},function<T(T,T)> elem = [](T x,T y){return x*y;})const{
    vector<vector<T>> c(V.size(),vector<T>(b.width()));
    for(int i=0;i<V.size();i++){
      for(int k=0;k<b.height();k++){
        for(int j=0;j<b.width();j++) c[i][j] = ret(c[i][j],elem(V[i][k],b.val(k,j)));
      }
    }
    return mat(c);
  }
  constexpr mat pow(ll y,function<T(T,T)> ret = [](T x,T y){return x+y;},function<T(T,T)> elem = [](T x,T y){return x*y;}) const {
    mat x = *this,z;
    while(y){
      if(y&1){
        if(z.height() == 0) z = x;
        else z = z.calc(x,ret,elem);
      }
      x = x.calc(x,ret,elem);
      y >>= 1;
    }
    return z;
  }
};
template<class T> class frac{
  T bunsi,bunbo;
  constexpr void setting() noexcept {
    T g = gcd(bunsi,bunbo);
    bunsi /= g;bunbo /= g;
    if(bunbo < 0){
      bunsi = -bunsi;bunbo = -bunbo;
    }
  }
public:
  constexpr frac(T Bunsi = 0,T Bunbo = 1) noexcept {
    bunsi = Bunsi;bunbo = Bunbo;
    setting();
  }
  constexpr T &Bunsi() noexcept {return bunsi;}
  constexpr const T &Bunsi() const noexcept {return bunsi;}
  constexpr T &Bunbo() noexcept {return bunbo;}
  constexpr const T &Bunbo() const noexcept {return bunbo;}
  constexpr frac<T> &operator+=(const frac<T> &rhs) noexcept {
    bunsi = bunsi*rhs.bunbo+bunbo*rhs.bunsi;
    bunbo *= rhs.bunbo;
    setting();
    return *this;
  }
  constexpr frac<T> &operator-=(const frac<T> &rhs) noexcept {
    bunsi = bunsi*rhs.bunbo-bunbo*rhs.bunsi;
    bunbo *= rhs.bunbo;
    setting();
    return *this;
  }
  constexpr frac<T> &operator*=(const frac<T> &rhs) noexcept {
    bunbo *= rhs.bunbo;
    bunsi *= rhs.bunsi;
    setting();
    return *this;
  }
  constexpr frac<T> &operator/=(const frac<T> &rhs) noexcept {
    bunbo *= rhs.bunsi;
    bunsi *= rhs.bunbo;
    setting();
    return *this;
  }
  constexpr frac<T> operator+(const frac<T> &rhs) const noexcept {return frac(*this) += rhs;}
  constexpr frac<T> operator-(const frac<T> &rhs) const noexcept {return frac(*this) -= rhs;}
  constexpr frac<T> operator*(const frac<T> &rhs) const noexcept {return frac(*this) *= rhs;}
  constexpr frac<T> operator/(const frac<T> &rhs) const noexcept {return frac(*this) /= rhs;}
  constexpr bool operator<(const frac<T> &rhs) const noexcept {return bunsi*rhs.bunbo < bunbo*rhs.bunsi;}
  constexpr bool operator>(const frac<T> &rhs) const noexcept {return bunsi*rhs.bunbo > bunbo*rhs.bunsi;}
  constexpr bool operator>=(const frac<T> &rhs) const noexcept {return bunsi*rhs.bunbo >= bunbo*rhs.bunsi;}
  constexpr bool operator<=(const frac<T> &rhs) const noexcept {return bunsi*rhs.bunbo <= bunbo*rhs.bunsi;}
  constexpr bool operator==(const frac<T> &rhs) const noexcept {return bunsi*rhs.bunbo == bunbo*rhs.bunsi;}
  constexpr bool operator!=(const frac<T> &rhs) const noexcept {return bunsi*rhs.bunbo != bunbo*rhs.bunsi;}
};
template<class T> class line{
  //y = ax+b;
  frac<T> a,b;
  bool a_inf;
  T inf_x;
public:
  constexpr line(T x1 = 0,T y1 = 0,T x2 = 1,T y2 = 1) noexcept {
    if(x1 != x2){
      a_inf = false;
      a = frac(y2-y1,x2-x1);
      b = frac(y1)-frac(x1)*a;
    }
    else{
      a_inf = true;
      inf_x = x1;
    }
  }
  constexpr frac<T> &slope() noexcept {return a;}
  constexpr const frac<T> &slope() const noexcept {return a;}
  constexpr frac<T> &inter() noexcept {return b;}
  constexpr const frac<T> &inter() const noexcept {return b;}
  constexpr bool match(const line &rhs) const noexcept {
    if(!a_inf && !rhs.a_inf) return a==rhs.a && b==rhs.b;
    else if(a_inf^rhs.a_inf) return false;
    else return inf_x==rhs.inf_x;
  }
  constexpr bool parallel(const line &rhs) const noexcept {
    if(!a_inf && !rhs.a_inf) return a==rhs.a;
    else return !(a_inf^rhs.a_inf);
  }
  constexpr pair<frac<T>,frac<T>> point(const line &rhs) const noexcept {
    //ax+b = y
    //cx+d = y
    //(a-c)x= d-b
    if(a_inf){
      frac<T> x(inf_x);
      frac<T> y = rhs.a*x+rhs.b;
      return make_pair(x,y);
    }
    else if(rhs.a_inf){
      frac<T> x(rhs.inf_x);
      frac<T> y = a*x+b;
      return make_pair(x,y);
    }
    else{
      frac<T> x = (rhs.b-b)/(a-rhs.a);
      frac<T> y = a*x+b;
      return make_pair(x,y);
    }
  }
};
int main(){
  cin.tie(0);ios::sync_with_stdio(false);
  //-----------------------------------------------
  int N;cin >> N;
  vector<vector<int>> G(N);
  for(int i=0;i<N-1;i++){
    int u,v;cin >> u >> v;u--;v--;
    G[u].emb(v);G[v].emb(u);
  }
  vector<short> B(N,-1);B[0] = 0;
  queue<int> q;q.push(0);
  while(!q.empty()){
    int f = q.front();q.pop();
    for(int nex:G[f]){
      if(B[nex] == -1){
        B[nex] = B[f]^1;q.push(nex);
      }
    }
  }
  vector<int> zero,one;
  for(int i=0;i<N;i++){
    (B[i]==0 ? zero:one).emb(i);
  }
  int Z = zero.size(),O = one.size();
  bool is_my_turn = (Z*O-(N-1))%2;
  cout << (is_my_turn ? "First":"Second") << endl;
  set<pair<int,int>> st;
  for(int z:zero)for(int o:one) st.emplace(z,o);
  for(int i=0;i<N;i++){
    for(int nex:G[i]) st.erase(mpa(i,nex));
  }
  while(true){
    if(is_my_turn){
      auto [z,o] = *st.begin();
      if(z > o) swap(z,o);
      cout << z+1 << " " << o+1 << endl;
      st.erase(mpa(z,o));
      st.erase(mpa(o,z));
      is_my_turn = false;
    }
    else{
      int z,o;cin >> z >> o;
      if(z==-1) return 0;
      z--;o--;
      st.erase(mpa(z,o));st.erase(mpa(o,z));
      is_my_turn = true;
    }
  }
}

Submission Info

Submission Time
Task E - Tree Game
User souta_1326
Language C++ 23 (gcc 12.2)
Score 425
Code Size 12785 Byte
Status AC
Exec Time 11 ms
Memory 3968 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status AC
AC × 29
Set Name Test Cases
Sample
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt
Case Name Status Exec Time Memory
hand_01.txt AC 3 ms 3684 KiB
random_01.txt AC 10 ms 3880 KiB
random_02.txt AC 10 ms 3952 KiB
random_03.txt AC 10 ms 3796 KiB
random_04.txt AC 10 ms 3824 KiB
random_05.txt AC 10 ms 3796 KiB
random_06.txt AC 10 ms 3884 KiB
random_07.txt AC 10 ms 3960 KiB
random_08.txt AC 10 ms 3864 KiB
random_09.txt AC 3 ms 3760 KiB
random_10.txt AC 4 ms 3880 KiB
random_11.txt AC 3 ms 3756 KiB
random_12.txt AC 4 ms 3744 KiB
random_13.txt AC 5 ms 3900 KiB
random_14.txt AC 6 ms 3792 KiB
random_15.txt AC 5 ms 3716 KiB
random_16.txt AC 6 ms 3808 KiB
random_17.txt AC 11 ms 3800 KiB
random_18.txt AC 10 ms 3800 KiB
random_19.txt AC 11 ms 3968 KiB
random_20.txt AC 11 ms 3828 KiB
random_21.txt AC 7 ms 3932 KiB
random_22.txt AC 3 ms 3748 KiB
random_23.txt AC 3 ms 3748 KiB
random_24.txt AC 5 ms 3856 KiB
random_25.txt AC 3 ms 3832 KiB
random_26.txt AC 9 ms 3832 KiB
random_27.txt AC 9 ms 3824 KiB
random_28.txt AC 3 ms 3848 KiB