版本比较
1
正文
增加 21 行删除 40 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576```cpp#include<bits/stdc++.h>using namespace std;int n, m, k, a[505][505];bool vis[505][505];vector<int> g[505];struct Node{int x, bu;};void bfs(int s){queue<Node> q;memset(vis, 0, sizeof vis);vis[s][0] = 1;q.push({s, 0});while(q.empty() == 0){Node c = q.front();q.pop();a[s][c.bu]++;if(c.bu == k){continue;}for(int i = 0; i < g[c.x].size(); i++){if(vis[g[c.x][i]][c.bu + 1] == 0){vis[g[c.x][i]][c.bu + 1] = 1;q.push({g[c.x][i], c.bu + 1});}}}return ;}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m >> k;for(int i = 1; i <= m; i++){int u, v;cin >> u >> v;g[u].push_back(v);g[v].push_back(u);}for(int i = 1; i <= n; i++){bfs(i);for(int j = 1; j <= k; j++){cout << a[i][j] << " ";}cout << "\n";}return 0;}```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657```cpp#include<bits/stdc++.h>using namespace std;const int N = 1e5 + 5;int n, m, a[N];vector<int> g[N];bool vis[N];void dfs(int x, int o){vis[x] = 1;a[x] = o;for(int i = 0; i < g[x].size(); i++){if(vis[g[x][i]] == 0){dfs(g[x][i], o);}}return ;}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m;for(int i = 1; i <= m; i++){int u, v;cin >> u >> v;g[v].push_back(u);}for(int i = n; i >= 1; i--){if(vis[i] == 0){dfs(i, i);}}for(int i = 1; i <= n; i++){cout << a[i] << " ";}return 0;}```