110202024-06-06 16:02:30gortomiA Day in Olbiacpp17Runtime error 0/100825ms17120 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 error3ms484 KiB
5Runtime error3ms576 KiB
6Runtime error3ms484 KiB
subtask30/16
7Runtime error3ms484 KiB
8Runtime error4ms792 KiB
9Runtime error4ms612 KiB
10Runtime error4ms516 KiB
11Runtime error4ms764 KiB
12Runtime error4ms740 KiB
13Runtime error4ms740 KiB
14Runtime error4ms888 KiB
15Runtime error4ms612 KiB
subtask40/16
16Accepted128ms9188 KiB
17Wrong answer136ms9828 KiB
subtask50/52
18Wrong answer609ms9156 KiB
19Wrong answer514ms8736 KiB
20Wrong answer825ms17080 KiB
21Accepted809ms14376 KiB
22Wrong answer735ms14880 KiB
23Wrong answer755ms15388 KiB
24Wrong answer748ms16156 KiB
25Wrong answer237ms17052 KiB
26Wrong answer282ms17052 KiB
27Wrong answer342ms17120 KiB
28Wrong answer648ms17120 KiB
29Wrong answer717ms17116 KiB
30Wrong answer736ms17116 KiB
31Wrong answer728ms17052 KiB