fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. #define int long long
  5. const int MOD = pow(10,9)+7;
  6. const int MOD2 = 998244353;
  7. const int INF = LLONG_MAX/2;
  8.  
  9. int primes[1000000];
  10.  
  11. void seive(){
  12. fill(primes, primes + 1000000, 1);
  13. primes[0] = primes[1] = 0;
  14. for(int i = 2 ; i*i < 1000000 ; i++){
  15. if(primes[i]){
  16. for(int j = i*i ; j < 1000000 ; j += i){
  17. primes[j] = 0;
  18. }
  19. }
  20. }
  21. for(int i = 1 ; i < 1000000 ; i++){
  22. primes[i] += primes[i-1];
  23. }
  24. }
  25. int factorial(int n){
  26. if(n==0){
  27. return 1;
  28. }
  29. return (n*(factorial(n-1)))%MOD;
  30. }
  31. bool isPrime(int n){
  32. if(n <= 1) return false;
  33. for(int i = 2 ; i*i <= n ; i++){
  34. if(n % i == 0) return false;
  35. }
  36. return true;
  37. }
  38.  
  39. int power(int a, int b){
  40. if(b == 0) return 1;
  41. a %= MOD;
  42. int value = power(a, b / 2);
  43. if(b % 2 == 0){
  44. return (value * value) % MOD;
  45. } else {
  46. return ((value * value) % MOD * (a % MOD)) % MOD;
  47. }
  48. }
  49.  
  50. int gcd(int a, int b){
  51. if(a == 0) return b;
  52. return gcd(b % a, a);
  53. }
  54. void solve() {
  55. int n1,m1;
  56. cin>>n1>>m1;
  57. vector<int>A[n1+1];
  58. int value[n1+1];
  59. for(int i = 0 ; i<m1 ; i++){
  60. int a,b;
  61. cin>>a>>b;
  62. A[a].push_back(b);
  63. A[b].push_back(a);
  64. }
  65. for(int i = 1 ; i<=n1 ; i++){
  66. cin>>value[i];
  67. }
  68. int d;
  69. cin>>d;
  70. queue<int>q;
  71. int visited[n1+1] = {0},visited2[n1+1] = {0};
  72. int level[n1+1] = {0},level2[n1+1] = {0};
  73. int five[n1+1] = {0},five2[n1+1] = {0};
  74. int ways[n1+1] = {0};
  75. visited[d] = 1;
  76. if(value[d]==5){
  77. five[d] = 1;
  78. }
  79. q.push(d);
  80. while(!q.empty()){
  81. int node = q.front();
  82. q.pop();
  83. for(auto node1 : A[node]){
  84. if(!visited[node1]){
  85. q.push(node1);
  86. visited[node1] = 1;
  87. level[node1] = level[node]+1;
  88. if(value[node1]==5){
  89. five[node1] = 1+five[node];
  90. }
  91. else{
  92. five[node1] = five[node];
  93. }
  94. }
  95. else{
  96. if((level[node]+1)==(level[node1])){
  97. if(value[node1]==5){
  98. five[node1] = max((1+five[node]),five[node1]);
  99. }
  100. else{
  101. five[node1] = max(five[node],five[node1]);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. visited2[d] = 1;
  108. ways[d] = 1;
  109. q.push(d);
  110. while(!q.empty()){
  111. int node = q.front();
  112. q.pop();
  113. for(auto node1 : A[node]){
  114. if(!visited2[node1]){
  115. q.push(node1);
  116. level2[node1] = level2[node]+1;
  117. if(value[node1]==5){
  118. five2[node1] = 1+five2[node];
  119. }
  120. else{
  121. five2[node1] = five2[node];
  122. }
  123. if(five2[node1] == five[node1]){
  124. ways[node1] = ways[node];
  125. }
  126. visited2[node1] = 1;
  127. }
  128. else{
  129. if((level2[node]+1)==(level2[node1])){
  130. if(value[node1]==5){
  131. five2[node1] = max((1+five2[node]),five2[node1]);
  132. }
  133. else{
  134. five2[node1] = max(five2[node],five2[node1]);
  135. }
  136. if(five2[node1]==five[node1]){
  137. ways[node1] = ways[node1] + ways[node];
  138. }
  139. }
  140. }
  141. }
  142. }
  143. for(int i = 1 ; i<n1+1 ; i++){
  144. cout<<"No of paths having shortest length from node 1 to node "<<i<<" and maximum no of 5s are : "<<ways[i]<<endl;
  145. }
  146. }
  147.  
  148. signed main(){
  149. ios::sync_with_stdio(false); cin.tie(NULL);
  150. //int t;
  151. //cin >> t;
  152. //while(t--){
  153. solve();
  154. //}
  155. return 0;
  156. }
Success #stdin #stdout 0.01s 5292KB
stdin
6 7
1 2
1 3
2 4
3 4
3 5
4 6
5 6
0 5 0 5 5 0
1
stdout
No of paths having shortest length from node 1 to node 1 and maximum no of 5s are : 1
No of paths having shortest length from node 1 to node 2 and maximum no of 5s are : 1
No of paths having shortest length from node 1 to node 3 and maximum no of 5s are : 1
No of paths having shortest length from node 1 to node 4 and maximum no of 5s are : 2
No of paths having shortest length from node 1 to node 5 and maximum no of 5s are : 1
No of paths having shortest length from node 1 to node 6 and maximum no of 5s are : 3