/***Farhan132***/
// #pragma GCC optimize("Ofast,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll, ll, ll > tp;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clz(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
const ll mod = (ll) 998244353;
// const ll mod = (ll) 1e9 + 7;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)2e18;
// ll dx[]={0,1,0,-1};
// ll dy[]={1,0,-1,0};
// ll dxx[]={0,1,0,-1,1,1,-1,-1};
// ll dyy[]={1,0,-1,0,1,-1,1,-1};
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 5e5 + 5;
ll dist[N], c[N];
vector < ll > v[N];
void dfs(ll s, ll p){
dist[s] = 0; c[s] = 1;
for(auto u : v[s]){
if(u - p){
dfs(u, s);
if(dist[u] + 1 > dist[s]){
dist[s] = dist[u] + 1;
c[s] = c[u];
}else{
if(dist[u] + 1 == dist[s]) c[s] += c[u];
}
}
}
return;
}
ii ans = {0, 1};
ll knap(vector < ll > c, ll k){
ll S = 0, ans = 0;
for(auto u : c){
ans -= (u * (u - 1)) / 2;
S += u;
}
ans += (S * (S - 1)) / 2;
return ans;
}
void calc(ll s, ll p, ii best){
vector < ii > paths = {best};
for(auto u : v[s]){
if(u - p) paths.pb({dist[u] + 1, c[u]});
}
sort(paths.rbegin(), paths.rend());
if(paths.size() >= 3){
ll res = (paths[0].ff * (paths[1].ff + paths[2].ff));
ll cnt = 0;
if(paths[0].ff == paths[2].ff){
vector < ll > C;
for(auto [x, y] : paths){
if(x == paths[0].ff) C.pb(y);
}
cnt = knap(C, 2);
}else{
if(paths[0].ff == paths[1].ff){
for(auto [x, y] : paths){
if(x == paths[2].ff) cnt += (paths[0].ss + paths[1].ss) * y;
}
}else{
if(paths[1].ff == paths[2].ff){
vector < ll > C;
for(auto [x, y] : paths){
if(x == paths[1].ff) C.pb(y);
}
cnt = knap(C, 2);
}else{
for(auto [x, y] : paths){
if(x == paths[2].ff) cnt += (paths[1].ss) * y;
}
}
}
}
if(res > ans.ff){
ans = {res, cnt};
}else{
if(res == ans.ff) ans.ss += cnt;
}
}
ll c1 = 0, c2 = 0;
for(auto [x, y] : paths){
if(paths.size() > 0 && paths[0].ff == x) c1 += y;
if(paths.size() > 1 && paths[1].ff == x) c2 += y;
}
for(auto u : v[s]){
if(u - p){
if(paths[0].ff == dist[u] + 1){
if(c[u] == c1){
calc(u, s, {paths[1].ff + 1, c2});
} else {
calc(u, s, {paths[0].ff + 1, c1 - c[u]});
}
}else{
calc(u, s, {paths[0].ff + 1, c1});
}
}
}
return;
}
void solve(){
ll n; cin >> n;
for(ll i = 1; i < n; i++){
ll x, y; cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
dfs(1, 0);
calc(1, 0, {0, 1});
cout << ans.ff << ' ' << ans.ss << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
auto start_time = clock();
#else
// freopen("subsequence.in", "r", stdin);
// freopen("subsequence.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//precalc();
ll T = 1, CT = 0;// cin >> T;
while(T--){
// cout << "Case #" << ++CT << ": ";
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
#endif
return 0;
}
Compilation message
road.cpp: In function 'int main()':
road.cpp:171:15: warning: unused variable 'CT' [-Wunused-variable]
171 | ll T = 1, CT = 0;// cin >> T;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11996 KB |
Output is correct |
4 |
Incorrect |
5 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11996 KB |
Output is correct |
4 |
Incorrect |
5 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
11996 KB |
Output is correct |
4 |
Incorrect |
5 ms |
11988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |