110212024-06-06 16:03:46gortomiA Day in Olbiacpp17Runtime error 0/100865ms16072 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 error3ms616 KiB
2Runtime error3ms484 KiB
subtask20/16
3Runtime error3ms484 KiB
4Runtime error3ms636 KiB
5Runtime error3ms484 KiB
6Runtime error3ms648 KiB
subtask30/16
7Runtime error4ms532 KiB
8Runtime error4ms544 KiB
9Runtime error4ms632 KiB
10Runtime error4ms592 KiB
11Runtime error4ms760 KiB
12Runtime error4ms604 KiB
13Runtime error4ms612 KiB
14Runtime error4ms600 KiB
15Runtime error3ms612 KiB
subtask40/16
16Accepted155ms8080 KiB
17Wrong answer160ms8676 KiB
subtask50/52
18Wrong answer634ms8028 KiB
19Wrong answer597ms7724 KiB
20Wrong answer865ms15944 KiB
21Accepted845ms13224 KiB
22Wrong answer779ms13852 KiB
23Wrong answer792ms14236 KiB
24Wrong answer765ms15040 KiB
25Wrong answer256ms15900 KiB
26Wrong answer273ms15900 KiB
27Wrong answer375ms15972 KiB
28Wrong answer624ms15964 KiB
29Wrong answer718ms15968 KiB
30Wrong answer723ms16072 KiB
31Wrong answer736ms15900 KiB