fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int n, m, s, t;
  5. vector<int> G[200050];
  6. int dist[200050];
  7.  
  8. int main() {
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11. cin >> n >> m >> s;
  12. while(m--) {
  13. int u, v;
  14. cin >> u >> v;
  15. G[u].push_back(v);
  16. G[v].push_back(u);
  17. }
  18. for(int i = 1; i <= n; i++) dist[i] = 1000000000;
  19. dist[s] = 0;
  20. queue<int> q; // lưu xem là những đỉnh nào sẽ cần đi cập nhật
  21. q.push(s);
  22. while(!q.empty()) {
  23. int u = q.front(); q.pop(); // lấy ra đỉnh ở đầu hàng đợi (đây chính là đỉnh có khoảng cách tới s nhỏ nhất hiện tại)
  24. for(int v : G[u]) if (dist[v] > dist[u] + 1) {
  25. dist[v] = dist[u] + 1; // s->v khi đi qua đỉnh u và cạnh (u, v) sẽ tối ưu hơn
  26. q.push(v);
  27. }
  28. }
  29. int ans = 0;
  30. for(int i = 1; i <= n; i++) if (dist[i] != 1000000000)
  31. ans++;
  32. cout << ans;
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0.01s 8156KB
stdin
Standard input is empty
stdout
Standard output is empty