#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n;
    cin>>n;
    stack<char>st;
    string s;
    cin>>s;
    long long int count=0;
    int i=0;
    while(s[i]!='\0')
    {
        char c=s[i];
        //cin>>c;
        if(st.empty())
        {
            st.push(c);
        }
        else
        {
            char ch=st.top();
            if(ch==c)
            {
                st.push(c);
            }
            else
            {
                if(ch=='(' && c==')')
                {
                    st.pop();
                }
                else
                {
                    count++;
                    st.pop();
                    st.push(c);
                }
            }
        }
        i++;
    }
    if(n%2==1)
        cout<<"No";
    else
    {
        if(s.empty()&&count==0)
                cout<<"Yes";
        if(!st.empty() && (count==1))
            cout<<"Yes";
        else
            cout<<"No";
    }

}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n;
    cin>>n;
    stack<char>st;
    string s;
    cin>>s;
    long long int count=0;
    int i=0;
    while(s[i]!='\0')
    {
        char c=s[i];
        //cin>>c;
        if(st.empty())
        {
            st.push(c);
        }
        else
        {
            char ch=st.top();
            if(ch==c)
            {
                st.push(c);
            }
            else
            {
                if(ch=='(' && c==')')
                {
                    st.pop();
                }
                else
                {
                    count++;
                    st.pop();
                    st.push(c);
                }
            }
        }
        i++;
    }
    if(n%2==1)
        cout<<"No";
    else
    {
        if(s.empty()&&count==0)
                cout<<"Yes";
        else if(!st.empty() && (count==1))
            cout<<"Yes";
        else
            cout<<"No";
    }

}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n;
    cin>>n;
    stack<char>st;
    string s;
    cin>>s;
    long long int count=0;
    int i=0;
    while(s[i]!='\0')
    {
        char c=s[i];
        //cin>>c;
        if(st.empty())
        {
            st.push(c);
        }
        else
        {
            char ch=st.top();
            if(ch==c)
            {
                st.push(c);
            }
            else
            {
                if(ch=='(' && c==')')
                {
                    st.pop();
                }
                else
                {
                    count++;
                    st.pop();
                    st.push(c);
                }
            }
        }
        i++;
    }
    if(n%2==1)
        cout<<"No";
    else
    {
        if(!st.empty() && (count==0 || count==1))
            cout<<"Yes";
        else
            cout<<"No";
    }

}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n;
    cin>>n;
    stack<char>st;
    string s;
    cin>>s;
    long long int count=0;
    int i=0;
    while(s[i]!='\0')
    {
        char c=s[i];
        //cin>>c;
        if(st.empty())
        {
            st.push(c);
        }
        else
        {
            char ch=st.top();
            if(ch==c)
            {
                st.push(ch);
            }
            else
            {
                if(ch=='(' && c==')')
                {
                    st.pop();
                }
                else
                {
                    count++;
                    st.pop();
                }
            }
        }
        i++;
    }
    if(n%2==1)
        cout<<"No";
    else
    {
        if(st.empty() && (count==0 || count==1))
            cout<<"Yes";
        else
            cout<<"No";
    }

}


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        string str;
        cin>>n;
        cin>>str;

	int first = 0;
	for (size_t i=0; i<n; i++){
		first += str[i] == '(';
	}

        if(first != n-first)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int chk =0;
            for(int i=0;i<n;i++)
            {
                if(str[i]=='(')
                    chk++;
                else
                    chk--;

                if(chk < -1)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            if(chk==2 || chk == 0)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
        return 0;
    }


#include <iostream>
#include <stack>

using namespace std;

#define endl '\n'

int main(){
    int n; cin >> n;
    string expr;
    int first = 0;

    for(size_t i=0; i<n; i++){
        char a; cin >> a;
        if (a == '(') first++;

        expr += a;
    }

    if(first == n-first){
        stack<char> stk;
        stack<char> stk2;

        for(size_t i=0; i<n; i++){
           if (expr[i] == '(') stk.push('(');
           else{
               if (! stk.empty()) stk.pop();
               else stk2.push(')');
           }
        }

        if (stk.size() < 2 and stk2.size() < 2)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    else
        cout << "No" << endl;
}


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        string str;
        cin>>n;
        cin>>str;

	int first = 0;
	for (size_t i=0; i<n; i++){
		first += str[i] == '(';
	}

        if(first != n-first)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int chk =0;
            for(int i=0;i<n;i++)
            {
                if(str[i]=='(')
                    chk++;
                else
                    chk--;

                if(chk < -2)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            if(chk==2 || chk == 0 chk == -2)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
        return 0;
    }


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        string str;
        cin>>n;
        cin>>str;
        if(n%2==1)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int chk =0;
            for(int i=0;i<n;i++)
            {
                if(str[i]=='(')
                    chk++;
                else
                    chk--;

                if(chk < -1)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            if(chk==2 || chk == 0 || chk == -2)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
        return 0;
    }


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        string str;
        cin>>n;
        cin>>str;
        if(n%2==1)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int chk =0;
            for(int i=0;i<n;i++)
            {
                if(str[i]=='(')
                    chk++;
                else
                    chk--;

                if(chk < -2)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            if(chk==2 || chk == 0)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
        return 0;
    }


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        string str;
        cin>>n;
        cin>>str;

	int first = 0;
	for (size_t i=0; i<n; i++){
		first += str[i] == '(';
	}

        if(first != n-first)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int chk =0;
            for(int i=0;i<n;i++)
            {
                if(str[i]=='(')
                    chk++;
                else
                    chk--;

                if(chk < -1)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            if(chk==2 || chk == 0 || chk == -2)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
        return 0;
    }


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        string str;
        cin>>n;
        cin>>str;

	int first = 0;
	for (size_t i=0; i<n; i++){
		first += str[i] == '(';
	}

        if(first != n-first)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int chk =0;
            for(int i=0;i<n;i++)
            {
                if(str[i]=='(')
                    chk++;
                else
                    chk--;

                if(chk < -2)
                {
                    cout<<"No"<<endl;
                    return 0;
                }
            }
            if(chk==2 || chk == 0 || chk == -2)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
        }
        return 0;
    }


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

vector<vector<pair<int, int>>> graph;
int i_v = -1, i_prev = -1;
#define MAXN (int)(3e5+42)
bool visited[MAXN];

void dfs(int v, int prev = -1, int edge_id = -1) {
  if (visited[v])
    return;
  visited[v] = true;
  if (graph[v].size() == 1)
    if (i_v == -1 || graph[prev].size() > graph[i_prev].size())
      i_v = v, i_prev = prev;
  for (auto p : graph[v])
    dfs(p.first, v, p.second);
}

int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  graph.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back({b, i});
    graph[b].push_back({a, i});
  }
  if (n == 2) {
    cout << "0\n";
    return 0;
  }
  int start = 0;
  for (int i = 0; i < n; i++)
    if (graph[i].size() > graph[start].size())
      start = i;
  dfs(start);
  set<int> left;
  for (int i = 0; i < n - 1; i++)
    left.insert(i);
  vector<int> ans(n - 1, -1);
  for (auto p : graph[i_prev]) {
    if (ans[p.second] != -1 || graph[p.first].size() != 1)
      continue;
    auto d = left.begin();
    ans[p.second] = *d;
    left.erase(d);
  }
  for (int i = 0; i < n - 1; i++) {
    if (ans[i] != -1)
      continue;
    auto d = prev(left.end());
    ans[i] = *d;
    left.erase(d);
  }
  for (auto p : ans)
    cout << p << '\n';

  return 0;
}


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

typedef long long ll;

vector<vector<pair<int, int>>> graph;
vector<int> pv;
int last = -1, start = 0;
bool visited[(int)2e5];
#define f first
#define s second

void dfs(int v, int par = -1) {
  if (visited[v] || last != -1)
    return;
  visited[v] = true;
  pv[v] = par;
  if (graph[v].size() == 1 && v != start)
    last = v;
  for (auto p : graph[v])
    dfs(p.first, v);
}

int get_eid(int a, int b) {
  for (auto p : graph[a])
    if (p.first == b)
      return p.second;
  assert(false);
  return -1;
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  graph.resize(n);
  pv.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back({b, i});
    graph[b].push_back({a, i});
  }
  if (n == 2) {
    cout << "0\n";
    return 0;
  }
  while (graph[start].size() != 1)
    ++start;
  dfs(start);

  vector<int> ans(n - 1, -1);
  vector<int> path = { last };
  while (last != start) {
    last = pv[last];
    path.push_back(last);
  }
  for (auto d : path)
    cout << d << ' ';
  cout << endl;
  int sz = path.size();
  ans[get_eid(path[sz - 2], path[sz - 1])] = 1;
  ans[get_eid(path[0], path[1])] = 0;

  vector<int> avail;
  for (int i = 2; i < n - 1; i++)
    avail.push_back(i);
  for (int i = 1; i < sz - 2; i++) {
    auto p = avail.back();
    avail.pop_back();
    ans[get_eid(path[i], path[i + 1])] = p;
  }
  for (int i = 0; i < n - 1; i++) {
    if (ans[i] != -1)
      continue;
    auto p = avail.back();
    avail.pop_back();
    ans[i] = p;
  }
  for (auto p : ans)
    cout << p << '\n';
  return 0;
}


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

typedef long long ll;
#define MAXN (int)(3e5+42)
vector<vector<int>> graph;
map<pair<int, int>, int> eid;
int last = -1, start = 0, pv[MAXN], visited[MAXN], ans[MAXN];

void dfs(int v, int par = -1) {
  if (visited[v] || last != -1)
    return;
  visited[v] = true;
  pv[v] = par;
  if (graph[v].size() == 1 && v != start)
    last = v;
  for (auto p : graph[v])
    dfs(p, v);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  graph.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
    eid[{a, b}] = eid[{b, a}] = i;
  }
  if (n == 2) {
    cout << "0\n";
    return 0;
  }
  while (graph[start].size() != 1)
    ++start;
  dfs(start);
  vector<int> path = { last };
  while (last != start) {
    last = pv[last];
    path.push_back(last);
  }
  int sz = path.size();
  vector<int> avail(n - 1);
  for (int i = 0; i < n - 1; i++)
    avail[i] = i, ans[i] = -1;
  swap(avail[0], avail[n - 1]);
  swap(avail[1], avail[n - sz + 1]);
  for (int i = 1; i < sz; i++) {
    auto p = avail.back();
    avail.pop_back();
    ans[eid[{path[i - 1], path[i]}]] = p;
  }
  for (int i = 0; i < n - 1; i++) {
    if (ans[i] != -1)
      continue;
    auto p = avail.back();
    avail.pop_back();
    ans[i] = p;
  }
  for (auto p : ans)
    cout << p << '\n';
  return 0;
}


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

vector<vector<pair<int, int>>> graph;
int i_v = -1, i_prev = -1;
#define MAXN (int)(3e5+42)
bool visited[MAXN];

void dfs(int v, int prev = -1, int edge_id = -1) {
  if (visited[v])
    return;
  visited[v] = true;
  bool all_leaves = true;
  for (auto p : graph[v]) {
    dfs(p.first, v, p.second);
    all_leaves = all_leaves && (p.first == prev || graph[p.first].size() == 1);
  }
  if (all_leaves && (i_prev == -1 || graph[v].size() > graph[i_prev].size()))
    i_prev = v;
}

int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  graph.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back({b, i});
    graph[b].push_back({a, i});
  }
  if (n == 2) {
    cout << "0\n";
    return 0;
  }
  int start = 0;
  for (int i = 0; i < n; i++)
    if (graph[i].size() > graph[start].size())
      start = i;
  dfs(start);
  set<int> left;
  for (int i = 0; i < n - 1; i++)
    left.insert(i);
  vector<int> ans(n - 1, -1);
  for (auto p : graph[i_prev]) {
    if (ans[p.second] != -1 /*|| graph[p.first].size() != 1*/)
      continue;
    auto d = left.begin();
    ans[p.second] = *d;
    left.erase(d);
  }
  for (int i = 0; i < n - 1; i++) {
    if (ans[i] != -1)
      continue;
    auto d = prev(left.end());
    ans[i] = *d;
    left.erase(d);
  }
  //cout << i_prev << " sss" << endl;
  for (auto p : ans)
    cout << p << '\n';

  return 0;
}


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

typedef long long ll;

vector<vector<pair<int, int>>> graph;
vector<ll> subtree_sz;
bool visited[(int)2e5];
#define f first
#define s second

void dfs(int v, int eid = -1) {
  if (visited[v])
    return;
  visited[v] = true;
  ll sz = 1;
  for (auto p : graph[v]) {
    dfs(p.first, p.second);
    sz += subtree_sz[p.first];
  }
  subtree_sz[v] = sz;
  //cout << v << ": " << subtree_sz[v] << '\n';
}

int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  graph.resize(n);

  vector<pair<int, int>> edges(n-1), path_cnt(n-1);
  // edge[i] <-> {a,b}
  // path_cnt[i] <-> {#paths through edge i, i}
  subtree_sz.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back({b, i});
    graph[b].push_back({a, i});
    edges[i] = {a, b};
  }
  dfs(0);
  for (int i = 0; i < n - 1; i++) {
    auto e = edges[i];

    ll d = subtree_sz[e.s] < subtree_sz[e.f] ? e.s : e.f;
    ll pcnt = (n - d) * d;
    path_cnt[i] = { pcnt, i };
  }
  sort(path_cnt.begin(), path_cnt.end());

  /*for (auto p : path_cnt)
    cout << p.first << ": " << p.second << endl;*/
  vector<int> ans(n-1);
  for (int i = 0; i < n - 1; i++)
    ans[path_cnt[i].second] = i;
  for (auto p : ans)
    cout << p << '\n';
  return 0;
}


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

typedef long long ll;
#define MAXN (int)(1e5+42)
vector<vector<pair<int, int>>> graph;
int last = -1, start = 0, pv[MAXN], visited[MAXN], ans[MAXN];

void dfs(int v, int par = -1) {
  if (visited[v] || last != -1)
    return;
  visited[v] = true;
  pv[v] = par;
  if (graph[v].size() == 1 && v != start)
    last = v;
  for (auto p : graph[v])
    dfs(p.first, v);
}

int eid(int a, int b) {
  for (auto p : graph[a])
    if (p.first == b)
      return p.second;
  assert(false);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  graph.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back({b, i});
    graph[b].push_back({a, i});
  }
  if (n == 2) {
    cout << "0\n";
    return 0;
  }
  while (graph[start].size() != 1)
    ++start;
  dfs(start);
  vector<int> path = { last };
  while (last != start) {
    last = pv[last];
    path.push_back(last);
  }
  int sz = path.size();
  vector<int> avail(n - 1);
  for (int i = 0; i < n - 1; i++)
    avail[i] = i, ans[i] = -1;
  swap(avail[0], avail[n - 2]);
  swap(avail[1], avail[n - sz]);
  for (int i = 1; i < sz; i++) {
    auto p = avail.back();
    avail.pop_back();
    ans[eid(path[i - 1], path[i])] = p;
  }
  for (int i = 0; i < n - 1; i++) {
    if (ans[i] != -1)
      continue;
    auto p = avail.back();
    avail.pop_back();
    ans[i] = p;
  }
  for (int i = 0; i < n - 1; i++)
    cout << ans[i] << '\n';
  return 0;
}


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx,indx2;
	edge(){}
	edge(int _u,int _v,int _indx,int _indx2)
	{
		u = _u, v = _v, indx = _indx,indx2 = _indx2;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1,
		a.indx2 = grap[v].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void lenear(int sc,int n)
{
	for(int i = 1;i <= n; i++)
	{
		if(grap[ i ].size() == 1)
			cst[ i ][0] = c++;
	}

}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	lenear(1,n);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx],cs2 = cst[v][moves[i].indx2];

		if(cs !=-1 && cs2 !=-1 ){
			cs = min(cs,cs2);
			cs2 = cs;
		}
		if(cs != -1)
			cout << cs << endl;
		else if(cs2 != -1)
			cout << cs2 << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx,indx2;
	edge(){}
	edge(int _u,int _v,int _indx,int _indx2)
	{
		u = _u, v = _v, indx = _indx,indx2 = _indx2;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1,
		a.indx2 = grap[v].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void lenear(int sc,int n)
{
	if(sc > n) return;
		if(grap[ sc ].size() == 1)
			cst[sc][0] = c++;
	lenear(sc+1,n);
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	lenear(1,n);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx],cs2 = cst[v][moves[i].indx2];

		if(cs !=-1 && cs2 !=-1 ){
			cs = min(cs,cs2);
			cs2 = cs;
		}
		if(cs != -1)
			cout << cs << endl;
		else if(cs2 != -1)
			cout << cs2 << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx,indx2;
	edge(){}
	edge(int _u,int _v,int _indx,int _indx2)
	{
		u = _u, v = _v, indx = _indx,indx2 = _indx2;
	}
};

