求助,为什么会RE
  • 板块学术版
  • 楼主SakurajiamaMai
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/10/29 13:47
  • 上次更新2023/11/2 10:54:37
查看原帖
求助,为什么会RE
784813
SakurajiamaMai楼主2023/10/29 13:47
#include <bits/stdc++.h>
using namespace std;

const int N = 4e5 + 10;

int n, m;
int h[N], e[N], ne[N], idx, dist[N], p[N];
int h1[N], e1[N], ne1[N];

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

void add1(int a, int b) {
    e1[idx] = b, ne1[idx] = h1[a], h1[a] = idx++;
}

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

void dfs(int u) {
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (find(j) != find(u)) {
            p[find(j)] = find(u);
            dfs(j);
        }
    }
}

void dfs1(int u, int fa) {
    for (int i = h1[u]; ~i; i = ne1[i]) {
        int j = e1[i];
        if (j != fa) {
            dist[j]++;
            dfs1(j, u);
        }
    }
}

int main() {
    cin >> n >> m;
    memset(h, -1, sizeof h);

    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v);
        add(v, u);
    }

    int t;
    cin >> t;

    for (int i = 1; i <= n; i++)
        p[i] = i;

    for (int i = 1; i <= n; i++)
        dfs(i);

    idx = 0;
    memset(h1, -1, sizeof h1);

    while (t--) {
        int a, b;
        cin >> a >> b;
        if (p[a] == p[b]) {
            add1(a, b);
            add1(b, a);
        }
    }

    for (int i = 1; i <= n; i++) {
        dist[i]++;
        dfs1(i, 0);
    }

    int res = 0, ans = 1e9;

    for (int i = 1; i <= n; i++) {
        res = max(res, dist[i]);
        ans = min(ans, dist[i]);
    }

    cout << res << ' ' << ans;
    return 0;
}

n和m都是在2e5

2023/10/29 13:47
加载中...