用点分的模板写的p3806,把边都改成了1,为什么wa了
查看原帖
用点分的模板写的p3806,把边都改成了1,为什么wa了
745640
ssjl楼主2022/7/26 16:24
#include <iostream>
#include <cstring>
#include <vector>
#define int long long
using namespace std;

const int N = 5e4 + 10;

int n, k;
int h[N], ne[2*N], e[2*N], w[2*N], idx;
int root, nt, mx[N], siz[N], dis[N];
int d[N], cnt, hav[N];
bool vis[N];
int ans;

inline void add(int a,int b, int v)
{
    e[idx] = b, ne[idx] = h[a];
    w[idx] = v, h[a] = idx ++;
}

inline void getroot(int x, int pre)
{
    mx[x] = 0, siz[x] = 1;
    for(int i = h[x]; ~i; i = ne[i])
    {
        int y = e[i];
        if(y != pre && !vis[y])
        {
            getroot(y, x);
            siz[x] += siz[y];
            mx[x] = max(mx[x], siz[y]);
        }
    }
    mx[x] = max(mx[x], nt - siz[x]);
    if(mx[x] < mx[root]) root = x;
}

inline void getdis(int x, int pre)
{
    d[++ cnt] = dis[x];
    for(int i = h[x]; ~i; i = ne[i])
    {
        int y = e[i];
        if(y != pre && !vis[y])
        {
            dis[y] = dis[x] + w[i];
            getdis(y, x);
        }
    }
}

inline void calc(int x)
{
    hav[0] = 1;
    vector<int> v;
    for(int i = h[x]; ~i; i = ne[i])
    {
        int y = e[i];
        if(!vis[y])
        {
            cnt = 0;
            dis[y] = w[i];
            getdis(y, x);
            for(int j = 1; j <= cnt; j ++)
                if(k >= d[j])
                    ans += hav[k - d[j]];
            for(int j = 1; j <= cnt; j ++)
            {
                v.push_back(d[j]);
                hav[d[j]] = 1;
            }
        }
    }
    for(auto t : v)
        hav[t] = 0;
}

inline void dfz_(int x)
{
    calc(x);
    vis[x] = 1;
    for(int i = h[x]; ~i; i = ne[i])
    {
        int y = e[i];
        if(!vis[y])
        {
            root = 0;
            nt = siz[y];
            getroot(y, x), getroot(root, 0);
            dfz_(root);
        }
    }
}

signed main()
{
    memset(h, -1, sizeof h);
    
    cin >> n >> k;
    
    for(int i = 1, x, y; i < n && cin >> x >> y; i ++)
        add(x, y ,1), add(y, x, 1);
    
    mx[0] = nt = n;
    
    getroot(1, 0), getroot(root, 0);
    
    dfz_(root);
    
    cout << ans << '\n';
    
    return 0;
}
2022/7/26 16:24
加载中...