Omeganott

Untitled

Aug 16th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.58 KB | None | 0 0
  1. /*
  2. #############################################################################################%####################################%###############
  3. #--%###*-=+-####--+::::=##%-:*#=:%#*-*#%-::+########%=::+##:=%#*-=#=-##:::::+%-:###-=####-:::=%##-::::*##=:=#####-::*%::::::+#-+###-::*##%--#%%--#
  4. #::%###*:-*:-##=:#=..::-###=.*#-.*#=:**:.:..:#####*#:.:::##.-##+.+#=:##...::+%:.=#%:-####:.::.:%#..:::*#%::.####-.::-%:....:+*:=#*..:.:=##:.=##:-#
  5. #::####*:-%::%#-:%=.#######*.+#:.:%:.%-:*##+:+#####-:#%##*#.-##+:=#=:*#.:%###%::.*%:-###%::##=:*#.-#####+:#.+##::####*##-:%##*.=#-:##%-:#%::.##::#
  6. #::####*:-#+:*#:=#=.::-*####:=*.=:#:-#::%##*.=#####::######.::::.=#=:##..:--#%::--#:-####:.##::##.::-=##-:#:=#+:=#######-:%##*.=%:-###+.=%::-:#::#
  7. #::####+:-#*:+*.*#=:::-#####:-=:+.*.+%::##%*.=*--=#::#####*..::..=#=:*#..::-#%:.+.+:-####:..:.+##:::-=##:=#-:*+:=#######::###*.=#:-###+:-#::*.*.-#
  8. #::%###+:-##:-::%#=:########:::-#:-:%#::*##*.+#--=#::*###*#:-##+:=#=:*#::%###%::%=.:-####:.#::*##:-####*.....#*:-#######-:###*:=%-:###=:+%::#-.::%
  9. #::*****:-##-..+##=.+***####=..##..:##*::%+.:####*#+::#*:##.-##*:=#=:##.:*#**%::#*:.-###%:.##:-##.:***#::###.-%-:-#+-%##-:###*:=#+..#+.-%#::#%:.:#
  10. #:..::+*:=##%::*##=:...:%##%#::##=:-###*:..:#########:::-#*:-##*:+%=:##:..::=%::##+:-###%-:#%=:=%:....+:-###--*#=:.:=###-:###*:+*#*.::-###:-##+:-#
  11. ################################################################################################################################################%#
  12. ####################################################%#################################################################%#########%#################
  13. %#%#%@%@@%%%%%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##%%%###%%%@%*+=@==#*+*%*++#@@@%#+.=@@*##+++%%@@@@%%%%%%%%%%%
  28. #%#%%%@#%%%%#%%@###%#%%%++*+#@@@@@@@@@@@@@@@@@@@@@%@@@@@@@@@@@%%%%@**@@@%*%%#:+@@%%#%#*++##%#+++++*%%%*++++++*@%@@@##=.-%@%%#%++++@@@@%%%%%%%%%%%%
  29. %@%#%#@%%@%%%%%%%%%%#%%@%%%#=*@@@@@@@@@@@@@@@%@@@@%@@@@@@@@@@@%#%%%#@@@%@%@@##:-%%#*+*#%%%*+++++++++++*++++++++%%##=:.*@@@#%%#++++@@@%%%######%%%%
  30. #%%%%@%%%%#%%%@@@@@%%%%%%%%%@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%@@%@@%#%%#%%*%@##*-:=+*%%%#*++++++++++++*+++++++++#+:+#@@@@@@@@%*+++@@@%%%##########
  31. %%%%@@@@%%%%%#%#%##%%%%%%#%%%#+==***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@***##*#%%%%%%@%%%#*#@%*+++++++++++++++%++++++++=#*++%@@%@@@@%*+++@@%%%%##########
  32. @@@%%%#%%###%%%%%#######%%%%%##+==#=====*+#%@@@@@@@@@@@@@@%@@@@+*#*+=#%@%#%%%%%%#%%%%%*+++++++**+++++++**+++**++++@+++%%@@@@@@#+=%%%%%%%##########
  33. ####%%%%%#%%%%%#%@%#####%%%%%%%*=+=#=====#+==*@@@@@@@@@@@**@@@###%@%%@@#%%%%%%%%##%%@*++++++*%++++++++++*++++###*+#*++*@@@@@%%%*@%%%%%%###########
  34. %##%%%%%%##%%%%##%%######%%#%%%#++=+%=====#===+%@@%%@@@@@%#@@@%%%%%@@%#%%%%%%%%%%%%%%++=++#%#*++++++++++*+++++#==##++++@%@@%%%%####%%##%%######*##
  35. %####%@%#%#%%%%###@%####%###%%%%#====*=====#==++#@@@@@@@@@@@@@@#%%%@%#%%%%%%%%%####%%#%%*===*#++++++++++*+++++#===+%+++%%%%%%%%%%#####%%%%%%%%%%%%
  36. %####%#%#%#%%%%###%@######%%%%%%%+===#+====+%+=+*%@@@@@@@@%%#*%%@@@%%%%#%#%%%%%#++*#%=---==-=+%*++++++++++++++#=====%++*%%%%%%%%%#%###%%%%%%%%%%%@
  37. %######%%%%%%%#%##%%%%%####%%%%%%#++==#======#=++*%@@@@@@@@@@%%@@@%%##%#%%%%##*++++#*-===---=-==##++++++++++**==-===%++#%%%%%%%%%%%%%#%%%%%%%@@@@@
  38. %####%%#%%%%###%%%#%%%#%###%@%%%%%++=++#=====**=+++%@@@@@@@@@@@@@##%#%%%%%%#%*+++++%+==-===--===-=*%*+++++#*==-=--==%++#%%%%%%%%%%%%%%%#*%@@@@@@@@
  39. %#######%@%%@@@%%#%%%%#%%#%%%%%%%##+*==**==+==*==+*+@@@@@@@@@@@@%%%%#%%%%%%#+++++++%+=========--====+%%**-=--====-==%++#@@%@%%%%#*%%%%%#%**+*@%@@%
  40. %%#%%#%%#%@#****#%@@@@@%%@@@@@@@@@@@@@@@@%%#***+*++++%@@@@@@@@@%%%%%%%#%%%%*+++++++%+===-==========--====---=====-=+%++#@@@%%+++###%#%-=+#=-#%@@@#
  41. %%##%%%#%##@##*******##%%%%%%#####**#**#*****##%@@%%%#@@@@@@@@%%%%%%%%%#%#*++++++++##=-====---=---==--==--=======-=*#++@@%#=*+++--:-=-+-==#*=++*=-
  42. #%##%%%####%@%#%%%%%%%%%%%#***********************#%@@@@@@@@@@%%%%%%##%##*++++++++++%=---=--===--===========-==-==-%#+*@#**%#%%%@#*%%%%%#%@@%-*==*
  43. %@#********##%%%%%%%%%%%##****************************%@@@@@@%#%%%%%#%%%*+++++++++++*%*-=-=-=-+%=-===========-*#==-%*=%+==%%%%%@@@##@%%@@@@@%@%%%%
  44. %%%%%%%%%%%%%%%%%%%%%%%#********************************#%@@@%##%#%%#%%*+++++++++++*++#%==--=%*%============--%%=-*#+%%%%%%%@@@@%@%@@@@@@@@@%@@@%@
  45. %%%%%%%%%%%%%%%%%%%%%%%#*******************************##%%%%#%%%%%%%%*++++++++=+#@#+++%%=-+#+*%====-=----===-##*#++++%%%%%@%%%@@@@@@@@@@@@@@@@@@@
  46. %%%%%%%%%%%#@%%%%%%%%%%###****************************#%%%#%#%%%%%%%%#++++++++#@%###++**###++++##-====--=====#**%*++++*@@@%##%@@@@@@@@@@@@@@@@@@@@
  47. %%%%@%*++++++%%%%%%%%%%%%%####*************####%%#%#**#%###%%%#%%%%%#+++*#%@%#*+++#%+%++++++++++#%==-===--=+%+++++++#==#%%%%@@@@@@@@@@@@@@@@@@@@@@
  48. %%@#+=+++++++*%@@@%%*+++*@%%%%%%%%%##%%%@@%%%#++==**+%#########%%@%%%#%%%%#%*+++++***=+++++++++++*%#===-==@*++++++++*%++%%@@@@@@@@@@@@@@@@@@@@@@@@
  49. @#+=+++++++++++++++=++++++%%%%%%%%%%%%%%###%#%*====##%##########%%%%%#%###%#+++=*++++++++++++++++++#@*-+%*+=+++++++++@++#@@@@@@@@@@@@@@@@@@@@@@@@@
  50. #+++++++++++++++++++++++++%%%%%@@@%##%%@#%%%%##+=*%##########%#@%%%%%%%%%%%++++#++++++++++++++++++++++#++++++++++++++*#++@@@@@@@@@@@@@@@@@@@@@@@@@
  51. #+++++*++++%++++++++++++++#@%@%%%#%##%%%%%%#%#%#%##############@%%%%%%%%%#++++*=++=+++++++++++++++++++*+++++++++++++++%++%@@@@@@@@@@@@@@@%%@%@@@@@
  52. ##%%%#++++#=++++++#+++++++*%#%%@%#######%#%%#%%%###%##########%#%%%%%@%#%*+++**=+++++++++++++++++++=++#+++++++++++++++%++#@%@@@@@@@@@@@@@@@@@%@%%#
  53. %%%%%++++%+++++++*@%#+=+++*%%%#%@#%%###%%@%#%%##############%%%%%%#%@%#%*++++*++++++++++++++++++++++++#+++++++++++++++#*+#@@@@@@@@@@%%#########%%%
  54. %%%@%*++*++++++++#@%@%####@%%%#%%@#%####%#%%%#%##############%%%%%%%@%%%++++*+++++++++++++++++++++++++#++++++++=++++++#%%@###*###%%%%%@@@@@@@@@@@@
  55. %%%@##++*+++++++*%@%%@@%%%@%%##%%%%##%%%#%%%%################%%#%%#@%%%#+++++=++++++++++++++++++++++++#+++++++*+++++++#%%%@%%%%#%%@%#%@@@@@@@@@@@@
  56. %%%@%##+*++++++#%@%%##@%%%%##%%%#%@%#%#%#%%#################%@%%#+=*##%%*++++++++++++++=+++++++++++=+=#+++++++*+++++++#@%%%%##%@@@@@@@@@@@@@@@@@@@
  57. %%%%@%##**++##%%%##%##%@%%#%#%%##%#@%#%%%#############%######+--====%%#%#+++++++++++++*+++++++++++++++#++++++**+++++++##***#%#%@@@@@@@@@@@@@@@@@@@
  58. %%%%%@%#%##%##%%##%#%%%%%#%%#%%%%%##@%#@%########%###%@#####==-======@%%#+++++++++++++#=++++++++++++#=*%+++++#++++++++%#*#%@@@%@@@@@%@@%%@@%%@@@@@
  59. %%%%%%@%#%%%%@%##%%#%%%%%@%##%%%%%%%%%%%###########@%#%####=--=-=-=*%#@#%*++++++++++++@*++++++++++*+==-=##+++@*+++++++%%@%%@@@@@@@%%@%%%%%%@@@@@@@
  60. %%%%%%%@%#%@%#%%%#%%##%%##@%#%%%%%#%#%%#%###%###%@###%%###=======+@*+=#@%%+++++++++++%*%+++++++=*#-=--====@+*+%+++++++%@@@@%%%%%%%%%%@@%@@@@@@@@@@
  61. %%%%@%%#%%%%@%%%%%%%######%%%##%%#%%#@%######@@######%%%#=-=--=%%#+-===*@%#++++++++%+==#*++++=*%=-======-====-+%*+++++@%%%@@@%%%%%@%%@@@@@@@@@@@@@
  62. @@#####%###%#%#%%%%%####%##%@%#%%%@%#%%%%%%##########%%*=-==#@#%==-=-==-+%@+++++##===-==@+++#*==-=======-==--===%#+++##@@@@%%%%%%%%%@@@@@@@@@@@@@@
  63. %###%%%%##%%%#@%%%%%%%%##%#%%@%@%#%##%###############%#%%##%#*---=-==--===*%###==-====-=+%%=----=-=====--===-==-=%%++@=%@%%%%%%%%%@@@@@%@@@@@@@@@@
  64. %%%############@#%#%%%%#%#%#%@%#%###########################==-======--======-===--===--===--==========-----======#%%-=+%%%@@%%%@@@@@@@@@@@@@@@@@@
  65. %%%%%#########%#%#%%%%%%%#@@###############################*=====-------=--==--==---==-=====-============-=-=======---==%@@%%%%%%@@@@@@@@@@@@@@@@@
  66. %%%%%##########%#@#%%#%%@%##%##########################%###==-=====------======-=-=----=-#=====-===-=--=====--=--======-@@@@@@@@@@@@@@@@@@@@@@@@@@
  67. %%%%%#########%%##%%%#%%##################################*========------=======+===---=#%==---=-==@==-===-*==-=-======-%@@@@@@@@@@@@@@@@@@@@@@@@@
  68. #%%%%#########%##%%%#@################################%###=-==========---=-====*%===-==*%%==-==-==@#%-=--==%+-===-====--#@@@@@@@@@@@@@@@@@@@@@@@@@
  69. #%%%%%%########%#%#@%################%##################%*=====-===-===--=-==-#%%--=-=*##%+-=====@*#%@---==%%-==--=====-*@@@@@@@@@@@@@@@@@@@@@@@@@
  70. #%%%%%%%########%#@###################################%##=-======-%====--=-==%#%+=-=-@#*%%*-=--+%*++##%*-==%%%---=----=-*@@@@@@@@@@@@@@@@@@@@@@@@@
  71. #%%%%%%%########%###############%@%%##%#################*-========@%+==-==-*@+%@=-=%%*++%%#==*%#++=++%%%%*@#%%#--======-*@@@@@@@@@@@@@@@@@@@@@@@@@
  72. #%%%%%%%######%%%############%@%#%#@####################=-=======+##@*-==@@**=%%###*+=++##%%%#*+++++++*#%%*+###@+=====-=%@@@@@@@@@@@@@@@@@@@@@@@@@
  73. */
  74.  
  75. #include <bits/stdc++.h>
  76.  
  77. using namespace std;
  78.  
  79. using ll = long long;
  80. const ll MOD = 1E9 + 7;
  81. const int INF = 1E9; const ll INFLL = 1E18;
  82.  
  83. int g;
  84. bool twoColorable;
  85.  
  86. void dfs(int x, int y, vector<vector<array<int, 2>>>& adj, vector<array<bool, 2>>& seen) {
  87.     seen[x][y] = true;
  88.     if(seen[x][y ^ 1]) {
  89.         twoColorable = false;
  90.     }
  91.     for(array<int, 2> i : adj[x]) {
  92.         if(!seen[i[0]][y ^ ((i[1] / g) % 2)]) {
  93.             dfs(i[0], y ^ ((i[1] / g) % 2), adj, seen);
  94.         }
  95.     }
  96. }
  97.  
  98. int n; int m; int k; int s; int t;
  99.  
  100. int main() {
  101.     ios_base::sync_with_stdio(false);
  102.     cin.tie(0);
  103.     int T; cin >> T;
  104.     for(int test = 1; test <= T; test++) {
  105.         cin >> n >> m >> k >> s >> t;
  106.         vector<vector<array<int, 2>>> adj(n);
  107.         g = k;
  108.         for(int i = 0; i < m; i++) {
  109.             int u; int v; int w;
  110.             cin >> u >> v >> w;
  111.             u--; v--;
  112.             adj[u].push_back({v, w});
  113.             adj[v].push_back({u, w});
  114.             g = __gcd(g, w);
  115.         }
  116.         if((k / g) % 2) {
  117.             cout << k / g << "\n";
  118.             continue;
  119.         }
  120.         vector<array<bool, 2>> seen(n, {false, false});
  121.         twoColorable = true;
  122.         dfs(s - 1, 0, adj, seen);
  123.         cout << max((twoColorable ? k / g / 2 : k / g), 1) << "\n";
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment