5分求助……
查看原帖
5分求助……
75954
AuCloud楼主2020/10/15 11:21

调了一上午了

#include <bits/stdc++.h>
using namespace std;
int head[100001], nxt[200001], to[200001], tot;
int fa[100001], dep[100001], sze[100001], son[100001], top[100001], ans[100001], zh;
struct hehe{
    int x, y, z, num;
}q[100001];
struct tree{
    int mx, ls, rs, id;
}tr[8000001];
int tot2, rt[100001];
int num[100001];
void tiaoshi(int p, int l, int r)
{
    if(l == r) cout << tr[p].id << ' ' << tr[p].mx << endl;
    int mid = (l + r) >> 1;
    if(tr[p].ls) tiaoshi(tr[p].ls, l, mid);
    if(tr[p].rs) tiaoshi(tr[p].rs, mid + 1, r);
}
void up(int p)
{
    if(tr[tr[p].ls].mx > tr[tr[p].rs].mx)
    {
        tr[p].id = tr[tr[p].ls].id;
        tr[p].mx = tr[tr[p].ls].mx;
    }
    else
    {
        tr[p].mx = tr[tr[p].rs].mx;
        tr[p].id = ((tr[tr[p].ls].mx == tr[tr[p].rs].mx) ? min(tr[tr[p].ls].id, tr[tr[p].rs].id) : tr[tr[p].rs].id);
    }
}
void merge(int &x, int y, int l, int r)
{
    if(l == r)
    {
//        cout << l << endl;
        tr[x].mx += tr[y].mx;
        return;
    }
    if(!x || !y)
    {
        if(!x) x = y;
        return;
    }
    int mid = (l + r) >> 1;
    merge(tr[x].ls, tr[y].ls, l, mid);
    merge(tr[x].rs, tr[x].rs, mid + 1, r);
    up(x);
}
void dfs3(int x)
{
//    cout << x << "eeeee\n";
//    tiaoshi(rt[3], 1, zh);
    for(int i = head[x]; i; i = nxt[i])
    {
        int y = to[i];
        if(y != fa[x])
        {
            dfs3(y);
            merge(rt[x], rt[y], 1, zh);
            /*if(x == 2)
            {
                cout << "dddddddddsb" << rt[x] << ' ' << tr[rt[x]].id << ' ' << tr[rt[x]].mx << endl;
            }*/
        }
    }
    ans[x] = tr[rt[x]].id;
}
void update(int l, int r, int x, int v, int &p)
{
    if(!p) p = ++tot2;
    if(l == r)
    {
        tr[p].mx += v;
        tr[p].id = l;
        return;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) update(l, mid, x, v, tr[p].ls);
    else update(mid + 1, r, x, v, tr[p].rs);
    up(p);
}
void add(int x, int y)
{
    nxt[++tot] = head[x];
    head[x] = tot;
    to[tot] = y;
}

void dfs1(int x, int from)
{
    fa[x] = from;
    dep[x] = dep[from] + 1;
    sze[x] = 1;
    for(int i = head[x]; i; i = nxt[i])
    {
        int y = to[i];
        if(y != from)
        {
//            cout << y << endl;
            dfs1(y, x);
            sze[x] += sze[y];
            if(sze[y] > sze[son[x]]) son[x] = y;
        }
    }
}
void dfs2(int x, int tp)
{
    top[x] = tp;
    if(!son[x]) return;
    dfs2(son[x], tp);
    for(int i = head[x]; i; i = nxt[i])
    {
        int y = to[i];
        if(y != son[x] && y != fa[x])
        {
            dfs2(y, y);
        }
    }
}
int LCA(int x, int y)
{
    while(top[x] != top[y])
    {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    return dep[x] > dep[y] ? y : x;
}
bool cmp(hehe x, hehe y)
{
    return x.z < y.z;
}
int main()
{
    int n, T;
    cin >> n >> T;
    for(int i = 1; i < n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        add(x, y);
        add(y, x);
    }
//    cout << "dsb\n";
    dfs1(1, -1);
//    cout << "dsb\n";
    dfs2(1, 1);
//    cout << "dsb\n";
    for(int i = 1; i <= T; i++)
    {
        scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].z);
        q[i].num = q[i].z;
    }
    sort(q + 1, q + T + 1, cmp);
    for(int i = 1; i <= T; i++)
    {
        q[i].z = (q[i].z == q[i - 1].num ? q[i - 1].z : q[i - 1].z + 1);
        num[q[i].z] = q[i].num;
    }
    zh = q[T].z;
    for(int i = 1; i <= T; i++)
    {
        int x = q[i].x, y = q[i].y, z = q[i].z;
        int lca = LCA(x, y);
//        cout << "qweqweqweqweqwe\n";
//        tiaoshi(rt[3], 1, zh);
//        cout << x << ' ' << y << ' ' << lca << ' ' << fa[lca] << ' ' << z << endl;
        update(1, zh, z, 1, rt[x]);
        update(1, zh, z, 1, rt[y]);
        update(1, zh, z, -1, rt[lca]);
        update(1, zh, z, -1, rt[fa[lca]]);
//        cout << tr[rt[2]].id << ' ' << tr[rt[2]].mx << endl;
    }
//    cout << "qwqwqwqwqwqwqwqwqwqwqwqwqwqwqwqwq" << fa[3] << endl;
    dfs3(1);
//    tiaoshi(rt[2], 1, zh);
    for(int i = 1; i <= n; i++)
    /*{
        cout << i << ' ' << tr[rt[i]].id << ' ' << tr[rt[i]].mx << endl;
    }*/
        printf("%d\n", num[ans[i]]);
    return 0;
}
2020/10/15 11:21
加载中...