fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 400 + 20;
  4. int n, m, s, t;
  5.  
  6. bool inq[maxn];
  7. int g[maxn][maxn], dis[maxn];
  8. vector<int> ans[maxn];
  9. void spfa() {
  10. queue<int> que;
  11. memset(dis, 0x3f, sizeof dis);
  12. que.push(s), dis[s] = 0, inq[s] = true;
  13. while(!que.empty()) {
  14. int u = que.front(); que.pop();
  15. inq[u] = false;
  16. for(int i = 1; i <= n; ++i) {
  17. if(g[u][i] != 0 && dis[i] > dis[u] + 1) {
  18. dis[i] = dis[u] + 1;
  19. if(!inq[i]) {
  20. que.push(i);
  21. inq[i] = true;
  22. }
  23. }
  24. }
  25. }
  26. }
  27.  
  28. int main() {
  29. scanf("%d%d%d%d", &n, &m, &s, &t);
  30. for(int i = 1; i <= m; ++i) {
  31. int u, v;
  32. scanf("%d%d", &u, &v);
  33. g[u][v] = g[v][u] = i;
  34. }
  35. spfa();
  36. for(int i = 1; i <= n; ++i) {
  37. for(int j = 1; j <= n; ++j) {
  38. if(g[i][j] && dis[j] == dis[i] + 1) {
  39. ans[dis[i]].push_back(g[i][j]);
  40. g[i][j] = g[j][i] = 0;
  41. }
  42. }
  43. }
  44. for(int i = 1; i <= n; ++i) {
  45. for(int j = 1; j <= n; ++j) {
  46. if(g[i][j]) {
  47. ans[dis[t]].push_back(g[i][j]);
  48. g[i][j] = g[j][i] = 0;
  49. }
  50. }
  51. }
  52. printf("%d\n", dis[t]);
  53. for(int i = 0; i < dis[t]; ++i) {
  54. printf("%d", (int)ans[i].size());
  55. for(int j = 0, sz = ans[i].size(); j < sz; ++j) {
  56. printf(" %d", ans[i][j]);
  57. }
  58. puts("");
  59. }
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 4176KB
stdin
4 4 1 4 
1 2 
1 3 
2 4 
3 4 
stdout
2
2 1 2
2 3 4