为什么全MLE了呀???
查看原帖
为什么全MLE了呀???
817044
cjwdyzxfblzs楼主2023/3/8 19:33

这是俺的代码

/*
思路:
    最大生成树 + LCA板子
    去掉边权小的边,构造成一棵树,树上两个节点都只有唯一的路径
    取 lca(x, y) 路上的 较小值即可
*/
#include <bits/stdc++.h>

using namespace std;

//const int M = 5e4;
const int N = 5e4;

int n, m;
int h[N], e[N], ne[N], w[N], idx;
int p[N], lg[N], vis[N];
int depth[N];
int dis[N][50], f[N][50];

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

inline int find(int x)
{
    if (p[x] != x)
        x = p[x] = find(p[x]);
    return p[x];
}

struct MAX_TREE
{
    int u; int v; int w;
} tr[N];

bool cmp(MAX_TREE a, MAX_TREE b)
{
    return a.w > b.w;
}

void kruskal()
{
    int num = 0;
    sort(tr + 1, tr + m + 1, cmp);
    for (int i = 1; i <= m; i ++ )
    {
        int a = tr[i].u, b = tr[i].v, w = tr[i].w;
        int pa = find(a), pb = find(b);
        
        if (pa == pb) continue;
        
        p[a] = pb;
        add(a, b, w), add(b, a, w);
        if ( ++ num == n)
            return;
    }
}

void DFS(int x, int father, int we)
{
    //cout << 114514 << endl;
    //if (vis[x]) return;
    vis[x] = 1;
    depth[x] = depth[father] + 1;
    f[x][0] = father;
    dis[x][0] = we;

    for (int i = 1; (1 << i) <= depth[x]; i ++ )
    {
        f[x][i] = f[f[x][i - 1]][i - 1];
        dis[x][i] = min(dis[x][i - 1], dis[f[x][i - 1]][i - 1]);
    }

    for (int i = h[x]; i != -1; i = ne[i])
        if (e[i] != father)
            DFS(e[i], x, w[e[i]]);
        
    return;
}

int LCA(int x, int y)
{
    if (find(x) != find(y))
        return -1;
    int ans = 19260815;
    if (depth[x] < depth[y])
        swap(x, y);

    while (depth[x] > depth[y])
    {
        ans = min(ans, dis[x][lg[depth[x] - depth[y]] - 1]);
        x = f[x][lg[depth[x] - depth[y]] - 1];
    }

    if (x == y)
        return ans;

    for (int i = lg[depth[x]] - 1; i >= 0; i--)
    {
        if (f[x][i] != f[y][i])
        {
            ans = min(ans, dis[x][i]);
            ans = min(ans, dis[y][i]);
            x = f[x][i];
            y = f[y][i];
        }
    }

    ans = min(ans, min(dis[x][0], dis[y][0]));
    return ans;
}

void init()
{
    memset(h, -1, sizeof(h));
    for (int i = 1; i <= n; i ++ )
        p[i] = i;
    for (int i = 1; i <= n; i ++ )
        lg[i] = lg[i - 1] + (1 << lg[i - 1] == 1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);

    cin >> n >> m;

    init();
//cout << "NO PROBLEM" << endl;
    for (int i = 1; i <= m; i ++ )
        cin >> tr[i].u >> tr[i].v >> tr[i].w;
    
//cout << "NO PROBLEM" << endl;
    kruskal();
    // sort(tr + 1, tr + m + 1, cmp);
//cout << "NO PROBLEM" << endl;
    for (int i = 1; i <= n; i ++ )
        if (!vis[i])
            DFS(i, 0, 0);
    

//cout << "NO PROBLEM" << endl;
    int q;
    cin >> q;
    for (int i = 1; i <= q; i ++ )
    {
        int x, y;
        cin >> x >> y;
        cout << LCA(x, y) << endl;
       // cout << "NO PROBLEM" << endl;
    }

    return 0;
}
2023/3/8 19:33
加载中...