結果

問題 No.1153 ねこちゃんゲーム
ユーザー beetbeet
提出日時 2020-08-07 21:57:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,382 bytes
コンパイル時間 3,032 ms
コンパイル使用メモリ 216,664 KB
実行使用メモリ 75,540 KB
最終ジャッジ日時 2023-10-25 00:57:02
合計ジャッジ時間 14,975 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 3 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 255 ms
39,088 KB
testcase_09 WA -
testcase_10 AC 248 ms
39,100 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 250 ms
39,084 KB
testcase_14 WA -
testcase_15 AC 259 ms
39,080 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 250 ms
39,136 KB
testcase_19 WA -
testcase_20 AC 244 ms
39,188 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 256 ms
39,100 KB
testcase_24 WA -
testcase_25 AC 252 ms
39,156 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 291 ms
75,540 KB
testcase_29 WA -
testcase_30 AC 285 ms
71,700 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 190 ms
40,408 KB
testcase_34 WA -
testcase_35 AC 193 ms
40,404 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,m;
  cin>>n>>m;
  auto as=read(m);
  for(int &a:as) a--;

  vector<vector<int>> G(n);
  for(int i=1;i<n;i++){
    int u,v;
    cin>>u>>v;
    u--;v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  using ll = long long;
  auto P = [&](int a,int b){return ((ll)(a)<<30)|b;};
  unordered_map<ll, int> dp;
  dp.reserve(n*2);

  vector<int> used(n,0);
  vector<int> skip(n,-1);

  vector<int> grnd(n,0);
  vector<vector<int>> cnts(n);

  auto func=
    MFP([&](auto dfs,int v,int p)->int{
      if(dp.count(P(v,p))) return dp[P(v,p)];
      int &res=dp[P(v,p)];
      res=0;

      auto &cnt=cnts[v];
      if(!used[v]){
        cnt.assign(G[v].size()+1,0);

        used[v]=1;
        skip[v]=p;
        for(int u:G[v]){
          if(u==p) continue;
          int k=dfs(u,v);
          if(k<(int)cnt.size()) cnt[k]++;
        }

        int &g=grnd[v];
        while(cnt[g]) g++;
        return res=g;
      }

      if(~skip[v]){
        int k=dfs(skip[v],v);
        if(k<(int)cnt.size()) cnt[k]++;
        skip[v]=-1;
      }

      int &g=grnd[v];
      while(cnt[g]) g++;

      if(~p){
        int cand=dfs(p,v);
        if(cand<g and cnt[cand]==1) return res=cand;
      }
      return res=g;
    });

  vector<int> gs(n);
  for(int i=0;i<n;i++) gs[i]=func(i,-1);

  int ans=0;
  for(int a:as) ans^=gs[a];
  if(ans==0) drop("-1 -1");

  for(int i=0;i<m;i++){
    int a=as[i];
    for(int b:G[a]){
      if(ans^gs[a]^func(b,a)) continue;
      cout<<i+1<<' '<<b+1<<endl;
      return 0;
    }
  }
  abort();
  return 0;
}
0