結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー beetbeet
提出日時 2019-03-04 22:44:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 2,334 bytes
コンパイル時間 2,473 ms
コンパイル使用メモリ 213,060 KB
実行使用メモリ 23,556 KB
最終ジャッジ日時 2023-09-05 18:34:34
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 72 ms
14,608 KB
testcase_08 AC 29 ms
9,068 KB
testcase_09 AC 34 ms
10,404 KB
testcase_10 AC 26 ms
8,608 KB
testcase_11 AC 129 ms
20,968 KB
testcase_12 AC 137 ms
21,080 KB
testcase_13 AC 127 ms
20,632 KB
testcase_14 AC 131 ms
20,492 KB
testcase_15 AC 141 ms
21,712 KB
testcase_16 AC 149 ms
21,776 KB
testcase_17 AC 145 ms
22,072 KB
testcase_18 AC 95 ms
23,528 KB
testcase_19 AC 96 ms
23,556 KB
testcase_20 AC 94 ms
22,724 KB
testcase_21 AC 88 ms
21,660 KB
20evil_special_uni1.txt AC 127 ms
22,020 KB
20evil_special_uni2.txt AC 121 ms
21,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
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;}

//BEGIN CUT HERE
template<typename Data, typename T>
struct ReRooting{
  struct Node{
    int to,rev;
    Data data;
    Node(int to,int rev,Data data):to(to),rev(rev),data(data){}
  };
  
  using F1 = function<T(T,T)>;
  using F2 = function<T(T, int, int, Data)>; // from, to
  
  vector<vector<Node> > G;
  vector<vector<T> > ld,rd;
  vector<int> lp,rp;
  
  const F1 f1;
  const F2 f2;
  const T id;

  ReRooting(int n,const F1 f1,const F2 f2,const T id):
    G(n),ld(n),rd(n),lp(n),rp(n),f1(f1),f2(f2),id(id){}

  void add_edge(int u,int v,Data d){
    G[u].emplace_back(v,(int)G[v].size(),d);
    G[v].emplace_back(u,(int)G[u].size()-1,d);
  }

  T dfs(int v,int p){
    while(lp[v]!=p&&lp[v]<(int)G[v].size()){
      auto &e=G[v][lp[v]];
      ld[v][lp[v]+1]=f1(ld[v][lp[v]],f2(dfs(e.to,e.rev),v,e.to,e.data));      
      lp[v]++;
    }
    while(rp[v]!=p&&rp[v]>=0){
      auto &e=G[v][rp[v]];
      rd[v][rp[v]]=f1(rd[v][rp[v]+1],f2(dfs(e.to,e.rev),v,e.to,e.data));
      rp[v]--;
    }
    if(p<0) return rd[v][0];
    return f1(ld[v][p],rd[v][p+1]);    
  }

  vector<T> build(){
    for(int i=0;i<(int)G.size();i++){
      ld[i].assign((int)G[i].size()+1,id);
      rd[i].assign((int)G[i].size()+1,id);
      lp[i]=0;
      rp[i]=(int)G[i].size()-1;      
    }
    vector<T> res;
    for(int i=0;i<(int)G.size();i++){
      res.emplace_back(dfs(i,-1));
    }
    return res;
  }
};
//END CUT HERE

struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed YUKI_768(){
  int n;
  cin>>n;
  auto f1=[](int a,int b){return max(a,0)+max(b,0);};
  auto f2=[](int a,int from,int to,int d){return d-a;};  
  ReRooting<int, int> G(n,f1,f2,0);
  for(int i=1;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    G.add_edge(a,b,1);
  }
  auto res=G.build();
  vector<int> ans;
  for(int i=0;i<n;i++)
    if(1-res[i]>0) ans.emplace_back(i);  
  cout<<ans.size()<<"\n";
  for(int x:ans) cout<<x+1<<"\n";
  cout<<flush;
  return 0;
};
/*
  verified on 2019/03/04
  https://yukicoder.me/problems/no/768
*/
signed main(){
  YUKI_768();
  return 0;
}
0