Problem:
L: 木の彩色
問題文
モデューロさんは木の絵を描くのがとてもうまいです。
モデューロさんは長い年月をかけて頂点が $N$ 個である木の絵を書きました。
この木の頂点には $1$ から $N$ までの番号がついており、頂点 $a_i$ と $b_i$ $(1 \leq i \leq N-1)$ は直接辺でつながれています。
すべての頂点には色がまだ塗られていません。
モデューロさんが書いたこの木の絵はたくさんの人を感動させ、有名な美術館に飾られることが決まりました。
この美術館には順に $N$ 人の人が来場します。モデューロさんは来場者特典として、それぞれの人に $1$ 枚ずつ木の絵のコピーを配布することにしました。
さらに、来場者に満足してもらうため、配布する木の絵の頂点すべてに色を塗ることにしました。
$k$ 番目 $(1 \leq k \leq N)$ に来場する人は、以下の 2 条件を共に満たす絵が配布されたときにのみ満足します。
最短距離が $k$ の倍数である任意の 2 頂点は、同じ色で塗られている。
最短距離が $k$ の倍数でない任意の 2 頂点は、異なる色で塗られている。
モデューロさんが持っている色の数は無限にあります。また、それぞれのコピーで色の塗り方が違っても構いません。
それぞれの来場者に対し、その来場者を満足させるように頂点を塗ることができるかどうか判定してください。
制約
$1 \leq N \leq 10^5$
$1 \leq a_i, b_i \leq N$
入力はすべて整数である
入力によって与えられるグラフが木であることは保証される。
入力
入力は以下の形式で標準入力から与えられる。
$N$
$a_1$ $b_1$
$a_2$ $b_2$
$\vdots$
$a_{N-1}$ $b_{N-1}$
$1$ 行目には、木の頂点数を表す整数 $N$ が与えられる。
$2$ 行目から $N$ 行目には、木の辺の情報が与えられる。このうち $i+1(1 \leq i \leq N-1)$ 行目には、頂点 $a_i$ と $b_i$ が辺でつながっていることを示す 2 つの整数 $a_i,b_i$ が与えられる。
出力
以下を満たす
0
または
1
からなる文字列 $S = s_1 s_2 \ldots s_N$ を出力せよ。
$s_k = 1$ : $k$ 番目の来場者が満足できるよう与えられた木の絵の頂点に彩色できる
$s_k = 0$ : $k$ 番目の来場者が満足できるよう与えられた木の絵の頂点に彩色できない
入力例 1
7
1 3
2 3
3 4
4 5
4 6
6 7
出力例 1
1100111
入力例1の木は以下の図のようになります。
例えば、$k=2$ の時、以下のように塗れば条件を満たします。
また、$k=5$ の時、以下のように塗れば条件を満たします。
入力例 2
6
1 2
2 3
3 4
4 5
5 6
出力例 2
111111
入力例 3
1
出力例 3
1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false); 
    cin.tie(nullptr);
    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].push_back(v); 
        g[v].push_back(u);
    }
    vector<int> parent(N,-1), q;
    q.push_back(0);
    for(int i=0;i<(int)q.size();i++){
        int cur=q[i];
        for(auto &adj: g[cur]){
            if(adj==parent[cur]) continue;
            parent[adj]=cur;
            q.push_back(adj);
        }
    }
    vector<int> len(N,0), lenDown(N,0);
    int maxInvalid=0;
    for(int i=N-1;i>=0;i--){
        int cur=q[i];
        for(auto &adj:g[cur]){
            if(adj==parent[cur]) continue;
            len[cur]=max(len[cur], len[adj]);
        }
        len[cur]++;
    }
    for(int i=0;i<N;i++){
        int cur=q[i];
        vector<int> maxLeft(g[cur].size()+1);
        maxLeft[0]=lenDown[cur];
        for(int j=0;j<(int)g[cur].size();j++){
            int adj=g[cur][j];
            maxLeft[j+1]=max(maxLeft[j], adj==parent[cur]?0:len[adj]);
        }
        int maxRight=0;
        for(int j=(int)g[cur].size()-1;j>=0;j--){
            int adj=g[cur][j];
            if(adj==parent[cur]) continue;
            lenDown[adj]=max(maxLeft[j], maxRight)+1;
            maxRight=max(maxRight, len[adj]);
        }
        if((int)g[cur].size()>=3){
            vector<int> lens; 
            lens.push_back(lenDown[cur]);
            for(auto &adj:g[cur]){
                if(adj==parent[cur]) continue;
                lens.push_back(len[adj]);
            }
            sort(lens.begin(), lens.end());
            int len0=lens.back();
            int len1=lens[lens.size()-2];
            int len2=lens[lens.size()-3];
            maxInvalid=max(maxInvalid, len0);
            if(len0==len2) maxInvalid=max(maxInvalid, len0+len2-1);
            else maxInvalid=max(maxInvalid, len0+len2);
        }
    }
    string ans(N,'1');
    for(int i=3;i<=maxInvalid && i<=N;i++){
        ans[i-1]='0';
    }
    cout<<ans<<"\n";
}
accuracy: 1
time_ms: 20
memory_kb: 11992
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=int64_t;


vector<vector<int>> g(100001);
vector<int> d(100001);
int limit=2;

void dfs(int c,int pa){
  d[c]=0;
  for(int nx:g[c]){
    if(nx==pa) continue;
    dfs(nx,c);
    d[c]=max(d[c],d[nx]);
  }
  d[c]++;
  return;
}

void dfs1(int c,int pa,int dp){
  vector<pair<int,int>> p;
  for(int nx:g[c]){
    if(nx==pa) continue;
    p.emplace_back(d[nx],nx);
  }
  p.emplace_back(dp,pa);
  sort(p.rbegin(),p.rend());
  
  if((g[c].size()>=3)&&(p[2].first>=1)){
    int d1=p[0].first;
    int d2=p[2].first;
    if(d1!=d2) limit=max(limit,d1+d2);
    else limit=max(limit,d1+d2-1);
  }
  
  for(int nx:g[c]){
    if(nx==pa) continue;
    if(nx==p[0].second) dfs1(nx,c,p[1].first+1);
    else dfs1(nx,c,p[0].first+1);
  }
  return;
}


