11021 | 2024-06-06 16:03:46 | gortomi | A Day in Olbia | cpp17 | Runtime error 0/100 | 865ms | 16072 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<vector<int> > g;
vector<bool> vis;
vector<int> s;
const int e = 100000;
int db[200005];
ll f, m;
ll ans = 0;
void calc(int v, int p)
{
s[v] = 1;
for(auto x : g[v])
{
if(x > m) break;
if(vis[x] || x == p) continue;
calc(x, v);
s[v] += s[x];
}
}
int findc(int v, int p, int bp)
{
for(auto x : g[v])
{
if(x > m) break;
if(x == p || vis[x]) continue;
if(s[x] > s[bp] / 2) return findc(x, v, bp);
}
return v;
}
vector<ll> act;
void dfs(int v, int p, int dep)
{
for(auto x : g[v])
{
if(x > m) break;
if(vis[x] || x == p) continue;
dfs(x, v, dep + 1);
}
ll mul = dep - v * f;
if(abs(mul) <= e)
{
ans += db[e - mul];
act.push_back(mul + e);
}
}
void dec(int v)
{
calc(v, -1);
int c = findc(v, -1, v);
vis[c] = 1;
vector<int> fi;
db[e - v * f]++;
for(auto x : g[c])
{
act.clear();
if(x > m) break;
if(vis[x]) continue;
dfs(x, -1, 1);
for(auto y : act) db[y]++, fi.push_back(y);
}
db[e - f * v]--;
for(auto x : fi) db[x]--;
for(auto x : g[c])
{
if(x > m) break;
if(vis[x]) continue;
dec(x);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
g.resize(n);
vis.resize(n);
s.resize(n);
for(int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i = 0; i < n; i++)
{
sort(g[i].begin(), g[i].end());
}
for(f = 1; f < n; f++)
{
m = n / f;
for(int j = 0; j <= m; j++)
{
if(!vis[j]) dec(j);
vis[j] = 0;
}
//cout << ans << "\n";
}
cout << ans << "\n";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Runtime error | 3ms | 616 KiB | ||||
2 | Runtime error | 3ms | 484 KiB | ||||
subtask2 | 0/16 | ||||||
3 | Runtime error | 3ms | 484 KiB | ||||
4 | Runtime error | 3ms | 636 KiB | ||||
5 | Runtime error | 3ms | 484 KiB | ||||
6 | Runtime error | 3ms | 648 KiB | ||||
subtask3 | 0/16 | ||||||
7 | Runtime error | 4ms | 532 KiB | ||||
8 | Runtime error | 4ms | 544 KiB | ||||
9 | Runtime error | 4ms | 632 KiB | ||||
10 | Runtime error | 4ms | 592 KiB | ||||
11 | Runtime error | 4ms | 760 KiB | ||||
12 | Runtime error | 4ms | 604 KiB | ||||
13 | Runtime error | 4ms | 612 KiB | ||||
14 | Runtime error | 4ms | 600 KiB | ||||
15 | Runtime error | 3ms | 612 KiB | ||||
subtask4 | 0/16 | ||||||
16 | Accepted | 155ms | 8080 KiB | ||||
17 | Wrong answer | 160ms | 8676 KiB | ||||
subtask5 | 0/52 | ||||||
18 | Wrong answer | 634ms | 8028 KiB | ||||
19 | Wrong answer | 597ms | 7724 KiB | ||||
20 | Wrong answer | 865ms | 15944 KiB | ||||
21 | Accepted | 845ms | 13224 KiB | ||||
22 | Wrong answer | 779ms | 13852 KiB | ||||
23 | Wrong answer | 792ms | 14236 KiB | ||||
24 | Wrong answer | 765ms | 15040 KiB | ||||
25 | Wrong answer | 256ms | 15900 KiB | ||||
26 | Wrong answer | 273ms | 15900 KiB | ||||
27 | Wrong answer | 375ms | 15972 KiB | ||||
28 | Wrong answer | 624ms | 15964 KiB | ||||
29 | Wrong answer | 718ms | 15968 KiB | ||||
30 | Wrong answer | 723ms | 16072 KiB | ||||
31 | Wrong answer | 736ms | 15900 KiB |