T了一个点求救
查看原帖
T了一个点求救
94365
Southern_way楼主2019/7/15 10:40

本机测发现是第一个dfs运行到3w多层的时候不动了 不知道为什么

#include<bits/stdc++.h>
#define RG register
#define LL long long
#define U(x, y, z) for (RG int x = y; x <= z; ++x)
#define D(x, y, z) for (RG int x = y; x >= z; --x)
#define N 300010
using namespace std;
template <typename T> void read(T &n){ bool f = 1; char ch = getchar(); n = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = 0; for (; isdigit(ch); ch = getchar()) n = (n << 1) + (n << 3) + (ch ^ 48); if (f == 0) n = -n;}
inline char Getchar(){ char ch; for (ch = getchar(); !isalpha(ch); ch = getchar()); return ch;}
template <typename T> inline void write(T n){ char ch[60]; bool f = 1; int cnt = 0; if (n < 0) f = 0, n = -n; do{ch[++cnt] = char(n % 10 + 48); n /= 10; }while(n); if (f == 0) putchar('-'); for (; cnt; cnt--) putchar(ch[cnt]);}
template <typename T> inline void writeln(T n){write(n); putchar('\n');}
template <typename T> inline void writesp(T n){write(n); putchar(' ');}
template <typename T> inline void chkmin(T &x, T y){x = x < y ? x : y;}
template <typename T> inline void chkmax(T &x, T y){x = x > y ? x : y;}
template <typename T> inline T Min(T x, T y){return x < y ? x : y;}
template <typename T> inline T Max(T x, T y){return x > y ? x : y;}
inline void FO(string s){freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout);}
int n, m, cnt, fa[N][31], lca0[N], deepth[N], head[N], up[N], w[N], S[N << 1], T[N << 1], ans[N], dn[N << 1], dis[N], lg[N];
struct edge{
    int v, nxt;
}e[N << 1];
vector <int> su[N], sd[N], tu[N], td[N];
inline void add_edge(int u, int v){
    ++cnt;
    e[cnt].v = v;
    e[cnt].nxt = head[u];
    head[u] = cnt;
}
inline void dfs(int u, int f){
	deepth[u] = deepth[f] + 1;
    fa[u][0] = f;
    for (RG int i = 1; (1 << i) <= deepth[u]; i++)
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    for (RG int i = head[u]; i; i = e[i].nxt)
        if (e[i].v != f)
            dfs(e[i].v, u);
}
inline int lca(int u,int v){
    if (deepth[u] < deepth[v])
        swap(u, v);
    while (deepth[u] > deepth[v])
        u = fa[u][lg[deepth[u] - deepth[v]] - 1];
    if (u == v) return u;
    for (int high = lg[deepth[u]] - 1; high >= 0; --high)
        if (fa[u][high] != fa[v][high])
            u = fa[u][high], v = fa[v][high];
    return fa[u][0];
}
inline void dfs1(int u, int f){
    int afu = up[deepth[u] + w[u]], afd = dn[deepth[u] - w[u] + N];
    for (RG int i = 0; i < su[u].size(); ++i)
    	++up[deepth[u]];
    for (RG int i = 0; i < td[u].size(); ++i)
    	++dn[deepth[u] - dis[td[u][i]] + N];
    for (RG int i = head[u]; i; i = e[i].nxt){
        int v = e[i].v;
        if (v != f) dfs1(v, u);
    }
    ans[u] += up[deepth[u] + w[u]] - afu + dn[deepth[u] - w[u] + N] - afd;
    for (RG int i = 0; i < tu[u].size(); ++i){
    	--up[deepth[S[tu[u][i]]]];
    //	if (u == 1) cout << S[tu[u][i]] << endl;
    	if (lca0[tu[u][i]] == u && deepth[S[tu[u][i]]] == deepth[u] + w[u]) --ans[u];
	}
    for (RG int i = 0; i < sd[u].size(); ++i)
    	--dn[deepth[T[sd[u][i]]] - dis[sd[u][i]] + N];
}
int main(){
    read(n), read(m);
        int u, v;
    U(i, 1, n - 1){
        read(u), read(v);
        add_edge(u, v);
        add_edge(v, u);
    }
    deepth[0] = -1;
    dfs(1, 0);
    U(i, 1, n)
        read(w[i]);
    U(i, 1, n)
        lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
    U(i, 1, m){
        int no = i << 1;
        read(S[no - 1]), read(T[no]);
        int zz = lca(S[no - 1], T[no]);
        T[no - 1] = zz;
        S[no] = zz;
        su[S[no - 1]].push_back(no - 1);
        tu[zz].push_back(no - 1);
        sd[zz].push_back(no);
        td[T[no]].push_back(no);
        lca0[no - 1] = lca0[no] = zz;
        dis[no] = deepth[S[no - 1]] + deepth[T[no]] - 2 * deepth[zz];
    }
    dfs1(1, 1);
    U(i, 1, n) writesp(ans[i]);
    return 0;
}
2019/7/15 10:40
加载中...