#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 500005
struct Edge{
int u, v;
Edge(){}
Edge(int _u, int _v): u(_u), v(_v){}
int other(int x){
return (x ^ u ^ v);
}
} edge[MAX];
vector<int> G[MAX];
int numNode;
struct DP{
int max_value, cnt;
DP():max_value(0), cnt(1){}
DP(int _max_value, int _cnt): max_value(_max_value), cnt(_cnt){}
};
DP combine(DP a, DP b){
if(a.max_value > b.max_value) return a;
if(a.max_value < b.max_value) return b;
return DP(a.max_value, a.cnt + b.cnt);
}
DP res;
DP dp[MAX];
void dfs(int u, int p = -1){
for (int&i : G[u]){
int v = edge[i].other(u);
if(v ^ p){
dfs(v, u);
dp[u] = combine(dp[u], DP(dp[v].max_value + 1, dp[v].cnt));
}
}
}
void reroot(int u, int p = -1){
sort(G[u].begin(), G[u].end(), [&](const int&a, const int&b){
return dp[a].max_value > dp[b].max_value;
});
if((int)G[u].size() >= 3){
int node[3];
node[0] = dp[edge[G[u][0]].other(u)].max_value + 1, node[1] = dp[edge[G[u][1]].other(u)].max_value + 1, node[2] = dp[edge[G[u][2]].other(u)].max_value + 1;
DP cand = DP(node[0] * (node[1] + node[2]), 0);
int sum = 0;
for (int&i : G[u]) {
int v = edge[i].other(u);
if(dp[v].max_value + 1 == node[2]) cand.cnt += sum * dp[v].cnt;
if(dp[v].max_value + 1 == node[1]) sum += dp[v].cnt;
}
res = combine(res, cand);
}
DP mx[2];
mx[1] = DP(0, 0);
for (int&i : G[u]) {
int v = edge[i].other(u);
if(dp[v].max_value + 1 > mx[0].max_value){
mx[1] = mx[0];
mx[0] = DP(dp[v].max_value + 1, dp[v].cnt);
}
else if(dp[v].max_value + 1 == mx[0].max_value) mx[0].cnt += dp[v].cnt;
else if(dp[v].max_value + 1 > mx[1].max_value) mx[1] = DP(dp[v].max_value + 1, dp[v].cnt);
else if(dp[v].max_value + 1 == mx[1].max_value) mx[1].cnt += dp[v].cnt;
}
DP rem = dp[u];
for (int&i : G[u]) {
int v = edge[i].other(u);
if(v ^ p){
dp[u] = mx[0];
if(dp[v].max_value + 1 == dp[u].max_value){
if(dp[v].cnt == dp[u].cnt){
dp[u] = mx[1];
}
else{
dp[u].cnt -= dp[v].cnt;
}
}
reroot(v, u);
}
}
dp[u] = rem;
}
void process(void){
cin >> numNode;
for (int i = 1; i < numNode; ++i){
cin >> edge[i].u >> edge[i].v;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
dfs(1);
reroot(1);
cout << res.max_value << " " << res.cnt;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21128 KB |
Output is correct |
4 |
Correct |
4 ms |
21072 KB |
Output is correct |
5 |
Correct |
5 ms |
21160 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Incorrect |
4 ms |
21072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21128 KB |
Output is correct |
4 |
Correct |
4 ms |
21072 KB |
Output is correct |
5 |
Correct |
5 ms |
21160 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Incorrect |
4 ms |
21072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21072 KB |
Output is correct |
2 |
Correct |
4 ms |
21072 KB |
Output is correct |
3 |
Correct |
4 ms |
21128 KB |
Output is correct |
4 |
Correct |
4 ms |
21072 KB |
Output is correct |
5 |
Correct |
5 ms |
21160 KB |
Output is correct |
6 |
Correct |
4 ms |
21072 KB |
Output is correct |
7 |
Incorrect |
4 ms |
21072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |