Problem p02850 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
                  　　　　　　　　　　(¸.·´ (¸.·'* ☆
                                        */

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>

#pragma GCC optimize("Ofast")
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rep2(i, n) for (int i = 2; i < (n); ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define reprm(i, n) for (int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printin(a) printf(a ? "possible\n" : "inposible\n")
#define printdb(a) printf("%.50lf\n", a)    // 少数出力
#define printdbd(a) printf("%.16lf\n", a)   // 少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) // string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2) // 等差数列の和、初項,末項,項数
#define p push
#define pb push_back
#define priq priority_queue
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg) / 360.0) * 2.0 * PI)
#define rad_to_deg(rad) (((rad) / 2.0 / PI) * 360.0)
#define Please return
#define AC 0

using ll = long long;

const int INF = 1073741823;
const int MINF = -1073741823;
const ll LINF = ll(4661686018427387903);
const ll MOD = 1000000007;
const double PI = acos(-1);

using namespace std;

void scans(string &str) {
  char c;
  scanf("%c", &c);
  if (c == '\n')
    scanf("%c", &c); // 最初の改行対策
  while (c != '\n' && c != -1) {
    str += c;
    scanf("%c", &c);
  }
}

void scanc(char &str) {
  char c;
  scanf("%c", &c);
  if (c == -1)
    return;
  while (c == '\n') {
    scanf("%c", &c);
  }
  str = c;
}

double acot(double x) { return PI / 2 - atan(x); }

/*-----------------------------------------ここからコード-----------------------------------------*/
ll ans = 0;
ll n;
void dfs(ll now, ll nowcolour, vector<vector<pair<ll, ll>>> &graph,
         vector<bool> &visited) {
  ll newcolour = 1;
  visited[now] = true;
  ll i = 0;
  for (auto a : graph[now]) {
    if (a.second == 0) {
      if (visited[a.first] == false) {
        if (nowcolour == newcolour)
          ++newcolour;
        graph[now][i].second = newcolour;
        dfs(a.first, newcolour, graph, visited);
        ++newcolour;
      }
    }
    ++i;
  }
  ans = max(ans, newcolour);
}

int main() {

  scanf("%lld", &n);
  vector<vector<pair<ll, ll>>> graph(n);
  vector<bool> visited(n);
  vector<pair<int, int>> anss(n - 1);
  ll a, b;
  rep(i, n - 1) {
    scanf("%lld%lld", &a, &b);
    graph[a - 1].pb(make_pair(b - 1, 0));
    graph[b - 1].pb(make_pair(a - 1, 0));
    anss[i] = make_pair(a - 1, b - 1);
  }
  dfs(0, 0, graph, visited);
  printf("%lld\n", ans - 1);
  for (auto a : anss) {
    ll ansa = -1, ansb = -1;
    for (auto b : graph[a.first]) {
      if (b.first == a.second)
        ansa = b.second;
    }
    if (ansa == -1)
      for (auto b : graph[a.second]) {
        if (b.first == a.first)
          ansb = b.second;
      }
    if (ansa == -1 && ansb == -1)
      continue;
    else
      printf("%lld\n", max(ansa, ansb));
  }

  Please AC;
}


Fixed submission:
/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
                  　　　　　　　　　　(¸.·´ (¸.·'* ☆
                                        */

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>

#pragma GCC optimize("Ofast")
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rep2(i, n) for (int i = 2; i < (n); ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define reprm(i, n) for (int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printin(a) printf(a ? "possible\n" : "inposible\n")
#define printdb(a) printf("%.50lf\n", a)    // 少数出力
#define printdbd(a) printf("%.16lf\n", a)   // 少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) // string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2) // 等差数列の和、初項,末項,項数
#define p push
#define pb push_back
#define priq priority_queue
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg) / 360.0) * 2.0 * PI)
#define rad_to_deg(rad) (((rad) / 2.0 / PI) * 360.0)
#define Please return
#define AC 0

using ll = long long;

const int INF = 1073741823;
const int MINF = -1073741823;
const ll LINF = ll(4661686018427387903);
const ll MOD = 1000000007;
const double PI = acos(-1);

using namespace std;

void scans(string &str) {
  char c;
  scanf("%c", &c);
  if (c == '\n')
    scanf("%c", &c); // 最初の改行対策
  while (c != '\n' && c != -1) {
    str += c;
    scanf("%c", &c);
  }
}

void scanc(char &str) {
  char c;
  scanf("%c", &c);
  if (c == -1)
    return;
  while (c == '\n') {
    scanf("%c", &c);
  }
  str = c;
}

double acot(double x) { return PI / 2 - atan(x); }

/*-----------------------------------------ここからコード-----------------------------------------*/
ll ans = 0;
ll n;
void dfs(ll now, ll nowcolour, vector<vector<pair<ll, ll>>> &graph,
         vector<bool> &visited) {
  ll newcolour = 1;
  visited[now] = true;
  ll i = 0;
  for (auto a : graph[now]) {
    if (a.second == 0) {
      if (visited[a.first] == false) {
        if (nowcolour == newcolour)
          ++newcolour;
        graph[now][i].second = newcolour;
        dfs(a.first, newcolour, graph, visited);
        ++newcolour;
      }
    }
    ++i;
  }
  ans = max(ans, newcolour);
}

int main() {

  scanf("%lld", &n);
  vector<vector<pair<ll, ll>>> graph(n);
  vector<bool> visited(n);
  vector<pair<int, int>> anss(n - 1);
  ll a, b;
  rep(i, n - 1) {
    scanf("%lld%lld", &a, &b);
    graph[a - 1].pb(make_pair(b - 1, 0));
    graph[b - 1].pb(make_pair(a - 1, 0));
    anss[i] = make_pair(a - 1, b - 1);
  }
  dfs(0, 0, graph, visited);
  printf("%lld\n", ans - 1);
  for (auto a : anss) {
    ll ansa = -1, ansb = -1;
    for (auto b : graph[a.first]) {
      if (b.first == a.second)
        ansa = b.second;
    }
    if (ansa == -1)
      for (auto b : graph[a.second]) {
        if (b.first == a.first)
          ansb = b.second;
      }
    if (ansa == -1 && ansb == -1)
      continue;
    else
      printf("%lld\n", max(ansa, ansb));
  }

  Please AC;
}