int main(){
  int N;
  cin >> N;
  if(N==1){
    cout << 1 << endl;
    return 0;
  }
  
  int a,b;

  for(int i=0;i<N-1;i++){
    cin >> a >> b;
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  
  dfs(0,-1);
  dfs1(0,-1,0);
  
  vector<int> ans(N,1);
  for(int i=2;i<limit;i++) ans[i]=0;
  //cout << limit << endl;
  //for(int i=0;i<N;i++) cout << d[i] << endl;
  
  for(int x:ans) cout << x;
  cout << endl;

  
}
accuracy: 1
time_ms: 50
memory_kb: 22776
final_rank: 4

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

struct Info{
	Info(int arg_node,int arg_dist){
		node = arg_node;
		dist = arg_dist;
	}
	bool operator<(const struct Info &arg) const{

		return dist > arg.dist;
	}
	int node,dist;
};

int N;
int max_dist[SIZE];
int number;
vector<int> G[SIZE];
vector<Info> children[SIZE];

void dfs(int node_id,int pre){

	int tmp_max = 0;

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs(child,node_id);

		children[node_id].push_back(Info(child,max_dist[child]+1));
		tmp_max = max(tmp_max,max_dist[child]+1);
	}
	max_dist[node_id] = tmp_max;
}

void dfs2(int node_id,int pre){

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre){

			if(node_id == children[pre][0].node){ //自分が親の最大方向

				if(children[pre].size() == 1){ //親の子が自分しかいない

					children[node_id].push_back(Info(pre,1));
				}else{

					children[node_id].push_back(Info(pre,children[pre][1].dist+1));
				}

			}else{

				children[node_id].push_back(Info(pre,children[pre][0].dist+1));
			}
			break;
		}
	}

	int value;

	if(children[node_id].size() > 2){

		sort(children[node_id].begin(),children[node_id].end());

		if(children[node_id][0].dist == children[node_id][2].dist){

			value = children[node_id][0].dist + children[node_id][2].dist;
		}else{

			value = children[node_id][0].dist+children[node_id][2].dist+1;
		}

		//printf("node_id:%d\n",node_id);
		/*for(int i = 0; i < 3; i++){

			printf("child[%d]:%d dist:%d\n",i,children[node_id][i].node,children[node_id][i].dist);
		}*/

		number = max(number,value);
	}

	//親を先に処理
	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs2(child,node_id);
	}
}

int main(){

	scanf("%d",&N);

	int from,to;
	for(int i = 0; i < N-1; i++){

		scanf("%d %d",&from,&to);
		from--;
		to--;
		G[from].push_back(to);
		G[to].push_back(from);
	}

	if(N <= 2){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	for(int i = 0; i < N; i++){

		max_dist[i] = 0;
	}

	dfs(0,-1);

	number = -1;
	dfs2(0,-1);

	if(number <= 2){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	printf("11");
	for(int i = 3; i <= number-1; i++){

		printf("0");
	}
	for(int i = number; i <= N; i++){

		printf("1");
	}
	printf("\n");

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 25424
final_rank: 9

code_snippet: // verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3142

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

#define call_from_test

#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
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;}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif


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

//BEGIN CUT HERE
template<typename T, typename Edge>
struct ReRooting{
  struct Node{
    int to,rev;
    Edge data;
    Node(int to,Edge data):to(to),data(data){}
    bool operator<(const Node &v)const{return to<v.to;};
  };

  using Fold = function<T(T, T)>;
  using Lift = function<T(T, Edge)>;

  vector< vector<Node> > G;
  vector< vector<T> > ld,rd;
  vector<int> lp,rp;

  const Fold fold;
  const Lift lift;
  const T id;

  ReRooting(int n,const Fold fold,const Lift lift,const T id):
    G(n),ld(n),rd(n),lp(n),rp(n),fold(fold),lift(lift),id(id){}

  void add_edge(int u,int v,Edge d,Edge e){
    G[u].emplace_back(v,d);
    G[v].emplace_back(u,e);
  }

  void add_edge(int u,int v,Edge d){add_edge(u,v,d,d);}

  // k: idx for edge (not vertex)
  T dfs(int v,int k){
    while(lp[v]!=k and lp[v]<(int)G[v].size()){
      auto &e=G[v][lp[v]];
      ld[v][lp[v]+1]=fold(ld[v][lp[v]],lift(dfs(e.to,e.rev),e.data));
      lp[v]++;
    }
    while(rp[v]!=k and rp[v]>=0){
      auto &e=G[v][rp[v]];
      rd[v][rp[v]]=fold(rd[v][rp[v]+1],lift(dfs(e.to,e.rev),e.data));
      rp[v]--;
    }
    if(k<0) return rd[v][0];
    return fold(ld[v][k],rd[v][k+1]);
  }

  int search(vector<Node> &vs,int idx){
    return lower_bound(vs.begin(),vs.end(),Node(idx,vs[0].data))-vs.begin();
  }

  vector<T> build(){
    int n=G.size();
    for(int i=0;i<n;i++){
      sort(G[i].begin(),G[i].end());
      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;
    }

    for(int i=0;i<n;i++)
      for(Node &t:G[i])
        t.rev=search(G[t.to],i);

    vector<T> res;
    for(int i=0;i<n;i++)
      res.emplace_back(dfs(i,-1));

    return res;
  }

  // p: idx for vertex
  T subtree(int v,int p){
    int k=search(G[p],v);
    assert(k<(int)G[p].size() and G[p][k].to==v);
    return lift(dfs(v,G[p][k].rev),G[p][k].data);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  using ll = long long;

  struct T{
    ll a,b,c;
    T(ll a,ll b,ll c):a(a),b(b),c(c){}
  };

  const ll INF = 1e9;
  auto fold=[&](T x,T y){
    vector<ll> vs({x.a,x.b,x.c,y.a,y.b,y.c});
    sort(vs.rbegin(),vs.rend());
    return T(vs[0],vs[1],vs[2]);
  };
  auto lift=[&](T x,ll y){
    chmax(x.a,0);
    x.a+=y;
    x.b=-INF;
    x.c=-INF;
    return x;
  };

  int n;
  cin>>n;
  if(n==1) drop(1);

  ReRooting<T, ll> G(n,fold,lift,T(-INF,-INF,-INF));
  for(int i=1;i<n;i++){
    int u,v;
    cin>>u>>v;
    u--;v--;
    G.add_edge(u,v,1);
  }
  auto res=G.build();

  string ans(n+1,'1');

  for(int i=0;i<n;i++){
    if(G.G[i].size()<3) continue;
    T v=res[i];
    ans[v.a+min({v.a-1,v.b,v.c})]='0';
  }

  for(int i=n-1;i>=0;i--)
    if(ans[i+1]=='0') ans[i]='0';

  ans[1]='1';
  ans[2]='1';
  cout<<ans.substr(1)<<endl;
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 88340
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
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;}
using Int = long long;
const char newl = '\n';

template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}


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, Data)>;

  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);
  }

  // p: idx for edge (not vertex)
  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),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),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;
  }
};

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

  struct T{
    Int a,b,c;
    T(Int a,Int b,Int c):a(a),b(b),c(c){}
  };

  const Int INF = 1e9;
  auto f1=
    [&](T x,T y){
      vector<Int> vs({x.a,x.b,x.c,y.a,y.b,y.c});
      sort(vs.rbegin(),vs.rend());
      return T(vs[0],vs[1],vs[2]);
    };
  auto f2=
    [&](T x,Int y){
      chmax(x.a,0);
      x.a+=y;
      x.b=-INF;
      x.c=-INF;
      return x;
    };

  Int n;
  cin>>n;
  if(n==1) drop(1);

  ReRooting<Int, T> G(n,f1,f2,T(-INF,-INF,-INF));
  for(Int i=1;i<n;i++){
    Int u,v;
    cin>>u>>v;
    u--;v--;
    G.add_edge(u,v,1);
  }
  auto res=G.build();

  string ans(n+1,'1');

  for(Int i=0;i<n;i++){
    if(G.G[i].size()<3) continue;
    T v=res[i];
    assert(v.a>=v.b);
    assert(v.b>=v.c);
    assert(v.c>=1);
    // cout<<v.a<<" "<<v.b<<" "<<v.c<<endl;
    ans[v.a+min({v.a-1,v.b,v.c})]='0';
  }

  for(Int i=n-1;i>=0;i--)
    if(ans[i+1]=='0') ans[i]='0';

  ans[1]='1';
  ans[2]='1';
  cout<<ans.substr(1)<<endl;
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 90512
final_rank: 16

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

