#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
template <class T>
using VV = vector<vector<T>>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<long long>;
using VVL = vector<vector<long long>>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using VD = vector<double>;
using VVD = vector<vector<double>>;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;
using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;
#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)x.size())
#define fastio() \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr)
#define debug(x) cerr << #x << " = " << x << endl;
const ll MOD = 1e9 + 7;
template <class T>
inline void maxi(T &a, T b)
{
a = max(a, b);
}
template <class T>
inline void mini(T &a, T b)
{
a = min(a, b);
}
template <class T>
inline void addi(T &a, T b)
{
a = (a + b) % MOD;
}
template <class T>
inline void subi(T &a, T b)
{
a = (a - b + MOD) % MOD;
}
template <class T>
inline T add(T a, T b)
{
return (a + b) % MOD;
}
template <class T>
inline T sub(T a, T b)
{
return (a - b + MOD) % MOD;
}
template <class T>
inline T mul(T a, T b)
{
return ((a % MOD) * (b % MOD)) % MOD;
}
constexpr ll binpow(ll a, ll b, ll mod)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
{
res = (res * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return res;
}
const int INF = 1e9;
const int MAX_N = 5e5 + 3;
int n;
VVL dp(MAX_N, VL(2, 0)); // 0 - best, 1 - second best
VL dp2(MAX_N, 0); // best up the tree
VVL dp_ways(MAX_N, VL(2, 1));
VL dp2_ways(MAX_N, 1);
VI g[MAX_N];
void dfs1(int c, int p)
{
for (auto v : g[c])
{
if (v == p)
continue;
dfs1(v, c);
if (dp[v][0] + 1 == dp[c][1])
{
dp_ways[c][1] = dp_ways[v][0];
continue;
}
if (dp[v][0] + 1 > dp[c][1])
{
dp[c][1] = dp[v][0] + 1;
dp_ways[c][1] = dp_ways[v][0];
}
if (dp[c][0] < dp[c][1] ||
(dp[c][0] == dp[c][1] && dp_ways[c][0] < dp_ways[c][1]))
{
swap(dp[c][0], dp[c][1]);
swap(dp_ways[c][0], dp_ways[c][1]);
}
}
}
ll ans = 0;
ll ans_ways = 0;
void dfs2(int c, int p)
{
if (sz(g[c]) >= 3)
{
VPLL vec = {{dp[c][0], dp_ways[c][0]},
{dp[c][1], dp_ways[c][1]},
{dp2[c], dp2_ways[c]}};
sort(RALL(vec));
ll t = vec[0].first * (vec[1].first + vec[2].first);
ll t_w = vec[0].second *vec[1].second *vec[2].second;
if(vec[0].first == vec[1].first == vec[2].first) {
t_w *= 3;
}
else if(vec[0].first == vec[1].first) {
t_w *= 2;
}
if(t > ans) {
ans = t;
ans_ways = t_w;
}
else if(t == ans) {
ans_ways += t_w;
}
}
for (auto v : g[c])
{
if (v == p)
continue;
dp2[v] = dp2[c] + 1;
if (dp[v][0] + 1 == dp[c][0])
{ // use second best
dp2[v] = dp[c][1] + 1;
dp2_ways[v] = dp_ways[c][1];
}
else
{
dp2[v] = dp[c][0] + 1;
dp2_ways[v] = dp_ways[c][0];
}
dfs2(v, c);
}
}
void solve()
{
cin >> n;
FOR(i, 0, n - 1)
{
int a, b;
cin >> a >> b;
a--, b--;
g[a].PB(b);
g[b].PB(a);
}
dfs1(0, 0);
dfs2(0, 0);
if(ans == 0) ans_ways = 1;
cout << ans << SPACE << ans_ways << LINE;
}
int main()
{
fastio();
int t = 1;
// cin >> t;
while (t--)
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |