广搜它爆了!!!
查看原帖
广搜它爆了!!!
996897
linyuxi2014楼主2024/10/29 13:17

Problem:Subtask #1 TLE

Code:

#include <bits/stdc++.h>
#define N 1000005
#define oo 0x3f3f
#define mod 100003
using namespace std;
int n, m, x, y;
vector<int> a[N];
int f[N], num[N];

void bfs(){
    queue<int> q;
    q.push(1);
    f[1] = 0;
    num[1] = 1;
    while (!q.empty()){
        int head = q.front();
        q.pop();
        for(int i = 0; i < a[head].size(); i++){
            int v = a[head][i];
            if (f[head] + 1 < f[v]){
                f[v] = f[head] + 1;
                num[v] = num[head];
                q.push(v);
            }
            else if (f[head] + 1 == f[v]){
                num[v] += num[head];
                num[v] %= mod;
            }
        }
    }
}

int main(){
    memset(f, oo, sizeof(f));
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    bfs();
    for(int i = 1; i <= n; i++)
        cout << num[i] % mod << endl;
    return 0;
}

Info:I don't know the "spfa"!

2024/10/29 13:17
加载中...