Advertisement
Dorijanko

Untitled

Oct 11th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define X first
  3. #define Y second
  4. #define x first
  5. #define y second
  6. using namespace std;
  7. using ll=long long;
  8. using pii=pair<int,int>;
  9. using vi=vector<int>;
  10. using vl=vector<ll>;
  11. #define pb push_back
  12. #define all(a) begin(a),end(a)
  13.  
  14. const int N=10010,MOD=1e9+7;
  15. const char en='\n';
  16. const ll LLINF=1ll<<60;
  17.  
  18. int n, m;
  19. vector < pii > v[N];
  20. ll dep[N], d;
  21. int par[N][15], deep[N];
  22. vector < pii > luk;
  23. vector < int > smece;
  24.  
  25. void dfs(int x, int lst) {
  26. par[x][0] = lst;
  27. for(int j = 1;j < 15;j++)
  28. par[x][j] = par[par[x][j - 1]][j - 1];
  29. cout << x << " " << lst << endl;
  30. for(pii tmp : v[x]) {
  31. if(tmp.Y == lst) continue;
  32. if(dep[tmp.Y] != -1 && dep[tmp.Y] < dep[x]) {
  33. luk.pb({x, tmp.Y});
  34. smece.pb(tmp.X);
  35. } else if(dep[tmp.Y] == -1) {
  36. dep[tmp.Y] = dep[x] + tmp.X;
  37. deep[tmp.Y] = deep[x] + 1;
  38. dfs(tmp.Y, x);
  39. }
  40. }
  41. }
  42.  
  43. int digni(int x, int k){
  44. for(int j = 0;j < 15;j++)
  45. if(k & (1 << j)) x = par[x][j];
  46. return x;
  47. }
  48.  
  49. int main()
  50. {
  51.  
  52. ios_base::sync_with_stdio(0);
  53. cin.tie(0);
  54. memset(dep, -1, sizeof(dep));
  55. cin >> n >> m;
  56. for(int i = 0;i < m;i++) {
  57. int a, b, c; scanf("%d%d%d", &a, &b, &c);
  58. v[a].pb({c, b});
  59. v[b].pb({c, a});
  60. }
  61. ll d = 0;
  62. for(int x = 1;x <= n;x++) {
  63. if(dep[x] != -1) continue;
  64. dep[x] = 0; dfs(x, x); deep[x] = 0;
  65. for(int i = 0;i < (int)luk.size();i++) {
  66. // cout << " luk " << luk[i].x << " " << luk[i].y << std::endl;
  67. d = gcd(d, dep[luk[i].x] - dep[luk[i].y] + smece[i]);
  68. for(int j = 0;j < (int)luk.size();j++) {
  69. if(i == j) continue;
  70. if(dep[luk[i].y] > dep[luk[j].y]) continue;
  71. if(dep[luk[i].x] > dep[luk[j].x]) continue;
  72. if(dep[luk[j].y] > dep[luk[i].x]) continue;
  73. if(digni(luk[j].x, deep[luk[j].x] - deep[luk[i].x]) != luk[i].x) continue;
  74. d = gcd(d, dep[luk[i].x] - dep[luk[j].y]);
  75. }
  76. }
  77. luk.clear(); smece.clear();
  78. }
  79. cout << d << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement