11020 | 2024-06-06 16:02:30 | gortomi | A Day in Olbia | cpp17 | Runtime error 0/100 | 825ms | 17120 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 | 484 KiB | ||||
5 | Runtime error | 3ms | 576 KiB | ||||
6 | Runtime error | 3ms | 484 KiB | ||||
subtask3 | 0/16 | ||||||
7 | Runtime error | 3ms | 484 KiB | ||||
8 | Runtime error | 4ms | 792 KiB | ||||
9 | Runtime error | 4ms | 612 KiB | ||||
10 | Runtime error | 4ms | 516 KiB | ||||
11 | Runtime error | 4ms | 764 KiB | ||||
12 | Runtime error | 4ms | 740 KiB | ||||
13 | Runtime error | 4ms | 740 KiB | ||||
14 | Runtime error | 4ms | 888 KiB | ||||
15 | Runtime error | 4ms | 612 KiB | ||||
subtask4 | 0/16 | ||||||
16 | Accepted | 128ms | 9188 KiB | ||||
17 | Wrong answer | 136ms | 9828 KiB | ||||
subtask5 | 0/52 | ||||||
18 | Wrong answer | 609ms | 9156 KiB | ||||
19 | Wrong answer | 514ms | 8736 KiB | ||||
20 | Wrong answer | 825ms | 17080 KiB | ||||
21 | Accepted | 809ms | 14376 KiB | ||||
22 | Wrong answer | 735ms | 14880 KiB | ||||
23 | Wrong answer | 755ms | 15388 KiB | ||||
24 | Wrong answer | 748ms | 16156 KiB | ||||
25 | Wrong answer | 237ms | 17052 KiB | ||||
26 | Wrong answer | 282ms | 17052 KiB | ||||
27 | Wrong answer | 342ms | 17120 KiB | ||||
28 | Wrong answer | 648ms | 17120 KiB | ||||
29 | Wrong answer | 717ms | 17116 KiB | ||||
30 | Wrong answer | 736ms | 17116 KiB | ||||
31 | Wrong answer | 728ms | 17052 KiB |