This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const int ms = 5e5 + 20;
const int inf = 1e9 + 20;
vector<int> adj[ms];
pair<long long, long long> res = {0, 1};
pair<int, int> operator+(pair<int, int> x, int d) {
return {x.first + d, x.second};
}
pair<long long, long long> max(pair<long long, long long> x, pair<long long, long long> y) {
if (x.first > y.first) {
return x;
}
else if (x.first < y.first) {
return y;
}
else {
return {x.first, x.second + y.second};
}
}
struct chunk {
int sz = 0;
long long s1 = 0;
long long s2 = 0;
void add(int x) {
sz++;
s1 += x;
s2 += 1LL * x * x;
}
void rem(int x) {
sz--;
s1 -= x;
s2 -= 1LL * x * x;
}
long long get1() {
return s1;
}
long long get2() {
return (s1 * s1 - s2) / 2;
}
};
struct group {
map<int, chunk, greater<int>> f;
void add(pair<int, int> x) {
f[x.first].add(x.second);
}
void rem(pair<int, int> x) {
auto it = f.find(x.first);
it->second.rem(x.second);
if (it->second.sz == 0) {
f.erase(it);
}
}
pair<int, int> best() {
return {f.begin()->first, f.begin()->second.s1};
}
void calc() {
auto it = f.begin();
if (it == f.end()) {
return;
}
int x1 = it->first;
if (x1 == 0) {
return;
}
chunk &p1 = it->second;
if (p1.sz >= 3) {
res = max(res, make_pair(1LL * x1 * (x1 + x1), p1.get2()));
return;
}
it++;
if (it == f.end()) {
return;
}
int x2 = it->first;
if (x2 == 0) {
return;
}
chunk &p2 = it->second;
if (p1.sz >= 2) {
res = max(res, make_pair(1LL * x1 * (x1 + x2), p1.get1() * p2.get1()));
return;
}
if (p2.sz >= 2) {
res = max(res, make_pair(1LL * x1 * (x2 + x2), p2.get2()));
return;
}
it++;
if (it == f.end()) {
return;
}
int x3 = it->first;
if (x3 == 0) {
return;
}
chunk &p3 = it->second;
res = max(res, make_pair(1LL * x1 * (x2 + x3), p2.get1() * p3.get1()));
return;
}
};
group g[ms];
void dfs1(int u, int pr) {
g[u].add({0, 1});
for (auto v: adj[u]) {
if (v != pr) {
dfs1(v, u);
g[u].add(g[v].best() + 1);
}
}
}
void dfs2(int u, int pr) {
g[u].calc();
for (auto v: adj[u]) {
if (v != pr) {
g[u].rem(g[v].best() + 1);
g[v].add(g[u].best() + 1);
dfs2(v, u);
g[v].rem(g[u].best() + 1);
g[u].add(g[v].best() + 1);
}
}
}
void just_do_it() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(1, -1);
dfs2(1, -1);
cout << res.first << " " << res.second;
}
// END MAIN CODE
Compilation message (stderr)
road.cpp: In function 'void file_io(std::string, std::string)':
road.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(fi.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
road.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(fo.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |