Submission #64773382


Source Code Expand

#ifndef IO_HPP
#define IO_HPP 1

#include <array>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

namespace io {

void __read(char& c) { std::cin >> c; }
void __read(std::string& s) { std::cin >> s; }

template <typename T>
void __read_real(T& x) {
  std::string s;
  __read(s);
  x = std::stod(s);
}

template <typename T>
void __read_integer(T& x) {
  std::cin >> x;
}

void __read(int& x) { __read_integer(x); }
void __read(unsigned int& x) { __read_integer(x); }
void __read(long& x) { __read_integer(x); }
void __read(unsigned long& x) { __read_integer(x); }
void __read(long long& x) { __read_integer(x); }
void __read(unsigned long long& x) { __read_integer(x); }
void __read(double& x) { __read_real(x); }
void __read(long double& x) { __read_real(x); }

template <class U, class V>
void __read(std::pair<U, V>& p) {
  __read(p.first);
  __read(p.second);
}

template <size_t N = 0, typename T>
void __read_tuple(T& t) {
  if constexpr (N < std::tuple_size<T>::value) {
    auto& x = std::get<N>(t);
    __read(x);
    __read_tuple<N + 1>(t);
  }
}
template <class... T>
void __read(std::tuple<T...>& t) {
  __read_tuple(t);
}

template <size_t N = 0, typename T>
void __read(std::array<T, N>& a) {
  for (auto& x : a) {
    __read(x);
  }
}

template <class T>
void __read(std::vector<T>& v) {
  for (auto& x : v) {
    __read(x);
  }
}

void read() {}
template <class H, class... T>
void read(H& h, T&... t) {
  __read(h), read(t...);
}

void __write(const char c) { std::cout << c; }

void __write(const std::string s) {
  for (char c : s) {
    __write(c);
  }
}

void __write(const char* s) {
  size_t len = strlen(s);
  for (size_t i = 0; i < len; ++i) {
    __write(s[i]);
  }
}

template <typename T>
void __write_integer(T x) {
  std::cout << x;
}

template <typename T>
void __write_real(T x) {
  std::ostringstream oss;
  oss << std::fixed << std::setprecision(15) << double(x);
  std::string s = oss.str();
  __write(s);
}

void __write(int x) { __write_integer(x); }
void __write(unsigned int x) { __write_integer(x); }
void __write(long x) { __write_integer(x); }
void __write(unsigned long x) { __write_integer(x); }
void __write(long long x) { __write_integer(x); }
void __write(unsigned long long x) { __write_integer(x); }
void __write(double x) { __write_real(x); }
void __write(long double x) { __write_real(x); }

template <class U, class V>
void __write(const std::pair<U, V> p) {
  __write(p.first);
  __write(' ');
  __write(p.second);
}

template <size_t N = 0, typename T>
void __write_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) {
      __write(' ');
    }
    const auto x = std::get<N>(t);
    __write(x);
    __write_tuple<N + 1>(t);
  }
}
template <class... T>
void __write(std::tuple<T...> t) {
  __write_tuple(t);
}

template <class T, size_t S>
void __write(const std::array<T, S> a) {
  auto n = a.size();
  for (size_t i = 0; i < n; i++) {
    if (i) {
      __write(' ');
    }
    __write(a[i]);
  }
}

template <class T>
void __write(const std::vector<T> v) {
  auto n = v.size();
  for (size_t i = 0; i < n; i++) {
    if (i) {
      __write(' ');
    }
    __write(v[i]);
  }
}

template <class T>
void __write(const std::set<T> s) {
  __write(std::vector<T>{s.cbegin(), s.cend()});
}

template <class K, class V>
void __write(const std::map<K, V> m) {
  __write(std::vector<std::pair<K, V>>{m.cbegin(), m.cend()});
}

void write() { __write('\n'); }
template <class Head, class... Tail>
void write(Head&& head, Tail&&... tail) {
  __write(head);
  if (sizeof...(Tail)) {
    __write(' ');
  }
  write(std::forward<Tail>(tail)...);
}

}  // namespace io

#endif  // IO_HPP

#ifdef ONLINE_JUDGE
#include <bits/allocator.h>
#pragma GCC optimize("Ofast,unroll-loops,inline")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif

#include <bits/stdc++.h>
#include <atcoder/all>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using namespace atcoder;
using namespace io;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vector<int> eccentricity(const vector<vector<int>>& g) {
  const int n = g.size();
  vector<int> height(n), answer(n);

  auto dfs = [&](auto&& self, int u, int p) -> void {
    height[u] = 0;
    for (int v : g[u]) {
      if (v == p) {
        continue;
      }
      self(self, v, u);
      height[u] = max(height[u], height[v] + 1);
    }
  };

  auto reroot = [&](auto&& self, int u, int p, int height_p) -> void {
    answer[u] = max(height[u], height_p);
    int m1{height_p}, m2{-1};

    for (int v : g[u]) {
      if (v == p) {
        continue;
      }
      const int candidate{height[v] + 1};
      if (candidate > m1) {
        m2 = m1;
        m1 = candidate;
      } else if (candidate > m2) {
        m2 = candidate;
      }
    }

    for (int v : g[u]) {
      if (v == p) {
        continue;
      }
      const int candidate{height[v] + 1};
      if (candidate == m1) {
        self(self, v, u, m2 + 1);
      } else {
        self(self, v, u, m1 + 1);
      }
    }
  };

  dfs(dfs, 0, -1);
  reroot(reroot, 0, -1, 0);
  return answer;
}

