이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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
vector<pair<int, 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 (pair<int, int>&x : G[u]){
int v = x.first;
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 pair<int, int>&a, const pair<int, int>&b){
return dp[a.first].max_value > dp[b.first].max_value;
});
if((int)G[u].size() >= 3){
int node[3];
node[0] = dp[G[u][0].first].max_value + 1, node[1] = dp[G[u][1].first].max_value + 1, node[2] = dp[G[u][2].first].max_value + 1;
DP cand = DP(node[0] * (node[1] + node[2]), 0);
int sum = 0;
for (pair<int, int>&x : G[u]) {
int v = x.first;
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 (pair<int, int>&x : G[u]) {
int v = x.first;
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 (pair<int, int>&x : G[u]) {
int v = x.first;
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){
int u, v; cin >> u >> v;
G[u].emplace_back(v, 1);
G[v].emplace_back(u, 1);
}
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 |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |