110192024-06-06 16:02:25gortomiA Day in Olbiapython3Runtime error 0/10018ms3096 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";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Runtime error17ms2916 KiB
2Runtime error17ms2916 KiB
subtask20/16
3Runtime error17ms3096 KiB
4Runtime error17ms2812 KiB
5Runtime error17ms2916 KiB
6Runtime error17ms2916 KiB
subtask30/16
7Runtime error17ms3044 KiB
8Runtime error17ms3048 KiB
9Runtime error17ms2960 KiB
10Runtime error17ms2916 KiB
11Runtime error17ms2852 KiB
12Runtime error18ms2764 KiB
13Runtime error17ms2908 KiB
14Runtime error17ms2936 KiB
15Runtime error17ms3044 KiB
subtask40/16
16Runtime error17ms2844 KiB
17Runtime error17ms2820 KiB
subtask50/52
18Runtime error17ms2916 KiB
19Runtime error17ms2916 KiB
20Runtime error17ms2916 KiB
21Runtime error17ms2844 KiB
22Runtime error17ms2936 KiB
23Runtime error17ms2864 KiB
24Runtime error17ms2952 KiB
25Runtime error17ms2936 KiB
26Runtime error17ms2916 KiB
27Runtime error17ms2916 KiB
28Runtime error17ms2824 KiB
29Runtime error17ms2916 KiB
30Runtime error17ms2916 KiB
31Runtime error17ms2936 KiB