이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int par[N], in[N], cnt[N], n, ans, res;
vector<int> adj[N];
bool maximize(int &a, const int b) {
if (a < b) {
a = b;
return true;
} return false;
}
void dfs(int u) {
cnt[u] = 1;
in[u] = 1;
for (auto v : adj[u]) {
if (v == par[u]) continue;
par[v] = u;
dfs(v);
if (maximize(in[u], in[v] + 1)) cnt[u] = 0;
if (in[u] == in[v] + 1) {
cnt[u] += cnt[v];
}
}
}
void dp(int u, int out, int outc) {
vector<ii> vt;
if (u != 1)
vt.emplace_back(out, outc);
for (auto v : adj[u]) {
if (v == par[u]) continue;
vt.emplace_back(in[v], cnt[v]);
}
sort(vt.begin(), vt.end(), greater<ii>());
if (vt.size() >= 3 && (vt[0].fi * (vt[1].fi + vt[2].fi)) >= res) {
int curres = (vt[0].fi * (vt[1].fi + vt[2].fi)), curcnt = 0;
int ba = 0;
for (auto v : vt) {
if (v.fi == vt[2].fi) ba += v.se;
}
if (vt[1].fi == vt[2].fi) {
int cnt = 0;
for (auto tmp : vt) {
int x = tmp.fi, num = tmp.se;
if (x == vt[1].fi) {
curcnt += cnt * num;
cnt += num;
}
}
}
else if (vt[0].fi == vt[1].fi) {
curcnt = (vt[0].se + vt[1].se) * ba;
}
else {
curcnt = vt[1].se * ba;
}
if (maximize(res, curres)) ans = 0;
ans += curcnt;
}
int mx1 = vt[0].fi, mx1c = 0, mx2 = 0, mx2c = 1;
if (vt.size() >= 2) mx2 = vt[1].fi, mx2c = 0;
for (auto tmp : vt) {
int v = tmp.fi, num = tmp.se;
if (v == mx1) mx1c += num;
if (v == mx2) mx2c += num;
}
for (auto v : adj[u]) {
if (v == par[u]) continue;
if (in[v] == mx1) {
if (cnt[v] == mx1c) dp(v, mx2 + 1, mx2c);
else dp(v, mx1 + 1, mx1c - cnt[v]);
}
else dp(v, mx1 + 1, mx1c);
}
}
void process() {
cin >> n;
ans = 1;
foru(i,1,n-1) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int flag = 1, cnt = 0;
foru(i,1,n) {
if (adj[i].size() > 2) flag = 0;
if (adj[i].size() == 1) cnt++;
}
dfs(1);
dp(1, 0, 1);
cout << res << ' ' << ans << '\n';
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
// dont stop
컴파일 시 표준 에러 (stderr) 메시지
road.cpp: In function 'void process()':
road.cpp:106:9: warning: variable 'flag' set but not used [-Wunused-but-set-variable]
106 | int flag = 1, cnt = 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... |