一种神奇的写法,但是TLE on #6
查看原帖
一种神奇的写法,但是TLE on #6
895312
Archy_楼主2024/8/13 18:11

没有使用最短路。不过为什么过不了???

#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#include <queue>
#define re register
#define mod 100003

using namespace std;

const int N = 1000005;
const int M = 2000005;
int n, m, dep[N], ans[N];
vector <int> g[N];
map <pair<int, int>, int> mp;
map <pair<int, int>, bool> vis;

inline int rd(){
	re int w = 1, x = 0;
    re char c = getchar();
	while(c < 48 || c > 57) {
		if(c == 45) w *= -1;
		c = getchar();
	}
	while(c >= 48 && c <= 57) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return x * w;
}

void bfs() {
	queue <pair<int, int> > q;
	q.push({1, 0});
	while(!q.empty()) {
		int u = q.front().first;
		int fa = q.front().second;
		q.pop();
		for(auto &v : g[u]) {
			if(v == fa) continue;
			if(dep[v] == 1e9) {
				dep[v] = min(dep[u] + 1, dep[v]);
				q.push({v, u});
			}
		}	
	}
	queue <int> q1;
	q1.push(1);
	while(!q1.empty()){
		int u = q1.front();
		q1.pop();
		for(auto &v : g[u]) {
			if(dep[v] != dep[u] + 1 || vis[{u, v}]) continue;
			ans[v] = ans[v] + ans[u] * mp[{u, v}] % mod;
			vis[{u, v}] = vis[{v, u}] = 1;
			q1.push(v);
		}
	}
}

int main() {
	n = rd(), m = rd();
	for(re int i = 1; i <= m; i ++) {
		int u = rd(), v = rd();
		g[u].push_back(v);
		g[v].push_back(u);
		mp[{u, v}] ++;
	}
	for(int i = 2; i <= n; i ++) dep[i] = 1e9;
	ans[1] = 1;
	bfs();
	for(int i = 1; i <= n; i ++) {
		cout << ans[i] % mod<<"\n";	
	}
	return 0;
}
2024/8/13 18:11
加载中...