struct Info{
	Info(int arg_node,int arg_dist){
		node = arg_node;
		dist = arg_dist;
	}
	bool operator<(const struct Info &arg) const{

		return dist > arg.dist;
	}
	int node,dist;
};

int N;
int max_dist[SIZE];
int number;
vector<int> G[SIZE];
vector<Info> children[SIZE];

void dfs(int node_id,int pre){

	int tmp_max = 0;

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs(child,node_id);

		children[node_id].push_back(Info(child,max_dist[child]+1));
		tmp_max = max(tmp_max,max_dist[child]+1);
	}
	max_dist[node_id] = tmp_max;
}

void dfs2(int node_id,int pre){

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre){

			if(node_id == children[pre][0].node){ //自分が親の最大方向

				if(children[pre].size() == 1){ //親の子が自分しかいない

					children[node_id].push_back(Info(pre,1));
				}else{

					children[node_id].push_back(Info(pre,children[pre][1].dist+1));
				}

			}else{

				children[node_id].push_back(Info(pre,children[pre][0].dist+1));
			}
			break;
		}
	}

	int value;

	if(children[node_id].size() > 2){

		sort(children[node_id].begin(),children[node_id].end());

		if(children[node_id][0].dist == children[node_id][2].dist){

			value = children[node_id][0].dist + children[node_id][2].dist;
		}else{

			value = children[node_id][0].dist+children[node_id][2].dist+1;
		}

		//printf("node_id:%d\n",node_id);
		/*for(int i = 0; i < 3; i++){

			printf("child[%d]:%d dist:%d\n",i,children[node_id][i].node,children[node_id][i].dist);
		}*/

		number = max(number,value);
	}

	//親を先に処理
	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs2(child,node_id);
	}
}

int main(){

	scanf("%d",&N);

	int from,to;
	for(int i = 0; i < N-1; i++){

		scanf("%d %d",&from,&to);
		from--;
		to--;
		G[from].push_back(to);
		G[to].push_back(from);
	}

	if(N <= 2){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	for(int i = 0; i < N; i++){

		max_dist[i] = 0;
	}

	dfs(0,-1);

	number = -1;
	dfs2(0,-1);

	if(number <= 2){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	printf("11");
	for(int i = 3; i <= number-1; i++){

		printf("0");
	}
	for(int i = number; i <= N; i++){

		printf("1");
	}
	printf("\n");

	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 25440
final_rank: 6

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

struct Info{
	Info(int arg_node,int arg_dist){
		node = arg_node;
		dist = arg_dist;
	}
	bool operator<(const struct Info &arg) const{

		return dist > arg.dist;
	}
	int node,dist;
};

int N;
int max_dist[SIZE];
int number;
vector<int> G[SIZE];
vector<Info> children[SIZE];

void dfs(int node_id,int pre){

	int tmp_max = 0;

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs(child,node_id);

		children[node_id].push_back(Info(child,max_dist[child]+1));
		tmp_max = max(tmp_max,max_dist[child]+1);
	}
	max_dist[node_id] = tmp_max;
}

void dfs2(int node_id,int pre){

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre){

			if(node_id == children[pre][0].node){ //自分が親の最大方向

				if(children[pre].size() == 1){ //親の子が自分しかいない

					children[node_id].push_back(Info(pre,1));
				}else{

					children[node_id].push_back(Info(pre,children[pre][1].dist+1));
				}

			}else{

				children[node_id].push_back(Info(pre,children[pre][0].dist+1));
			}
			break;
		}
	}

	int value;

	if(children[node_id].size() > 2){

		sort(children[node_id].begin(),children[node_id].end());

		if(children[node_id][0].dist == children[node_id][2].dist){

			value = children[node_id][0].dist + children[node_id][2].dist;
		}else{

			value = children[node_id][0].dist+children[node_id][2].dist+1;
		}

		//printf("node_id:%d\n",node_id);
		/*for(int i = 0; i < 3; i++){

			printf("child[%d]:%d dist:%d\n",i,children[node_id][i].node,children[node_id][i].dist);
		}*/

		number = max(number,value);
	}

	//親を先に処理
	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs2(child,node_id);
	}
}

