Official

G - Return to 1 Editorial by en_translator


First of all, the vertices not reachable from vertex 11 and those from which vertex 11 is not reachable (and the adjacent edges) can be removed from the graph. We now assume that the graph is strongly connected.

Let LL be the set of sizes of lengths of cycles containing vertex 11, and GG be the greatest common divisors of the numbers contained in LL (or 00 if LL is an empty set). Then, the answer is Yes if and only if GG is a divisor of 101010010^{10^{100}}.

Proof Suppose that there are two cycles of lengths aa and bb. Also, suppose that gcd(a,b)=g\gcd(a,b) = g, where a=gaa=ga' and b=gbb=gb'. Then, for all integers kabk\geq a'b', there exist non-negative integers xx and yy such that ax+by=ka'x+b'y=k. Thus, by using the two cycles together, we can construct any cycle of a length that is a multiple of gcd(a,b)\gcd(a,b) and is at least lcm(a,b)\mathrm{lcm}(a,b). As an extension, by using tt cycles of lengths l1,l2,l_1,l_2, \dots, and ltl_t, we can construct any cycle of a length that is a multiple of gcd(l1,l2,lt)\gcd(l_1,l_2, \dots l_t) and is at least lcm(l1,l2,lt)\mathrm{lcm}(l_1,l_2, \dots l_t), which can be shown by induction. Since 101010010^{10^{100}} is sufficiently large, if GG is a divisor of 101010010^{10^{100}}, then a cycle of length 101010010^{10^{100}} exists. Conversely, if GG has a prime divisor other than 22 and 55, then it is obvious that there is no cycle of a length that is a multiple of 101010010^{10^{100}}. Thus, the proposition is proven.


However, LL can be an infinite set, so we cannot find GG. Now, (it is a bit sudden) let us take an arbitrary directed spanning tree TT rooted at vertex 11, and let did_i be the depth of vertex ii in TT. Then, for all integers aa, the following fact holds:

L contains an integer that is not a multiple of Lthere is an edge uv such that du+1dv≢0(moda). L\text{ contains an integer that is }\textbf{not}\text{ a multiple of }L \Leftrightarrow \text{there is an edge }u\rightarrow v\text{ such that } d_u+1-d_v \not\equiv 0 \pmod a.

Proof

(\Rightarrow) We show the contraposition. Since du+1dv(moda)d_u+1 \equiv d_v \pmod a for all edges uvu \rightarrow v, the sequence of dimodad_i \bmod a on the cycle that starts from and ends at vertex 11 is always periodic, like 01a10100 \rightarrow 1 \rightarrow \dots \rightarrow a-1 \rightarrow 0 \rightarrow 1 \rightarrow \dots \rightarrow 0. Thus, lmoda0l \bmod a \equiv 0 for all lLl \in L.

(\Leftarrow) Take a cycle that contains an edge such that du+1dv≢0(moda)d_u+1-d_v \not\equiv 0 \pmod a exactly once, and let lLl \in L be its length. (Since every edge in TT satisfies du+1dv=00(moda)d_u+1-d_v =0\equiv 0 \pmod a, such a cycle always exists.) Then, l≢0(moda)l \not\equiv 0 \pmod a.


Hence, if we let GG' be the greatest common divisor of du+1dv|d_u+1-d_v| for all edges uvu \rightarrow v, then GG' coincides with gg. Since GG can be found in a total of O(N+M)O(N+M) time, the problem has been solved.

Sample code (C++):

Copy
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int t;
  5. cin >> t;
  6. while (t--) {
  7. int n, m;
  8. cin >> n >> m;
  9. vector<vector<int>> G(n), rG(n);
  10. for (int i = 0; i < m; i++) {
  11. int u, v;
  12. cin >> u >> v;
  13. --u, --v;
  14. G[u].push_back(v);
  15. rG[v].push_back(u);
  16. }
  17. vector<int> dep(n, -1);
  18. dep[0] = 0;
  19. auto dfs = [&](auto &dfs, int u) -> void {
  20. for (int v: G[u]) {
  21. if (dep[v] != -1) continue;
  22. dep[v] = dep[u] + 1;
  23. dfs(dfs, v);
  24. }
  25. };
  26. dfs(dfs, 0);
  27. vector<bool> valid(n); // valid[i] = i から 0 に到達可能かどうか
  28. auto dfs_r = [&](auto &dfs_r, int u) -> void {
  29. valid[u] = true;
  30. for (int v: rG[u]) {
  31. if (valid[v]) continue;
  32. dfs_r(dfs_r, v);
  33. }
  34. };
  35. dfs_r(dfs_r, 0);
  36. for (int i = 0; i < n; i++) if (!valid[i]) dep[i] = -1;
  37. int g = 0;
  38. for (int u = 0; u < n; u++) {
  39. if (dep[u] == -1) continue;
  40. for (int v: G[u]) {
  41. if (dep[v] == -1) continue;
  42. g = gcd(g, abs(dep[u] + 1 - dep[v]));
  43. }
  44. }
  45. if (!g) {
  46. cout << "No\n";
  47. continue;
  48. }
  49. while (g % 2 == 0) g /= 2;
  50. while (g % 5 == 0) g /= 5;
  51. cout << (g == 1 ? "Yes" : "No") << '\n';
  52. }
  53. }
#include<bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<vector<int>> G(n), rG(n);
        for (int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            --u, --v;
            G[u].push_back(v);
            rG[v].push_back(u);
        }
        vector<int> dep(n, -1);
        dep[0] = 0;
        auto dfs = [&](auto &dfs, int u) -> void {
            for (int v: G[u]) {
                if (dep[v] != -1) continue;
                dep[v] = dep[u] + 1;
                dfs(dfs, v);
            }
        };
        dfs(dfs, 0);
        vector<bool> valid(n); // valid[i] = i から 0 に到達可能かどうか
        auto dfs_r = [&](auto &dfs_r, int u) -> void {
            valid[u] = true;
            for (int v: rG[u]) {
                if (valid[v]) continue;
                dfs_r(dfs_r, v);
            }
        };
        dfs_r(dfs_r, 0);
        for (int i = 0; i < n; i++) if (!valid[i]) dep[i] = -1;
        int g = 0;
        for (int u = 0; u < n; u++) {
            if (dep[u] == -1) continue;
            for (int v: G[u]) {
                if (dep[v] == -1) continue;
                g = gcd(g, abs(dep[u] + 1 - dep[v]));
            }
        }
        if (!g) {
            cout << "No\n";
            continue;
        }
        while (g % 2 == 0) g /= 2;
        while (g % 5 == 0) g /= 5;
        cout << (g == 1 ? "Yes" : "No") << '\n';
    }
}

posted:
last update:



2025-04-09 (Wed)
04:03:12 +00:00