MLE求调
查看原帖
MLE求调
1036180
ChampionCyan楼主2024/6/10 21:17
#include <bits/stdc++.h>
#define MAXN 1000001
using namespace std;
vector<int> e[MAXN];
int mina[MAXN], ans[MAXN];
bool vis[MAXN];
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    while (m--) {
        int x, y;
        scanf("%d%d", &x, &y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
        mina[i] = INT_MAX;
    struct node {
        int v, t;
    };
    queue<node> q;
    q.push({1, 0});
    while (!q.empty()) {
        int v = q.front().v;
        int t = q.front().t;
        t %= 100003;
        q.pop();
        vis[v] = true;
        if (mina[v] == t) 
            ans[v]++;
        else if (mina[v] > t) {
            mina[v] = t;
            ans[v] = 1;
        } else 
            continue;
        for (int i : e[v])
            if (!vis[i]) q.push({i, t + 1});
    }
    for (int i = 1; i <= n; i++)
        if (ans[i] > 100003) 
            printf("0\n");
        else 
            printf("%d\n", ans[i]);
    return 0;
}
2024/6/10 21:17
加载中...