int main(){

	scanf("%d",&N);

	int from,to;
	for(int i = 0; i < N-1; i++){

		scanf("%d %d",&from,&to);
		from--;
		to--;
		G[from].push_back(to);
		G[to].push_back(from);
	}

	if(N <= 2){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	for(int i = 0; i < N; i++){

		max_dist[i] = 0;
	}

	dfs(0,-1);

	number = -1;
	dfs2(0,-1);

	if(number == -1){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	printf("11");
	for(int i = 3; i <= number-1; i++){

		printf("0");
	}
	for(int i = number; i <= N; i++){

		printf("1");
	}
	printf("\n");

	return 0;
}
accuracy: 0.8026315789473685
time_ms: 70
memory_kb: 25496
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

struct Info{
	Info(int arg_node,int arg_dist){
		node = arg_node;
		dist = arg_dist;
	}
	bool operator<(const struct Info &arg) const{

		return dist > arg.dist;
	}
	int node,dist;
};

int N;
int max_dist[SIZE];
int number;
vector<int> G[SIZE];
vector<Info> children[SIZE];

void dfs(int node_id,int pre){

	int tmp_max = 0;

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs(child,node_id);

		children[node_id].push_back(Info(child,max_dist[child]+1));
		tmp_max = max(tmp_max,max_dist[child]+1);
	}
	max_dist[node_id] = tmp_max;
}

void dfs2(int node_id,int pre){

	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre){

			if(node_id == children[pre][0].node){ //自分が親の最大方向

				if(children[pre].size() == 1){ //親の子が自分しかいない

					children[node_id].push_back(Info(pre,1));
				}else{

					children[node_id].push_back(Info(pre,children[pre][1].dist+1));
				}

			}else{

				children[node_id].push_back(Info(pre,children[pre][0].dist+1));
			}
			break;
		}
	}

	int value;

	if(children[node_id].size() > 2){

		sort(children[node_id].begin(),children[node_id].end());

		if(children[node_id][0].dist == children[node_id][2].dist){

			value = children[node_id][0].dist + children[node_id][2].dist;
		}else{

			value = children[node_id][0].dist+children[node_id][2].dist+1;
		}

		//printf("node_id:%d\n",node_id);
		/*for(int i = 0; i < 3; i++){

			printf("child[%d]:%d dist:%d\n",i,children[node_id][i].node,children[node_id][i].dist);
		}*/

		number = max(number,value);
	}

	//親を先に処理
	for(int i = 0; i < G[node_id].size(); i++){

		int child = G[node_id][i];
		if(child == pre)continue;

		dfs2(child,node_id);
	}
}

int main(){

	scanf("%d",&N);

	int from,to;
	for(int i = 0; i < N-1; i++){

		scanf("%d %d",&from,&to);
		from--;
		to--;
		G[from].push_back(to);
		G[to].push_back(from);
	}

	if(N <= 2){

		for(int i = 0; i < N; i++){

			printf("1");
		}
		printf("\n");
		return 0;
	}

	for(int i = 0; i < N; i++){

		max_dist[i] = 0;
	}

	dfs(0,-1);

	number = -1;
	dfs2(0,-1);

	//printf("number:%d\n",number);

	printf("11");
	for(int i = 3; i <= number-1; i++){

		printf("0");
	}
	for(int i = number; i <= N; i++){

		printf("1");
	}
	printf("\n");

	return 0;
}
accuracy: 0.19736842105263158
time_ms: 30
memory_kb: 25452
final_rank: 20

code_snippet: #include <iostream>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <climits>
#include <numeric>
#include <queue>
#include <cmath>
#include <iomanip>
#include <array>
#include <string>
#include <stack>

int main() {
	int n; std::cin >> n;
	std::vector<std::vector<int>> nodes(n);
	for (auto i = 1; i < n; ++i) {
		int a, b; std::cin >> a >> b;
		nodes[a - 1].push_back(b - 1);
		nodes[b - 1].push_back(a - 1);
	}
	std::stack<int> stack; stack.push(0);
	std::vector<int> depth(n, -1); depth[0] = 1;
	while (!stack.empty()) {
		const auto top = stack.top(); stack.pop();
		for (const auto next : nodes[top]) if (depth[next] == -1) {
			depth[next] = depth[top] + 1;
			stack.push(next);
		}
	}
	const auto end_a = std::distance(depth.begin(), std::max_element(depth.begin(), depth.end()));
	stack.push(end_a);
	depth.assign(n, -1); depth[end_a] = 1;
	std::vector<int> height(n, -1);
	while (!stack.empty()) {
		const auto top = stack.top(); stack.pop();
		if (top >= 0) {
			stack.push(-1 - top);
			for (const auto next : nodes[top]) if (depth[next] == -1) {
				depth[next] = depth[top] + 1;
				stack.push(next);
			}
		}
		else {
			const auto current = -1 - top;
			height[current] = 1;
			for (const auto next : nodes[current]) {
				height[current] = std::max(height[current], height[next] + 1);
			}
		}
	}
	int max{ 2 };
	for (auto i = 0; i < n; ++i) if (nodes[i].size() >= 3) {
		std::vector<int> length; length.reserve(nodes[i].size() + 1);
		std::transform(nodes[i].begin(), nodes[i].end(), std::back_inserter(length), [&depth, &height, i](const int a) {return depth[i] < depth[a] ? height[a] : depth[a]; });
		std::sort(length.rbegin(), length.rend());
		const auto k = length[0] + length[2];
		if (length[0] == length[2]) {
			max = std::max(max, k);
		}
		else {
			max = std::max(max, k + 1);
		}
	}
	for (auto i = 1; i <= n; ++i) {
		if (3 <= i && i < max) {
			std::cout << '0';
		}
		else {
			std::cout << '1';
		}
	}
	std::cout << std::endl;
}
accuracy: 1
time_ms: 60
memory_kb: 10112
final_rank: 2

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#define llint long long
#define inf 1e18
#define mod 998244353
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)