std::vector< int > grap[100009],cst[100009];
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1), cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1,
		a.indx2 = grap[v].size() - 1;
		moves.pb(a);
	}
}

int c = 0;

void lenear(int sc,int n)
{
	for(int i = 1;i <= n; i++)
	{
		if(grap[ i ].size() == 1)
			cst[ i ][0] = c++;
	}
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
//FastIO;

	int n;
	cin >> n;
	input(n);
	lenear(1,n);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx],cs2 = cst[v][moves[i].indx2];

		if(cs !=-1 && cs2 !=-1 ){
			cs = min(cs,cs2);
			cs2 = cs;
		}
		if(cs != -1)
			cout << cs << endl;
		else if(cs2 != -1)
			cout << cs2 << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx;
	edge(){}
	edge(int _u,int _v,int _indx)
	{
		u = _u, v = _v, indx = _indx;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v);//, grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void dfs(int sc)
{
	if(visit[sc]) return;
	visit[sc] = 1;
	for(int i = 0;i < grap[sc].size();i++)
	{
		if(grap[ grap[sc][i] ].size() == 0){
			cst[sc][i] = c++;
		//	cout << sc << " " << grap[sc][i] << " "  << c-1 << endl;
		}
		else dfs( grap[sc][i] );

	}
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	dfs(msc);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx];

		if(cs != -1)
		{
			cout << cs << endl;
		}
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx;
	edge(){}
	edge(int _u,int _v,int _indx)
	{
		u = _u, v = _v, indx = _indx;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void dfs(int sc)
{
	if(visit[sc]) return;
	visit[sc] = 1;
	for(int i = 0;i < grap[sc].size();i++)
	{
		if(grap[ grap[sc][i] ].size() == 1){
			cst[sc][i] = c++;
			//cout << sc << " " << grap[sc][i] << " "  << c << endl;
		}
		else dfs( grap[sc][i] );

	}
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	dfs(msc);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx];

		if(cs != -1)
			cout << cs << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx;
	edge(){}
	edge(int _u,int _v,int _indx)
	{
		u = _u, v = _v, indx = _indx;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void dfs(int sc)
{
	if(visit[sc]) return;
	visit[sc] = 1;
	for(int i = 0;i < grap[sc].size();i++)
	{
		if(grap[ grap[sc][i] ].size() == 0){
			cst[sc][i] = c++;
		}
		else dfs( grap[sc][i] );

	}
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	dfs(msc);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx];

		if(cs != -1)
			cout << cs << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx,indx2;
	edge(){}
	edge(int _u,int _v,int _indx,int _indx2)
	{
		u = _u, v = _v, indx = _indx,indx2 = _indx2;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1,
		a.indx2 = grap[v].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void dfs(int sc,int n)
{
	if(sc > n) return;
	if(visit[sc]) return;
		if(grap[ sc ].size() == 1)
			cst[sc][0] = c++;
	dfs(sc+1,n);
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	dfs(1,n);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx],cs2 = cst[v][moves[i].indx2];

		if(cs != -1)
			cout << cs << endl;
		else if(cs2 != -1)
			cout << cs2 << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


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

//____________________________________________________________________________________________________________________________________

#define PI  2*acos(0.0)
#define pf  printf
#define sf  scan
#define ff  first
#define ss  second
#define mp  make_pair
#define pb  push_back
typedef long long ll;
typedef unsigned long long ull;
typedef std::vector<int> vi;
typedef vector<long long> vll ;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define sc(n)       sf("%d",&n);
#define scf(f)      sf("%f",&f);
#define FOR(i,a,n)  for(int i = a; i < n; i++)
#define TEST(test)  for(int cse = 1; cse <= test; cse++)

#define all(x) (x).begin(), (x).end()
#define fap(x) cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const ll INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
//____________________________________________________________________________________________________________________________________

struct edge{
	int u,v,indx,indx2;
	edge(){}
	edge(int _u,int _v,int _indx,int _indx2)
	{
		u = _u, v = _v, indx = _indx,indx2 = _indx2;
	}
};
std::vector< int > grap[100009],cst[100009];
bool visit[100009];
int msc = 1;
std::vector < edge > moves;
void input(int n)
{
	int u,v;
	for(int i = 1; i < n; i++)
	{
		cin >> u >> v;
		grap[u].pb(v), grap[v].pb(u);
		cst[u].pb(-1);
		cst[v].pb(-1);
		edge a;
		a.u = u,a.v = v, a.indx = grap[u].size() - 1,
		a.indx2 = grap[v].size() - 1;
		moves.pb(a);
		msc = u;
	}
}

int c = 0;

void dfs(int sc)
{
	if(visit[sc]) return;
	visit[sc] = 1;
	for(int i = 0;i < grap[sc].size();i++)
	{
		if(grap[ grap[sc][i] ].size() == 1){
			cst[sc][i] = c++;
			//cout << sc << " " << grap[sc][i] << " "  << c-1 << endl;
		}
		else dfs( grap[sc][i] );

	}
}

int main()
{
#ifdef PARTHO
    freopen("/home/partho/Desktop/IO/input.txt","r",stdin);
    freopen("/home/partho/Desktop/IO/output.txt","w",stdout);
    int start_time = clock();
#endif
FastIO;

	int n;
	cin >> n;
	input(n);
	dfs(msc);

	for(int i = 0; i < moves.size(); i++){
		int u = moves[i].u, v = moves[i].v, cs = cst[u][moves[i].indx],cs2 = cst[v][moves[i].indx2];

		if(cs != -1)
			cout << cs << endl;
		else if(cs2 != -1)
			cout << cs2 << endl;
		else cout << c++ << endl;
	}

#ifdef PARTHO
    int end_time = clock();
    printf("Time = %.4f\n",(end_time-start_time+0.0)/CLOCKS_PER_SEC);
#endif

    return 0;
}
///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

int val = 1;
int ans[MAXN + 1];
int dist[MAXN + 1];
int cnt = 0;

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = val++;
        cnt++;
    } else if (adj[v].size() == 1) {
        ans[v] = val++;
        cnt++;
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1, 1);

    for (int i = 2; i <= n; i++) {
        if (!ans[i]) {
            ans[i] = val++;
        }
    }

    for (auto p : edge) {
        if (cnt == 2) {
            if (dist[p.first] < dist[p.second]) {
                cout << n - ans[p.second] - 1 << '\n';
            } else {
                cout << n - ans[p.first] - 1 << '\n';
            }
        } else {
            if (dist[p.first] < dist[p.second]) {
                cout << ans[p.second] - 1 << '\n';
            } else {
                cout << ans[p.first] - 1 << '\n';
            }
        }
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

int val = 1;
int ans[MAXN + 1];
int dist[MAXN + 1];

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = val++;
    } else if (adj[v].size() == 1) {
        ans[v] = val++;
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }

    if (!ans[v]) {
        ans[v] = val++;
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1, 1);

    for (auto p : edge) {
        if (dist[p.first] < dist[p.second]) {
            cout << ans[p.second] - 1 << '\n';
        } else {
            cout << ans[p.first] - 1 << '\n';
        }
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

int val = 1;
int ans[MAXN + 1];
int dist[MAXN + 1];
int cnt = 0;

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = val++;
        cnt++;
    } else if (adj[v].size() == 1) {
        ans[v] = val++;
        cnt++;
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1, 1);

    for (int i = 2; i <= n; i++) {
        if (!ans[i]) {
            ans[i] = val++;
        }
    }

    for (auto p : edge) {
        if (cnt == 2) {
            if (dist[p.first] < dist[p.second]) {
                cout << n - ans[p.second] << '\n';
            } else {
                cout << n - ans[p.first] << '\n';
            }
        } else {
            if (dist[p.first] < dist[p.second]) {
                cout << ans[p.second] - 1 << '\n';
            } else {
                cout << ans[p.first] - 1 << '\n';
            }
        }
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

set<int> nums;
int ans[MAXN + 1];
int dist[MAXN + 1];

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = *nums.begin();
        nums.erase(nums.begin());
    } else if (adj[v].size() == 1) {
        ans[v] = *nums.begin();
        nums.erase(nums.begin());
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for (int i = 0; i < n - 1; i++) {
        nums.insert(i + 1);
    }

    dfs(1, 1);

    for (int i = 2; i <= n; i++) {
        if (!ans[i]) {
            ans[i] = *nums.begin();
            nums.erase(nums.begin());
        }
    }

    for (auto p : edge) {
        if (dist[p.first] < dist[p.second]) {
            cout << ans[p.second] - 1 << '\n';
        } else {
            cout << ans[p.first] - 1 << '\n';
        }
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

int val = 1;
int ans[MAXN + 1];
int dist[MAXN + 1];

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = val++;
    } else if (adj[v].size() == 1) {
        ans[v] = val++;
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1, 1);

    for (int i = 2; i <= n; i++) {
        if (!ans[i]) {
            ans[i] = val++;
        }
    }

    for (auto p : edge) {
        if (dist[p.first] < dist[p.second]) {
            cout << ans[p.second] - 1 << '\n';
        } else {
            cout << ans[p.first] - 1 << '\n';
        }
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

int val = 1;
int ans[MAXN + 1];
int dist[MAXN + 1];

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = val++;
    } else if (adj[v].size() == 1) {
        ans[v] = val++;
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;

    if (n == 2) {
        cout << "0\n";
        return 0;
    }

    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1, 1);

    for (int i = 2; i <= n; i++) {
        if (!ans[i]) {
            ans[i] = val++;
        }
    }

    for (auto p : edge) {
        if (dist[p.first] < dist[p.second]) {
            cout << ans[p.second] - 1 << '\n';
        } else {
            cout << ans[p.first] - 1 << '\n';
        }
    }

    return 0;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 100000

using namespace std;

vector<pair<int, int>> edge;
vector<int> adj[MAXN + 1];

int val = 1;
int ans[MAXN + 1];
int dist[MAXN + 1];
int cnt = 0;

void dfs(int v, int p) {
    if (v == 1 && adj[v].size() == 1) {
        ans[adj[v][0]] = val++;
        cnt++;
    } else if (adj[v].size() == 1) {
        ans[v] = val++;
        cnt++;
    }

    if (v > 1) {
        dist[v] = dist[p] + 1;
    }

    for (int e : adj[v]) {
        if (e != p) {
            dfs(e, v);
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b; cin >> a >> b;
        edge.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(1, 1);

    for (int i = 2; i <= n; i++) {
        if (!ans[i]) {
            ans[i] = val++;
        }
    }

    for (auto p : edge) {
        if (cnt == 2) {
            if (dist[p.first] < dist[p.second]) {
                cout << n - ans[p.second] + 1 << '\n';
            } else {
                cout << n - ans[p.first] + 1 << '\n';
            }
        }

        if (dist[p.first] < dist[p.second]) {
            cout << ans[p.second] - 1 << '\n';
        } else {
            cout << ans[p.first] - 1 << '\n';
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

// put smallest values at the edges connected to leaves

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n);
    v1p e(n-1)
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v; --u; --v;
        a[u].pb(v);
        a[v].pb(u);
        e[i] = {u, v};
    }

    int cnt = 0;
    v1d ans(n, -1ll);
    for(int i=0; i<n-1; ++i) {
        int p = e[i].ff;
        int q = e[i].ss;
        if(a[p].size() == 1 || a[q].size() == 1) {ans[i] = cnt; cnt++;}
    }

    for(int i=0; i<n; ++i) {
        if(ans[i] == -1) {
            cout << cnt << endl; cnt++;
        }
        else cout << ans[i];
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

// put smallest values at the edges connected to leaves

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n);
    v1p e(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v; --u; --v;
        a[u].pb(v);
        a[v].pb(u);
        e[i] = {u, v};
    }

    int cnt = 0;
    v1d ans(n-1, -1ll);
    for(int i=0; i<n-1; ++i) {
        int p = e[i].ff;
        int q = e[i].ss;
        if(a[p].size() == 1 || a[q].size() == 1) {ans[i] = cnt; cnt++;}
    }

    for(int i=0; i<n-1; ++i) {
        if(ans[i] == -1) {
            cout << cnt << endl; cnt++;
        }
        else cout << ans[i];
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

// put smallest values at the edges connected to leaves

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n);
    v1p e(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v; --u; --v;
        a[u].pb(v);
        a[v].pb(u);
        e[i] = {u, v};
    }

    int cnt = 0;
    v1d ans(n, -1ll);
    for(int i=0; i<n-1; ++i) {
        int p = e[i].ff;
        int q = e[i].ss;
        if(a[p].size() == 1 || a[q].size() == 1) {ans[i] = cnt; cnt++;}
    }

    for(int i=0; i<n; ++i) {
        if(ans[i] == -1) {
            cout << cnt << endl; cnt++;
        }
        else cout << ans[i];
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n);
    v1p x(n);
    v1p l(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v; u--; v--;
        a[u].pb(v);
        a[v].pb(u);
        l[i] = {u, v};
    }
    for(int i=0; i<n; ++i) {
        x[i].ff = a[i].size(); x[i].ss = i;
    }
    RS(x);
    int cnt = n-2;
    map<pr, int> m;
    for(int i=0; i<n; ++i) {
        int y = x[i].ss;
        for(auto &k : a[y]) {
            if(m.count({y, k}) || m.count({k, y})) continue;
            m[{y, k}] = cnt;
            cnt--;
        }
    }
    for(int i=0; i<n-1; ++i) {
        int p = l[i].ff;
        int q = l[i].ss;
        if(m.count({p, q})) cout << m[{p, q}] << endl;
        else cout << m[{q, p}] << endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

// put smallest values at the edges connected to leaves

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n);
    v1p e(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v; --u; --v;
        a[u].pb(v);
        a[v].pb(u);
        e[i] = {u, v};
    }

    int cnt = 0;
    v1d ans(n-1, -1ll);
    for(int i=0; i<n-1; ++i) {
        int p = e[i].ff;
        int q = e[i].ss;
        if(a[p].size() == 1 || a[q].size() == 1) {ans[i] = cnt; cnt++;}
    }

    for(int i=0; i<n-1; ++i) {
        if(ans[i] == -1) {
            cout << cnt << endl; cnt++;
        }
        else cout << ans[i] << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

void dfs(int i, int d, v2d &a, v1d &dis, int par = 0) {
    dis[i] = d;
    for(auto &j : a[i]) {
        if(j != par) {
            dfs(j, d+1, a, dis, i);
        }
    }
}

void dfs1(int i, int d, v2d &a, v1d &dis, v1d &p, int par = 0) {
    dis[i] = d;
    p[i] = par;
    for(auto &j : a[i]) {
        if(j != par) {
            dfs1(j, d+1, a, dis, p, i);
        }
    }
}

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n+1);
    v1d dis(n+1);
    v1d p(n+1);
    set<pr> k;
    v1p l(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v;
        a[u].pb(v);
        a[v].pb(u);
        k.insert({u, v});
        l[i] = mp(u, v);
    }
    dfs(1, 0, a, dis);
    int mx = -1;
    int x;
    for(int i = 2; i<=n; ++i) {
        if(mx < dis[i]) {
            x = i; mx = dis[i];
        }
    }
    fill(all(dis), 0);
    dfs1(x, 0, a, dis, p);
    mx = -1;
    int to;
    for(int i=1; i<=n ;++i ) {
        if(mx < dis[i]) {
            to = i; mx = dis[i];
        }
    }
    map<pr, int> m;
    int cnt = 0;
    while(to != x) {
        int x = to;
        int y = p[to];
        if(k.count({x, y}))
            m[{x, y}] = cnt;
        else
            m[{y, x}] = cnt;
        cnt++;
        to = p[to];
    }
    for(int i=0; i<n-1; ++i) {
        if(m.count(l[i])) cout << m[l[i]] << endl;
        else {
            cout << cnt << endl; cnt++;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

void dfs(int i, int d, v2d &a, v1d &dis, int par = 0) {
    dis[i] = d;
    for(auto &j : a[i]) {
        if(j != par) {
            dfs(j, d+1, a, dis, i);
        }
    }
}

void dfs1(int i, int d, v2d &a, v1d &dis, v1d &p, int par = 0) {
    dis[i] = d;
    p[i] = par;
    for(auto &j : a[i]) {
        if(j != par) {
            dfs1(j, d+1, a, dis, p, i);
        }
    }
}

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n+1);
    v1d dis(n+1);
    v1d p(n+1);
    set<pr> k;
    v1p l(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v;
        a[u].pb(v);
        a[v].pb(u);
        k.insert({u, v});
        l[i] = mp(u, v);
    }
    dfs(1, 0, a, dis);
    int mx = -1;
    int x;
    for(int i = 2; i<=n; ++i) {
        if(mx < dis[i]) {
            x = i; mx = dis[i];
        }
    }
    fill(all(dis), 0);
    dfs1(x, 0, a, dis, p);
    mx = -1;
    int to;
    for(int i=1; i<=n ;++i ) {
        if(mx < dis[i]) {
            to = i; mx = dis[i];
        }
    }
    map<pr, int> m;
    int cnt = n-2;
    while(to != x) {
        int x = to;
        int y = p[to];
        if(k.count({x, y}))
            m[{x, y}] = cnt;
        else
            m[{y, x}] = cnt;
        cnt--;
        to = p[to];
    }
    for(int i=0; i<n-1; ++i) {
        if(m.count(l[i])) cout << m[l[i]] << endl;
        else {
            cout << cnt << endl; cnt--;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define v1d vector<int>
#define mp make_pair
#define pb push_back
#define v1p vector<pr>
#define vb vector<bool>
#define v2d vector<vector<int>>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define eb emplace_back
#define endl "\n"
#define MOD 1000000007
#define pqx priority_queue <int>
#define pqn priority_queue <int, v1d, greater<int> >
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int32_t main() {
    fio
    int n; cin >> n;
    v2d a(n);
    v1p x(n);
    v1p l(n-1);
    for(int i=0; i<n-1; ++i) {
        int u, v; cin >> u >> v; u--; v--;
        a[u].pb(v);
        a[v].pb(u);
        l[i] = {u, v};
    }
    for(int i=0; i<n; ++i) {
        x[i].ff = a[i].size(); x[i].ss = i;
    }
    RS(x);
    int cnt = n-2;
    map<pr, int> m;
    for(int i=0; i<n; ++i) {
        int y = x[i].ss;
        v1p j;
        for(auto &k : a[y]) {
            j.pb({a[k].size(), k});
        }
        RS(j);
        for(auto &t : j) {
            int k = t.ss;
            if(m.count({y, k}) || m.count({k, y})) continue;
            m[{y, k}] = cnt;
            cnt--;
        }

    }
    for(int i=0; i<n-1; ++i) {
        int p = l[i].ff;
        int q = l[i].ss;
        if(m.count({p, q})) cout << m[{p, q}] << endl;
        else cout << m[{q, p}] << endl;
    }

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;

const int MOD = 1000000007 ; //   1000000009 , 1000000023 ,998244353
const ll INF = ll(1e18);
const ld PI = 4*atan((ld)1);

using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a , ll b){
  if(a==0)return b;
  return gcd(b%a,a);
}

int main() {

#ifdef AVI
  freopen("input.txt" , "r" , stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<vector<ll>>g(n+1 , vector<ll>());
  vector<pll>ar;
  for(ll i=0 ; i<n-1 ; i++){
    ll x,y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    ar.push_back({x,y});
  }
  stack<ll>st;
  map<pll,ll>mp;
  for(ll i=n ; i>=1 ; i--)st.push(i);
    //cout << st.top();
  for(ll x=1 ; x<=n ; x++){
    if(g[x].size()>=3){
      for(auto &y:g[x]){
        if(!st.size())break;
        if(!mp[{x,y}]){mp[{x,y}]  = mp[{y , x}] =  st.top() ; st.pop();}
      }
    }
  }
  for(auto &x:ar){
    if(mp[x])cout << mp[x]-1 << '\n';
    else cout << st.top()-1 << '\n' , st.pop();
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;

const int MOD = 1000000007 ; //   1000000009 , 1000000023 ,998244353
const ll INF = ll(1e18);
const ld PI = 4*atan((ld)1);

using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a , ll b){
  if(a==0)return b;
  return gcd(b%a,a);
}
vector<vector<ll>>g;
map<pll,ll>res;
ll n;
void bfs(ll s){
  queue<ll>q;
  vector<bool>vis(n+1 , false);
  q.push(s);
  set<ll>st;
  for(ll i=1 ; i<=n ; i++)st.insert(i);
  while(!q.empty()){
    ll t = q.front();
    q.pop();
    vis[t] = 1;
    for(auto &x:g[t]){
      if(!vis[x])q.push(x) , res[{t,x}] = res[{x,t}] = *st.begin() , st.erase(st.begin());
    }
  }
}
int main() {

#ifdef AVI
  freopen("input.txt" , "r" , stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  //ll n;
  cin >> n;
  g = vector<vector<ll>>(n+1 , vector<ll>());
  vector<pll>ar;
  for(ll i=0 ; i<n-1 ; i++){
    ll x,y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    ar.push_back({x,y});
  }
  bfs(1);
  for(auto &x:ar){
    cout << res[x]-1 << '\n';
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;

const int MOD = 1000000007 ; //   1000000009 , 1000000023 ,998244353
const ll INF = ll(1e18);
const ld PI = 4*atan((ld)1);

using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a , ll b){
  if(a==0)return b;
  return gcd(b%a,a);
}

int main() {

#ifdef AVI
  freopen("input.txt" , "r" , stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<vector<ll>>g(n+1 , vector<ll>());
  vector<pll>ar;
  for(ll i=0 ; i<n-1 ; i++){
    ll x,y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    ar.push_back({x,y});
  }
  stack<ll>st;
  map<pll,ll>mp;
  for(ll i=n ; i>=1 ; i--)st.push(i);
    //cout << st.top();
  for(ll x=1 ; x<=n ; x++){
    if(g[x].size()>=3){
      for(auto &y:g[x]){
        if(!st.size())break;
        mp[{x,y}]  = mp[{y , x}] =  st.top() ; st.pop();
      }
    }
  }
  for(auto &x:ar){
    if(mp[x])cout << mp[x]-1 << '\n';
    else cout << st.top()-1 << '\n' , st.pop();
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;

const int MOD = 1000000007 ; //   1000000009 , 1000000023 ,998244353
const ll INF = ll(1e18);
const ld PI = 4*atan((ld)1);

using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a , ll b){
  if(a==0)return b;
  return gcd(b%a,a);
}

int main() {

#ifdef AVI
  freopen("input.txt" , "r" , stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<vector<ll>>g(n+1 , vector<ll>());
  vector<pll>ar;
  for(ll i=0 ; i<n-1 ; i++){
    ll x,y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    ar.push_back({x,y});
  }
  stack<ll>st;
  map<pll,ll>mp;
  for(ll i=n ; i>=1 ; i--)st.push(i);
    //cout << st.top();
  for(ll x=1 ; x<=n ; x++){
    if(g[x].size()>=3){
      for(auto &y:g[x]){mp[{x,y}]  = mp[{y , x}] =  st.top() ; st.pop();}
    }
  }
  for(auto &x:ar){
    if(mp[x])cout << mp[x]-1 << '\n';
    else cout << st.top()-1 << '\n' , st.pop();
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;

const int MOD = 1000000007 ; //   1000000009 , 1000000023 ,998244353
const ll INF = ll(1e18);
const ld PI = 4*atan((ld)1);

using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a , ll b){
  if(a==0)return b;
  return gcd(b%a,a);
}

int main() {

#ifdef AVI
  freopen("input.txt" , "r" , stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<vector<ll>>g(n+1 , vector<ll>());
  vector<pll>ar;
  for(ll i=0 ; i<n-1 ; i++){
    ll x,y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    ar.push_back({x,y});
  }
  stack<ll>st;
  map<pll,ll>mp;
  for(ll i=n-1 ; i>=0 ; i--)st.push(i);
  for(ll x=1 ; x<=n ; x++){
    if(g[x].size()>=3){
      for(auto &y:g[x])mp[{x,y}]  = mp[{y , x}] =  st.top() , st.pop();
    }
  }
  for(auto &x:ar){
    if(mp[x])cout << mp[x]-1 << '\n';
    else cout << st.top()-1 << '\n' , st.pop();
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef unsigned long long ull;

const int MOD = 1000000007 ; //   1000000009 , 1000000023 ,998244353
const ll INF = ll(1e18);
const ld PI = 4*atan((ld)1);

using namespace __gnu_pbds;
template<typename T, class cmp = std::less<T>>
using Tree = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a , ll b){
  if(a==0)return b;
  return gcd(b%a,a);
}

int main() {

#ifdef AVI
  freopen("input.txt" , "r" , stdin);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<vector<ll>>g(n+1 , vector<ll>());
  vector<pll>ar;
  for(ll i=0 ; i<n-1 ; i++){
    ll x,y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    ar.push_back({x,y});
  }
  stack<ll>st;
  map<pll,ll>mp;
  for(ll i=n-1 ; i>=0 ; i--)st.push(i);
  for(ll x=1 ; x<=n ; x++){
    if(g[x].size()>=3){
      for(auto &y:g[x])mp[{x,y}]  = mp[{y , x}] =  st.top() , st.pop();
    }
  }
  for(auto &x:ar){
    if(mp[x])cout << mp[x] << '\n';
    else cout << st.top()<< '\n' , st.pop();
  }
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    for (int i=n-2;i>=0;i--)
        cout<<i<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    for (int i=0;i<n-1;i++)
        cout<<i<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
vector<pair<int,int>>vec[maxn];
int vis[maxn];
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        vec[u].pb(make_pair(v,i));
        vec[v].pb(make_pair(u,i));
    }
    for (int i=0;i<n;i++)
        vis[i]=-1;
    for (auto i:vec)
        if (i.size()>=3)
        {
            for (int j=0;j<3;j++)
                vis[i[j].second]=j;
            int x=3;
            for (int j=0;j<n-1;j++)
                if (vis[j]==-1)
                    vis[j]=x++;
            for (int j=0;j<n-1;j++)
                cout<<vis[j]<<endl;
            return 0;
        }
    for (int i=0;i<n-1;i++)
        cout<<i<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    for (int i=0;i<n-1;i++)
    {
        if (i%2==0)
            cout<<i<<endl;
    }
    for (int i=n-2;i>=0;i--)
    {
        if (i%2==1)
            cout<<i<<endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    for (int i=0;i<n-1;i++)
    {
        if (i%2==0)
            cout<<i<<endl;
    }
    for (int i=0;i<n-1;i++)
    {
        if (i%2==1)
            cout<<i<<endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    for (int i=0;i<n-1;i++)
        cout<<i<<endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    for (int i=0;i<n-1;i++)
    {
        if (i%2==0)
            cout<<i<<endl;
    }
    for (int i=0;i<n-1;i--)
    {
        if (i%2==1)
            cout<<i<<endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof (a))
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <deque>
#include <vector>
#define endl '\n'
#define pb push_back
typedef long long ll;
const int maxn=100010;
const int inf=1e9;
const ll mod=1e9+7;
const double pi=acos(-1.0);
using namespace std;
int main()
{
    int n;
    cin>>n;
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
    }
    cout<<0<<endl;
    cout<<n/2<<endl;
    for (int i=n-2;i>0;i--)
    {
        if (i!=n/2)
            cout<<i<<endl;

    }
    return 0;
}


 #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
   ll n,x=0,i,m,j,t,p;

         cin>>n;
         ll a[n+5],b[n+5],c[n+5],d[n+5],e[n+5];
         for(i=0;i<n-1;i++){
            cin>>a[i]>>b[i];
            c[i]=b[i]-a[i];
            e[i]=c[i];
         }
         for(i=0;i<n-1;i++){
            d[i]=i;
         }
         sort(e,e+n-1);

         for(i=0;i<n-1;i++){
            for(j=0;j<n-1;j++){
                if(e[i]==c[j]){
                    cout<<d[i]<<endl;
                    c[j]=-5;
                    break;
                }
            }
         }

}


#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;const int N = 100001;int freq[N];int main() {    int n;cin >> n;vector < pair < int, int > > v;for (int i = 0 ; i < n - 1 ; i++) {
        int a;int b;cin >> a >> b;v.push_back(make_pair(a, b));freq[a - 1]++;freq[b - 1]++;} int mex = 0; int mexIdx = -1;for (int i = 0 ; i < n ; i++) {if (freq[i] > mex) {
            mex = freq[i];mexIdx = i;}}
    int idx1 = 0;int idx2 = mex; for (int i = 0 ; i < n - 1 ; i++) {if (v[i].first == (mexIdx + 1) || v[i].second == (mexIdx + 1)) {cout << idx1 << endl;
            idx1++; } else {cout << idx2 << endl;idx2++;}}
return 0;
}


 #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
   ll n,x=0,i,m,j,t,p;

         cin>>n;
         ll a[n+5],b[n+5],c[n+5],d[n+5],e[n+5];
         for(i=0;i<n-1;i++){
            cin>>a[i]>>b[i];
            c[i]=b[i]-a[i];
            e[i]=c[i];
         }
         for(i=0;i<n-1;i++){
            d[i]=i;
         }
         sort(e,e+5);

         for(i=0;i<n-1;i++){
            for(j=0;j<n-1;j++){
                if(e[i]==c[j]){
                    cout<<d[i]<<endl;
                    c[j]=-5;
                    break;
                }
            }
         }

}


#include <iostream>
using namespace std;

int main() {

      int t;
      cin>>t;
      int n =t;
      while(t--){
      	int a,b;
      	cin>>a>>b;

      }

      for(int i=n-2;i>=0;i--) {
      		cout<<i<<endl;
      	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;

int n,cnt;
vector<pair<int,int> >v;
vector<int>g[N];
map<pair<int,int>,int>mp;
map<pair<int,int>,bool>vis;

int main()
{
    ROCK;
    cin>>n;
    for(int i=1; i<n; i++)
    {
        int x,y;cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }for(auto i : v){
        int x=i.f,y=i.sec;
        if((int)g[x].size()==1 || (int)g[y].size()==1){
            mp[{x,y}]=cnt;vis[{x,y}]=1;cnt++;
            continue;
        }
    }for(auto i : v){if(!vis[{i.f,i.sec}]){mp[{i.f,i.sec}]=cnt;cnt++;}}
    for(auto i : v)cout<<mp[{i.f,i.sec}]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;
vector<int>g[N];
int vis[N],cnt=0,n;
map<pair<int,int>,int>mp;
vector<pair<int,int> >v;

void dfs(int node){
  vis[node]=1;
  for(auto child : g[node]){
    if(vis[child]==-1){
        mp[{node,child}]=cnt;
        mp[{child,node}]=cnt;
        cnt+=2;
        if(cnt>n-2)cnt=1;
        dfs(child);
    }
  }
}

int main()
{
    ROCK;
    memset(vis,-1,sizeof vis);
    cin>>n;
    int start=-1;
    for(int i=1; i<n; i++)
    {

        int x,y;cin>>x>>y;
        if(start==-1)start=x;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }

    //sort(v.begin(),v.end());
    dfs(start);
    for(auto x : v)cout<<mp[{x.f,x.sec}]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;
vector<int>g[N];
int vis[N],cnt=0,n;
map<pair<int,int>,int>mp;
vector<pair<int,int> >v;

void dfs(int node){
  vis[node]=1;
  for(auto child : g[node]){
    if(vis[child]==-1){
        mp[{node,child}]=cnt;
        mp[{child,node}]=cnt;
        cnt+=2;
        if(cnt>n-2)cnt=1;
        dfs(child);
    }
  }
}

int main()
{
    ROCK;
    memset(vis,-1,sizeof vis);
    cin>>n;
    int start=-1;
    for(int i=1; i<n; i++)
    {

        int x,y;cin>>x>>y;
        if(start==-1)start=x;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }

    sort(v.begin(),v.end());
    dfs(1);
    for(auto x : v)cout<<mp[{x.f,x.sec}]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;
vector<pair<pair<int,int> ,int> >v;
map<int,int>mp;

int main()
{
    ROCK;
    int n;cin>>n;
    for(int i=0; i<n-1; i++)
    {
        int x,y;cin>>x>>y;
        v.push_back({{x,y},i+1});
    }

    sort(v.begin(),v.end());
    for(int i=0,j=v.size()-1,cnt=0; i<=j;cnt++){
        if(i%2==0)mp[v[i++].sec]=cnt;
        else mp[v[j--].sec]=cnt;
    }

    for(auto x : mp)cout<<x.sec<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;
vector<int>g[N];
int vis[N],cnt=0,n;
map<pair<int,int>,int>mp;
vector<pair<int,int> >v;

void dfs(int node){
  vis[node]=1;
  for(auto child : g[node]){
    if(vis[child]==-1){
        mp[{node,child}]=cnt;
        mp[{child,node}]=cnt;
        cnt+=2;
        if(cnt>n-2)cnt=1;
        dfs(child);
    }
  }
}

int main()
{
    ROCK;
    memset(vis,-1,sizeof vis);
    cin>>n;
    int start=-1;
    for(int i=1; i<n; i++)
    {

        int x,y;cin>>x>>y;
        if(start==-1)start=x;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }

    //cout<<start<<endl;
    dfs(1);

    for(auto x : v)cout<<mp[{x.f,x.sec}]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;

int vis[N],cnt,n;

map<pair<int,int>,int>mp;
vector<pair<int,int> >v;
vector<int>g[N];

void bfs(int node)
{
    queue<int>q;
    q.push(node);
    vis[node]=0;
    while(q.size())
    {
        int x=q.front();
        q.pop();
        for(auto child : g[x])
        {
            if(!vis[child])
            {
                vis[child]=vis[x]+1;
                if(cnt>n-2)cnt=0;
                mp[{x,child}]=cnt;
                mp[{child,x}]=cnt;
                cnt++;
                q.push(child);
            }
        }
    }

}

int main()
{
    ROCK;
    cin>>n;
    for(int i=1; i<n; i++)
    {
        int x,y;cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }

    int mx=0,start=0;
    for(int i=1;i<=n;i++){
        if((int)g[i].size()>=mx){
            mx=(int)g[i].size();
            start=i;
        }
    }

    bfs(start);
    for(auto x : v)cout<<mp[{x.f,x.sec}]<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;
vector<int>g[N];
int vis[N],cnt=0,n;
map<pair<int,int>,int>mp;
vector<pair<int,int> >v;

void dfs(int node){
  vis[node]=1;
  for(auto child : g[node]){
    if(vis[child]==-1){
        mp[{node,child}]=cnt;
        cnt+=2;
        if(cnt>n-2)cnt=1;
        dfs(child);
    }
  }
}

int main()
{
    ROCK;
    memset(vis,-1,sizeof vis);
    cin>>n;
    for(int i=1; i<n; i++)
    {
        int x,y;cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }

    dfs(1);
    for(auto x : v)cout<<mp[{x.f,x.sec}]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;

int vis[N],n;

map<pair<int,int>,int>mp;
vector<pair<int,int> >v;
vector<int>g[N];

void bfs(int node)
{
    memset(vis,-1,sizeof vis);
    queue<int>q;
    q.push(node);
    vis[node]=0;
    while(q.size())
    {
        int x=q.front();
        q.pop();
        for(auto child : g[x])
        {
            if(vis[child]==-1)
            {
                vis[child]=vis[x]+1;
                q.push(child);
            }
        }
    }

}

int cnt=1;
void dfs(int node){
  vis[node]=1;
  for(auto child : g[node]){
    if(!vis[child]){

        mp[{node,child}]=cnt;
        mp[{child,node}]=cnt;
        cnt+=2;
        if(cnt>n-2){
            if(cnt%2)cnt=2;
            if(cnt>n-2)cnt=0;
        }dfs(child);
    }
  }
}

int main()
{
    ROCK;
    cin>>n;
    for(int i=1; i<n; i++)
    {
        int x,y;cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        v.push_back({x,y});
    }

    bfs(1);
    int start=0,mxdis=0;
    for(int i=1;i<=n;i++){
        if(vis[i]>mxdis){
            mxdis=vis[i];
            start=i;
        }
    }

    memset(vis,0,sizeof vis);
    dfs(start);
    for(auto x : v)cout<<mp[{x.f,x.sec}]<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds ;
const long long int inf =(long long)1e18;
const int dx[4] = {0, 0, 1, -1},dy[4] = {1, -1, 0, 0};
typedef long long int ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

const int N=2e5+10;
vector<pair<pair<int,int> ,int> >v;
map<int,int>mp;

int main()
{
    ROCK;
    int n;cin>>n;
    for(int i=0; i<n-1; i++)
    {
        int x,y;cin>>x>>y;
        v.push_back({{x,y},i+1});
    }

    sort(v.begin(),v.end());
    for(int i=0,j=v.size()-1,cnt=0; i<=j;cnt++){
        if((i+j)%2==0){mp[v[i].sec]=cnt;i++;}
        else {mp[v[j].sec]=cnt;j--;}
    }

    for(auto x : mp)cout<<x.sec<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/
int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<pair<int , int > , int > > vp;
 for(int i = 0 ; i < n-1 ; i++)
 {
  int x , y ; cin >> x >> y;
  v[x].insert(y);
  v[y].insert(x);
  if(v[x].size() == 3)
    sp.insert(i);
  if(v[y].size() == 3)
    sp.insert(i);
  vp.pb(mk(mk(x , y) , 0));
 }

  int d = 0 , k = n-2;
 for(int i = 0 ; i<n-1 ; i++)
 {
  if(sp.size() != 0  &&  sp.find(i) != sp.end())
    cout << k-- << endl;
  else cout << d++ << endl;
 }

}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/
int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int ,int> > vp;
 for(int i = 0 ; i<n-1 ; i++)
 {
  int x , y; cin >> x >> y;
  vp.pb(mk(x , y));
  v[x].insert(y);
  v[y].insert(x);
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int k = 0 , d = n-2;

  for(int i = 0 ; i<vp.size() ; i++)
  {
    int x = vp[i].ff;
    int y = vp[i].ss;
    if(sp.size() != 0 && sp.find(x) != sp.end())
      cout << d-- << endl;
    else cout << k++ << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/
int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int ,int> > vp;
 for(int i = 0 ; i<n-1 ; i++)
 {
  int x , y; cin >> x >> y;
  vp.pb(mk(x , y));
  v[x].insert(y);
  v[y].insert(x);
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int ar[n+1][n+1]={0};
 if(sp.size() != 0)
 {
  int k = n-1;
   for(auto it : sp)
   {
    //cout << "it = " << it << endl;
     for(auto it1 : v[it])
      {
        //cout << "it1 = " << it1 << endl;
        ar[it][it1] = k;
        ar[it1][it] = k--;
      }
   }
   k = 1 ;
   for(int i = 1 ; i<=n ; i++)
    for(auto it : v[i])
      if(ar[i][it] == 0)
        {
          //cout << "i = " << i << " it = " << it << "  k = " << k <<  endl;
          ar[i][it] = k;
          ar[it][i] = k++;
        }
 }
 else{
  int k = 1 ;
   for(int i = 1 ; i<=n ; i++)
    for(auto it : v[i])
      if(ar[i][it] == 0)
        {
         //cout << "i = " << i << " it = " << it << endl;
          ar[it][i] = k;
          ar[i][it] = k++;
        }
 }
 for(int i = 0 ; i<vp.size() ; i++)
 {
  cout << ar[vp[i].ff][vp[i].ss] - 1 << endl;
 }

}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/

int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int , int> > vp;
 for(int i = 0 ; i<n-1; i++)
 {
  int x , y; cin >> x >> y;
  v[x].insert(i);
  v[y].insert(i);
  vp.pb(mk(x , y));
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int d = 0 , k = n - 2;
 for(int i = 0 ; i<n-1; i++)
 {
   int x = vp[i].ff;
   int y = vp[i].ss;
   if(sp.size() != 0 && (sp.find(x) != sp.end()) || sp.find(y) != sp.end())
    cout << d++ << endl;
  else cout << k-- << endl;
 }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/
int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int ,int> > vp;
 for(int i = 0 ; i<n-1 ; i++)
 {
  int x , y; cin >> x >> y;
  vp.pb(mk(x , y));
  v[x].insert(y);
  v[y].insert(x);
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int ar[n+1][n+1]={0};
 if(sp.size() != 0)
 {
  int k = 1;
   for(auto it : sp)
   {
    //cout << "it = " << it << endl;
     for(auto it1 : v[it])
      {
        //cout << "it1 = " << it1 << endl;
        ar[it][it1] = k;
        ar[it1][it] = k++;
      }
   }
   for(int i = 1 ; i<=n ; i++)
    for(auto it : v[i])
      if(ar[i][it] == 0)
        {
          //cout << "i = " << i << " it = " << it << "  k = " << k <<  endl;
          ar[i][it] = k;
          ar[it][i] = k++;
        }
 }
 else{
  int k = 1 ;
   for(int i = 1 ; i<=n ; i++)
    for(auto it : v[i])
      if(ar[i][it] == 0)
        {
         //cout << "i = " << i << " it = " << it << endl;
          ar[it][i] = k;
          ar[i][it] = k++;
        }
 }
 for(int i = 0 ; i<vp.size() ; i++)
 {
  cout << ar[vp[i].ff][vp[i].ss] - 1 << endl;
 }

}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/
int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int ,int> > vp;
 for(int i = 0 ; i<n-1 ; i++)
 {
  int x , y; cin >> x >> y;
  vp.pb(mk(x , y));
  v[x].insert(y);
  v[y].insert(x);
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int ar[n+1][n+1]={0};
 if(sp.size() != 0)
 {
  int k = n-1;
   for(auto it : sp)
   {
    //cout << "it = " << it << endl;
     for(auto it1 : v[it])
      {
        //cout << "it1 = " << it1 << endl;
        ar[it][it1] = k;
        ar[it1][it] = k--;
      }
   }
   k = 1 ;
   for(int i = 1 ; i<=n ; i++)
    for(auto it : v[i])
      if(ar[i][it] == 0)
        {
          //cout << "i = " << i << " it = " << it << "  k = " << k <<  endl;
          ar[i][it] = k;
          ar[it][i] = k++;
        }
 }
 else{
  int k = 1 ;
   for(int i = 1 ; i<=n ; i++)
    for(auto it : v[i])
      if(ar[i][it] == 0)
        {
          cout << "i = " << i << " it = " << it << endl;
          ar[it][i] = k;
          ar[i][it] = k++;
        }
 }
 for(int i = 0 ; i<vp.size() ; i++)
 {
  cout << ar[vp[i].ff][vp[i].ss] - 1 << endl;
 }

}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int , int> > vp;
 for(int i = 0 ; i<n-1; i++)
 {
  int x , y; cin >> x >> y;
  v[x].insert(i);
  v[y].insert(i);
  vp.pb(mk(x , y));
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int d = 0 , k = n - 2;
 for(int i = 0 ; i<n-1; i++)
 {
   int x = vp[i].ff;
   int y = vp[i].ss;
   if(sp.size() != 0 && (sp.find(x) != sp.end()) || sp.find(y) != sp.end())
    cout << d++ << endl;
  else cout << k-- << endl;
 }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/

int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n ; cin >> n;
 set<int>v[n+1];
 int key = -1;
 vector<pair<int , int > > vp;
 for(int i = 0 ; i<n-1 ; i++)
 {
  int x , y ; cin >> x >> y;
  v[x].insert(i);
  v[y].insert(i);
  vp.pb(mk(x, y));
  if(key == -1 && v[x].size() == 3 )
     key = x;
  else if(key == -1 && v[y].size() == 3)
    key = y;
 }
 int c = 0 , k = n-2;
 for(int i = 0 ; i<n-1 ; i++)
 {
    int x = vp[i].ff;
    int y = vp[i].ss;
    if(key != -1 && (v[key].find(i) != v[key].end()))
      cout << c++ << endl;
    else cout << k-- << endl;
 }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define mod 998244353
typedef long long ll;

/*bool cmp(pair<ll,string>p1 , pair<ll , string>p2)
{
  if(p1.ff > p2.ff)
    return true;
  return false;
}*/
int main()
{
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 #endif

 fast
 int n; cin >> n;
 set<int>v[n+1];
 set<int>sp;
 vector<pair<int ,int> > vp;
 for(int i = 0 ; i<n-1 ; i++)
 {
  int x , y; cin >> x >> y;
  vp.pb(mk(x , y));
  v[x].insert(y);
  v[y].insert(x);
  if(v[x].size() == 3)
    sp.insert(x);
  if(v[y].size() == 3)
    sp.insert(y);
 }
 int k = 0 , d = n-2;
 for(int i = 0 ; i<vp.size() ; i++)
 {
   int x = vp[i].ff;
   int y = vp[i].ss;
   if(sp.size() != 0 && (sp.find(x) != sp.end() || sp.find(y) != sp.end()))
    cout << d-- << endl;
  else cout << k++ << endl;
 }
/*
 for(int i = 0 ; i<vp.size() ; i++)
 {
  cout << ar[vp[i].ff][vp[i].ss] << endl;
 }
*/

}


/* viper_mark | Aman kumar singh | On project baigan bharta. */
#include <bits/stdc++.h>
#define fast_inou ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pll pair<ll,ll>
#define mod 1000000007
#define ln "\n"
#define ll long long
#define mr make_pair
#define pb push_back
using namespace std;
int main()
{
    fast_inou
    ll n;
    cin>>n;
    vector<ll> vect[n+1];
    vector<pll> vect1;
    for(ll i=0;i<n;i++)
    {
        ll x,y;
        cin>>x>>y;
        vect1.pb({x,y});
        vect[x].pb(y);
        vect[y].pb(x);
    }
    ll ind=-1;
    for(ll i=1;i<=n;i++)
    {
        if(vect[i].size()>=3)
        {
            ind=i;
            break;
        }
    }
    ll val1=n-2;
    ll val2=0;
    ll c=0;
   // cout<<ind<<ln;
    for(ll i=0;i<n-1;i++)
    {
       if(vect1[i].first==ind || vect1[i].second==ind)
       {
           if(c<3)
           {
               cout<<val2<<ln;
               val2++;
           }
           else
           {
               cout<<val1<<ln;
               val1--;
           }
           c++;
       }
       else
       {
           cout<<val1<<ln;
           val1--;
       }
    }
    return 0;
}