void solve() {
  int n1;
  read(n1);
  vector<pair<int, int>> uv1(n1 - 1);
  read(uv1);
  int n2;
  read(n2);
  vector<pair<int, int>> uv2(n2 - 1);
  read(uv2);
  vector<vector<int>> g1(n1), g2(n2);
  for (auto& [u, v] : uv1) {
    --u, --v;
    g1[u].push_back(v);
    g1[v].push_back(u);
  }
  for (auto& [u, v] : uv2) {
    --u, --v;
    g2[u].push_back(v);
    g2[v].push_back(u);
  }
  const auto ecc1{eccentricity(g1)}, ecc2{eccentricity(g2)};
  int m{max(*max_element(ecc1.cbegin(), ecc1.cend()),
            *max_element(ecc2.cbegin(), ecc2.cend()))};
  vector<long long> a(n1), b(n2);
  for (int e1 : ecc1) {
    ++a[e1];
  }
  for (int e2 : ecc2) {
    ++b[e2];
  }
  const auto c{convolution_ll(a, b)};
  int64_t answer{};
  for (int k{}; k < int(c.size()); ++k) {
    answer += c[k] * max(k + 1, m);
  }
  write(answer);
}

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

Submission Info

Submission Time
Task F - Add One Edge 3
User nika_skybytska
Language C++ 20 (gcc 12.2)
Score 500
Code Size 6582 Byte
Status AC
Exec Time 276 ms
Memory 68164 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 49
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3560 KiB
00_sample_01.txt AC 1 ms 3488 KiB
01_random_00.txt AC 239 ms 49932 KiB
01_random_01.txt AC 49 ms 13876 KiB
01_random_02.txt AC 25 ms 8592 KiB
01_random_03.txt AC 4 ms 4036 KiB
01_random_04.txt AC 230 ms 46456 KiB
01_random_05.txt AC 127 ms 30320 KiB
01_random_06.txt AC 103 ms 24680 KiB
01_random_07.txt AC 89 ms 19496 KiB
01_random_08.txt AC 105 ms 25336 KiB
01_random_09.txt AC 93 ms 21624 KiB
01_random_10.txt AC 190 ms 50952 KiB
01_random_11.txt AC 85 ms 24992 KiB
01_random_12.txt AC 172 ms 42396 KiB
01_random_13.txt AC 39 ms 12348 KiB
01_random_14.txt AC 170 ms 40824 KiB
01_random_15.txt AC 44 ms 15304 KiB
01_random_16.txt AC 102 ms 31204 KiB
01_random_17.txt AC 80 ms 21872 KiB
01_random_18.txt AC 193 ms 50736 KiB
01_random_19.txt AC 173 ms 41448 KiB
01_random_20.txt AC 237 ms 49360 KiB
01_random_21.txt AC 134 ms 32680 KiB
01_random_22.txt AC 54 ms 14176 KiB
01_random_23.txt AC 56 ms 15716 KiB
01_random_24.txt AC 111 ms 26896 KiB
01_random_25.txt AC 120 ms 28748 KiB
01_random_26.txt AC 126 ms 31348 KiB
01_random_27.txt AC 122 ms 26708 KiB
01_random_28.txt AC 108 ms 25716 KiB
01_random_29.txt AC 55 ms 15392 KiB
01_random_30.txt AC 232 ms 67900 KiB
01_random_31.txt AC 231 ms 66360 KiB
01_random_32.txt AC 130 ms 47880 KiB
01_random_33.txt AC 201 ms 55172 KiB
01_random_34.txt AC 107 ms 37716 KiB
01_random_35.txt AC 45 ms 17156 KiB
01_random_36.txt AC 98 ms 32316 KiB
01_random_37.txt AC 57 ms 22292 KiB
01_random_38.txt AC 63 ms 27072 KiB
01_random_39.txt AC 85 ms 25352 KiB
01_random_40.txt AC 249 ms 68164 KiB
01_random_41.txt AC 255 ms 68044 KiB
01_random_42.txt AC 276 ms 63196 KiB
01_random_43.txt AC 266 ms 63012 KiB
01_random_44.txt AC 259 ms 57164 KiB
01_random_45.txt AC 1 ms 3532 KiB
01_random_46.txt AC 74 ms 42964 KiB