using namespace std;
typedef pair<llint, llint> P;

int n;
vector<llint> G[100005];
map<P, llint> mp;
int dif[100005];

int parent[500005];
llint dp[500005], dp2[500005];

const llint e = 0; //
llint ope(llint a, llint b)
{
	return max(a, b); //
}

llint get(int u, int v)
{
	if(parent[u] == v) return dp2[u];
	else return dp[v];
}

void dfs(int v, int p)
{
	parent[v] = p;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		dfs(G[v][i], v);
	}
	
	llint sum = e;
	for(int i = 0; i < G[v].size(); i++){
		int u = G[v][i];
		if(u == p) continue;
		sum = ope(sum, get(v, u)); //
	}
	dp[v] = sum+1; //
}

llint lsum[500005], rsum[500005];
void dfs2(int v, int p)
{
	llint m = G[v].size(), sum;
	
	sum = lsum[0] = e;
	for(int i = 0; i < m; i++){
		int u = G[v][i];
		sum = ope(sum, get(v, u)); //
		lsum[i+1] = sum;
	}
	sum = rsum[m+1] = e;
	for(int i = m-1; i >= 0; i--){
		int u = G[v][i];
		sum = ope(sum, get(v, u)); //
		rsum[i+1] = sum;
	}
	for(int i = 0; i < m; i++){
		if(G[v][i] == p) continue;
		dp2[G[v][i]] = ope(lsum[i], rsum[i+2])+1; //
	}
	
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == p) continue;
		dfs2(G[v][i], v);
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	int u, v;
	for(int i = 0; i < n-1; i++){
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	
	dfs(1, -1);
	dfs2(1, -1);
	
	/*for(auto it = mp.begin(); it != mp.end(); it++){
		cout << it->first.first << " " << it->first.second << " " << it->second << endl;
	}*/
	
	vector<llint> vec;
	for(int i = 1; i <= n; i++){
		if(G[i].size() < 3) continue;
		vec.clear();
		for(int j = 0; j < G[i].size(); j++){
			vec.push_back(get(i, G[i][j]));
		}
		sort(vec.rbegin(), vec.rend());
		llint x = vec[0], y = vec[2], l, r;
		//cout << i << " " << x << " " << y << endl;
		if(y == 0 || x+y < 3) continue;
		
		if(x > y) l = 3, r = x+y;
		else l = 3, r = 2*x-1;
		dif[l]++, dif[r+1]--;
	}
	
	llint sum = 0;
	for(int i = 1; i <= n; i++){
		sum += dif[i];
		if(sum) cout << 0;
		else cout << 1;
	}
	cout << endl;

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 16868
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=101000;
int n,u,v;
int dis[N],dis2[N];
VI e[N],br[N];

void dfs(int u,int f) {
	dis[u]=0;
	for (auto v:e[u]) {
		if (v==f) continue;
		dfs(v,u);
		dis[u]=max(dis[u],dis[v]+1);
		br[u].pb(dis[v]+1);
	}
}

void dfs2(int u,int f) {
	int mx=0,mx2=0;
	if (u!=1) br[u].pb(dis2[u]);
	mx=dis2[u];
	//printf("uu %d %d\n",u,dis2[u]);
	for (auto x:br[u]) {
		if (x>mx) mx2=mx,mx=x;
		else if (x>mx2) mx2=x;
	}
	for (auto v:e[u]) {
		if (v==f) continue;
		if (dis[v]+1==mx) dis2[v]=mx2+1;
		else dis2[v]=mx+1;
		dfs2(v,u);
	}
}
int main() {
	scanf("%d",&n);
	rep(i,1,n) {
		scanf("%d%d",&u,&v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	dfs2(1,0);
	/*rep(i,1,n+1) {
		for (auto x:br[i]) printf("%d ",x);
		puts("");
	}*/
	int t=0;
	rep(i,1,n+1) if (SZ(br[i])>=3) {
		sort(all(br[i]));
		reverse(all(br[i]));
		t=max(t,br[i][0]+br[i][2]-(br[i][0]==br[i][2]));
	}
	rep(i,1,n+1) putchar((i<=2||i>t)?'1':'0');
	puts("");
}
accuracy: 1
time_ms: 70
memory_kb: 24372
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r) //[l, r]
{
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
    return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
    cout<<x;
    if(suc==1) cout<<'\n';
    else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
    for(int i=0;i<v.size();i++)
    print(v[i],i==(int)(v.size())-1?suc:2);
}
const int maxn=1e5+7;
vi G[maxn];
int dp[maxn];
int len;
void dfs(int u,int fa=-1)
{
    dp[u]=1;
    for(auto v:G[u])if(v!=fa)
    {
        dfs(v,u);
        dp[u]=max(dp[u],dp[v]+1);
    }
}
void dfs2(int u,int fa=-1)
{
    multiset<int> son;
    for(auto v:G[u])
        son.insert(dp[v]);
    if(son.size()>2)
    {
        //dbg(u,son);
        auto it=son.rbegin();
        vi all;
        for(int i=0;i<3;i++)
        {
            all.push_back(*it);
            it++;
        }
        if(all[0]!=all[2])
            len=max(len,all[0]+all[2]+1);
        else len=max(len,all[0]+all[2]);
    }
    for(auto v:G[u])if(v!=fa)
    {
        son.erase(son.find(dp[v]));
        int tmp=dp[u];
        if(son.empty()) dp[u]=1;
        else dp[u]=(*son.rbegin())+1;
        int tmpv=dp[v];
        dp[v]=max(dp[v],dp[u]+1);
        dfs2(v,u);
        dp[u]=tmp;
        dp[v]=tmpv;
        son.insert(dp[v]);
    }
}
int main()
{
    int n;
    cin>>n;
    len=2;
    string ans;
    for(int i=0;i<n;i++) ans+='1';
    bool ac=1;
    for(int i=1,u,v;i<n;i++)
    {
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1);
    dfs2(1);
    for(int i=2;i<len-1;i++) ans[i]='0';
    cout<<ans<<'\n';
}
accuracy: 1
time_ms: 90
memory_kb: 30672
final_rank: 13

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

vector<vector<int>> g;
vector<int> dp;
vector<vector<int>> len;

void predfs(int now,int par){
    int res=0;
    for(auto nex:g[now]) if(nex!=par){
        predfs(nex,now);
        chmax(res,dp[nex]+1);
        len[now].push_back(dp[nex]+1);
    }
    dp[now]=res;
}

void dfs(int now,int par,int pval){
    if(par!=-1) len[now].push_back(pval);
    sort(len[now].rbegin(),len[now].rend());

    for(auto nex:g[now]) if(nex!=par){
        int neco=len[now][0];
        if(neco==dp[nex]+1){
            if(len[now].size()>1) neco=len[now][1];
            else neco=0;
        }
        dfs(nex,now,neco+1);
    }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  g.resize(N);
  rep(i,N-1){
      int a,b;
      cin>>a>>b;
      a--;b--;
      g[a].push_back(b);
      g[b].push_back(a);
  }

  dp.resize(N,0);
  len.resize(N);
  predfs(0,-1);

  dfs(0,-1,-1);

  int lim=0;
  for(int i=0;i<N;i++){
      if(len[i].size()<=2) continue;
      int fir=len[i][0];
      int sec=len[i][2];
      if(fir==sec) sec--;
      chmax(lim,fir+sec);
  }

  string ans(N,'1');
  rep(i,lim) ans[i]='0';
  ans[0]='1';ans[1]='1';
  cout<<ans<<endl;


  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 25352
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> G[1<<17];
vector<int> dp(1<<17,0);
int r = 3;

int dfs(int u,int v){
    int ret = 0;
    for(auto g:G[u]){
        if(g == v) continue;
        chmax(ret, dfs(g, u));
    }
    return dp[u] = ret + 1;
}

void dfs2(int u,int v,P x){
    vector<P> vv;
    vv.pb(mp(0, -1));
    for(auto g:G[u]){
        if(g == v){
            vv.pb(x);
        }
        else{
            vv.pb(mp(dp[g], g));
        }
    }
    sort(RALL(vv));
    for(auto g:G[u]){
        if(g == v) continue;
        if(g == vv[0].sc){
            dfs2(g, u, mp(vv[1].fs + 1, u));
        }
        else{
            dfs2(g, u, mp(vv[0].fs + 1, u));
        }
    }
    if(vv.size() >= 4){
        int a = vv[0].fs, b = vv[1].fs, c = vv[2].fs;
        chmax(r, a + c + (a != c));
    }
}


int main(){
    int n;cin >> n;
    for (int i = 0; i < n-1; i++) {
        int a,b;cin >> a >> b;
        a--,b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0,-1);
    dfs2(0,-1,mp(0,-1));
    string ans(n, '1');
    for (int i = 3; i < r; i++) {
        ans[i - 1] = '0';
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 28140
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> G[1<<17];
vector<int> dp(1<<17);

int dfs(int u,int v){
    int ret = 0;
    for(auto g:G[u]){
        if(g == v) continue;
        ret = max(ret, dfs(g, u));
    }
    return dp[u] = ret + 1;
}

int n;
int l = 3;

void dfs2(int u, int v, P x){
    vector<P> vv;
    for(auto g:G[u]){
        if(g == v){
            vv.pb(x);
        }
        else{
            vv.pb(mp(dp[g],g));
        }
    }
    sort(RALL(vv));
    for(auto g:G[u]){
        if(g == v) continue;
        if(vv[0].sc == g){
            if(vv.size() == 1) dfs2(g, u, mp(1, u));
            else dfs2(g, u, mp(vv[1].fs + 1, u));
        }
        else{
            dfs2(g, u, mp(vv[0].fs + 1, u));
        }
    }
    if(vv.size() >= 3){
        int a = vv[0].fs, b = vv[1].fs, c = vv[2].fs;
        for (int k = l; k < n; k++) {
            if(k > a + c) break;
            else if(a + c == k && k - b == k - c && (k - c) * 2 == k) break;
            l++;
        }
    }
    return;
}



int main(){
    cin >> n;
    if(n == 1){
        cout << 1 << endl;
        return 0;
    }
    for (int i = 0; i < n-1; i++) {
        int a,b;cin >> a >> b;
        a--,b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0,0);
    dfs2(0,0,mp(0,0));
    for (int i = 1; i <= 2; i++) {
        cout << 1;
    }
    for (int i = 3; i < l; i++) {
        cout << 0;
    }
    for (int i = l; i <= n; i++) {
        cout << 1;
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 28220
final_rank: 12

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

long long n;
vector<vector<int>> edge;
vector<int> dp;
vector<bool> ch;

string solve();
int dfs(int now);
int reroot(int now);

int main() {
  cin >> n;
  edge.resize(n);
  for(int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    edge[--a].push_back(--b);
    edge[b].push_back(a);
  }
  cout << solve() << endl;
  return 0;
}

string solve() {
  string res;
  for(int i = 0; i < n; ++i) res += "1";
  dp.assign(n, 0);
  ch.assign(n, 0);
  ch[0] = 1;
  dfs(0);
  ch.assign(n, 0);
  ch[0] = 1;
  int num = reroot(0);
  if(num != -1)
    for(int j = 2; j <= num; ++j) res[j] = '0';
  return res;
}

int dfs(int now) {
  int res = 0;
  for(auto to : edge[now])
    if(!ch[to]) {
      ch[to] = 1;
      res = max(res, 1 + dfs(to));
    }
  return dp[now] = res;
}

int reroot(int now) {
  int res = -1, num = 0, bf = dp[now];
  dp[now] = 0;
  vector<int> child, pre, suf;
  for(auto to : edge[now]) {
    child.push_back(dp[to]);
    pre.push_back(dp[to]);
    suf.push_back(dp[to]);
  }
  num = child.size();
  for(int i = 1; i < num; ++i) {
    pre[i] = max(pre[i - 1], pre[i]);
    suf[num - i - 1] = max(suf[num - i], suf[num - i - 1]);
  }
  for(int i = 0; i < num; ++i)
    if(!ch[edge[now][i]]) {
      dp[now] = 0;
      ch[edge[now][i]] = 1;
      if(i != 0) dp[now] = max(dp[now], pre[i - 1] + 1);
      if(i != num - 1)
        dp[now] = max(dp[now], suf[i + 1] + 1);
      res = max(res, reroot(edge[now][i]));
    }
  if(num >= 3) {
    sort(child.begin(), child.end(), greater<int>());
    if(child[0] == child[2])
      res = max(res, child[0] + child[2]);
    else
      res = max(res, child[0] + child[2] + 1);
  }
  dp[now] = bf;
  return res;
}
accuracy: 1
time_ms: 100
memory_kb: 35216
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> G[1<<17];
vector<int> dp(1<<17);

int dfs(int u,int v){
    int ret = 0;
    for(auto g:G[u]){
        if(g == v) continue;
        chmax(ret, dfs(g, u));
    }
    return dp[u] = ret + 1;
}

int n;
int l = 3;

void dfs2(int u, int v, P x){
    vector<P> vv;
    for(auto g:G[u]){
        if(g == v){
            vv.pb(x);
        }
        else{
            vv.pb(mp(dp[g], g));
        }
    }
    sort(RALL(vv));
    for(auto g:G[u]){
        if(g == v) continue;
        if(vv[0].sc == g){
            if(vv.size() == 1) dfs2(g, u, mp(1, u));
            else dfs2(g, u, mp(vv[1].fs + 1, u));
        }
        else{
            dfs2(g, u, mp(vv[0].fs + 1, u));
        }
    }
    if(vv.size() >= 3){
        int a = vv[0].fs, b = vv[1].fs, c = vv[2].fs;
        if(a == c) chmax(l, a + c);
        else chmax(l, a + c + 1);
    }
    return;
}



int main(){
    cin >> n;
    if(n == 1){
        cout << 1 << endl;
        return 0;
    }
    for (int i = 0; i < n-1; i++) {
        int a,b;cin >> a >> b;
        a--,b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0,0);
    dfs2(0,0,mp(0,0));
    for (int i = 1; i <= 2; i++) {
        cout << 1;
    }
    for (int i = 3; i < l; i++) {
        cout << 0;
    }
    for (int i = l; i <= n; i++) {
        cout << 1;
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 28080
final_rank: 10

code_snippet: #include <iostream>
#include <vector>

using namespace std;
vector<int> G[100010];
int d[3][100010] = {};
int dfs(int s, int p,int k){
    for(int v:G[s]){
        if(v==p) continue;
        d[k][v] = d[k][s] + 1;
        dfs(v,s,k);
    }
}

int mx[100010] = {};
void dfs2(int s,int p,int dia,int par){
    for(int v:G[s]){
        if(v==p) continue;
        if(d[0][v] + d[1][v]==dia) continue;
        d[2][v] = d[2][s] + 1;
        mx[par] = max(mx[par],d[2][v]);
        dfs2(v,s,dia,par);
    }
}

int n,seg[2][200010];
void init(int n,int x){
    for(int i=n - 1;i>0;i--){
        seg[x][i] = max(seg[x][i<<1],seg[x][i<<1|1]);
    }
}

void update(int val,int p,int x){
    for(seg[x][p += n] = val;p>1;p>>=1){
        seg[x][p>>1] = max(seg[x][p],seg[x][p^1]);
    }
}

int query(int l,int r,int x){
    int res = 0;
    for(l += n,r += n; l<r;l>>=1,r>>=1){
        if(l&1) res = max(res,seg[x][l++]);
        if(r&1) res = max(res,seg[x][--r]);
    }
    return res;
}

int ans[100010];
int main(){
    int i;
    cin >> n;
    for(i=0;i<n - 1;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b); G[b].push_back(a);
    }
    dfs(0,-1,0);
    int j = -1,x = -1;
    for(i=0;i<n;i++){
        if(d[0][i]>x){
            j = i; x = d[0][i];
        }
    }
    for(i=0;i<n;i++){
        d[0][i] = 0;
    }
    dfs(j,-1,0);
    int jj = -1; x = -1;
    for(i=0;i<n;i++){
        if(d[0][i]>x){
            x = d[0][i];
            jj = i;
        }
    }
    int dia = x;
    dfs(jj,-1,1);
    vector<int> choku;
    ans[1] = 1; ans[2] = 1;
    for(i=dia + 1;i<=n;i++){
        ans[i] = 1;
    }
    for(i=0;i<n;i++){
        if(d[0][i] + d[1][i]==dia){
            dfs2(i,-1,dia,i);
            choku.push_back(i);
        }
    }
    for(i=0;i<choku.size();i++){
        int v = choku[i];
        if(mx[v]) update(mx[v] + d[0][v],d[0][v],0);
        if(mx[v]) update(mx[v] + d[1][v],d[1][v],1);
    }
    for(i=3;i<=dia;i++){
        if(i&1){
            if(query(0,n,0)>=i || query(0,n,1)>=i) ans[i] = 0;
            else ans[i] = 1;
        }else{
            int k = i/2;
            if(max(query(0,n,0),query(0,n,1))<i){
                ans[i] = 1;
            }else if(max(query(0,k,0),query(k + 1,n,0))>=i || max(query(0,k,1),query(k + 1,n,1))>=i){
                ans[i] = 0;
            }else if(max(query(k,k + 1,0),query(k,k + 1,1))==i){
                ans[i] = 1;
            }else{
                ans[i] = 0;
            }
        }
    }
    for(i=1;i<=n;i++){
        cout << ans[i];
    }
    cout << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 18384
final_rank: 8

code_snippet: #include <iostream>
#include <vector>

using namespace std;
vector<int> G[100010];
int d[3][100010] = {};
int dfs(int s, int p,int k){
    for(int v:G[s]){
        if(v==p) continue;
        d[k][v] = d[k][s] + 1;
        dfs(v,s,k);
    }
}

int mx[100010] = {};
void dfs2(int s,int p,int dia,int par){
    for(int v:G[s]){
        if(v==p) continue;
        if(d[0][v] + d[1][v]==dia) continue;
        d[2][v] = d[2][s] + 1;
        mx[par] = max(mx[par],d[2][v]);
        dfs2(v,s,dia,par);
    }
}

int n,seg[2][200010];
void init(int n,int x){
    for(int i=n - 1;i>0;i--){
        seg[x][i] = max(seg[x][i<<1],seg[x][i<<1|1]);
    }
}

void update(int val,int p,int x){
    for(seg[x][p += n] = val;p>1;p>>=1){
        seg[x][p>>1] = max(seg[x][p],seg[x][p^1]);
    }
}

int query(int l,int r,int x){
    int res = 0;
    for(l += n,r += n; l<r;l>>=1,r>>=1){
        if(l&1) res = max(res,seg[x][l++]);
        if(r&1) res = max(res,seg[x][--r]);
    }
    return res;
}

int ans[100010];
int main(){
    int i;
    cin >> n;
    for(i=0;i<n - 1;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b); G[b].push_back(a);
    }
    dfs(0,-1,0);
    int j = -1,x = -1;
    for(i=0;i<n;i++){
        if(d[0][i]>x){
            j = i; x = d[0][i];
        }
    }
    for(i=0;i<n;i++){
        d[0][i] = 0;
    }
    dfs(j,-1,0);
    int jj = -1; x = -1;
    for(i=0;i<n;i++){
        if(d[0][i]>x){
            x = d[0][i];
            jj = i;
        }
    }
    int dia = x;
    dfs(jj,-1,1);
    vector<int> choku;
    ans[1] = 1; ans[2] = 1;
    for(i=dia + 1;i<=n;i++){
        ans[i] = 1;
    }
    for(i=0;i<n;i++){
        if(d[0][i] + d[1][i]==dia){
            dfs2(i,-1,dia,i);
            choku.push_back(i);
        }
    }
    for(i=0;i<choku.size();i++){
        int v = choku[i];
        if(mx[v]) update(mx[v] + d[0][v],d[0][v],0);
        if(mx[v]) update(mx[v] + d[1][v],d[1][v],1);
    }
    for(i=3;i<=dia;i++){
        if(i&1){
            if(query(0,n,0)>=i || query(0,n,1)>=i) ans[i] = 0;
            else ans[i] = 1;
        }else{
            int k = i/2;
            if(max(query(0,n,0),query(0,n,1))<i){
                ans[i] = 1;
            }else if(max(query(0,k,0),query(k + 1,n,0))>=i || max(query(0,k,1),query(k + 1,n,1))>=i){
                ans[i] = 0;
            }else if(max(query(k,k + 1,0),query(k,k + 1,1))==k){
                ans[i] = 1;
            }else{
                ans[i] = 0;
            }
        }
    }
    for(i=1;i<=n;i++){
        cout << ans[i];
    }
    cout << endl;
}
accuracy: 0.75
time_ms: 90
memory_kb: 18384
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
vector<vector<ll> > v(100001);

ll ans=0,num=0,niko,kotori;
ll umi=0,mid;
bool hanayo;
void dfs(ll now,ll count,ll p){
	if(count>=ans){
		num=now;
		ans=count;
	}
	for(auto& i:v[now]){
		if(i==p)continue;
		dfs(i,count+1,now);
	}
	return;
}
bool eri=true;
void solve(ll now,ll count,ll p){
	if(v[now].size()==1){
		if(count!=niko){
			eri=false;
		}
	}
	for(auto& i:v[now]){
		if(i==p)continue;
		solve(i,count+1,now);
	}
	return;
}
void honoka(ll now,ll count,ll p){
	if(v[now].size()==1){
		if(count>=mid)umi++;
	}else if(v[now].size()>=3){
		if(count>=mid)hanayo=false;
	}
	for(auto& i:v[now]){
		if(i==p)continue;
		honoka(i,count+1,now);
	}
	return;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    ll a,b;
    for(int i=0;i<n-1;i++){
    	cin>>a>>b;
    	a--;b--;
    	v[a].pb(b);
    	v[b].pb(a);
    }
    ll m3=0;
    for(int i=0;i<n;i++){
    	if(3<=v[i].size()){m3++;a=i;}
    }
    if(m3==0){
    	for(int i=0;i<n;i++){
    		cout << 1;
    	}
    	cout <<endl;
    	return 0;
    }
    dfs(0,0,-1);
    kotori=num;
    dfs(num,0,-1);
    
    bool maki=true;
    if(m3>=2)maki=false;
    else{
    	niko=ans/2;
        solve(a,0,-1);
        maki=eri;
    }
    ll le=0,ri=ans+1;
    while(ri-le>1){
    	mid=(le+ri)/2;
    	umi=0;
    	hanayo=true;
    	honoka(num,0,-1);
        honoka(kotori,0,-1);
        if(umi==2&&hanayo){
        	ri=mid;
        }else{
        	le=mid;
        }
    }
    

    for(int i=1;i<=n;i++){
    	if(i>ans)cout <<1;
    	else if(i==1||i==2)cout << 1;
    	else if(i>=ri)cout << 1;
    	else if(i&1)cout<<0;
    	else if(i==ans&&maki){
    		cout << 1;
    	}else{
    		cout << 0;
    	}
    }
    cout<< endl;
	return 0;
}
accuracy: 0.75
time_ms: 260
memory_kb: 13176
final_rank: 19