Submission #635635

# Submission time Handle Problem Language Result Execution time Memory
635635 2022-08-26T14:14:41 Z 406 Subtree (INOI20_subtree) C++17
100 / 100
96 ms 33572 KB
#include <bits/stdc++.h>
using ll = long long;
using namespace std;

#define forn(a, b) for (int a = 0; a < (b); a++)
#define rofn(a, b) for (int a = (b) - 1; a >= 0; a--)
#define LOG(a) (31 - __builtin_clz(a))
#define chmx(a, b) a = max(a, b)
#define chmn(a, b) a = min(a, b)
#define pb push_back
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1,*p2;

inline int read() {
	int x = 0, f = 1; char c;
	while(!isdigit(c = gc())) if(c=='-') f=-1;
	while(isdigit(c)) x = (x<<3)+(x<<1)+(c^48), c = gc();
	return x * f;
}

const ll INFL = 1ll << 60;
const int INF = 1 << 29;
const int N = 1e5 + 50;
const int MOD = 1e9 + 7;

ll pw(ll a, ll b) {
    a %= MOD;
    b %= (MOD - 1);
    ll re = 1;
    while (b) {
        if (b & 1)
            re = (re * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return re;
}

ll dp[N];
int h[N], par[N], n, m;
bitset<N> mark;
vector<int> adj[N];

void dfs(int v) {
    mark[v] = true;
    dp[v] = 1;
    vector<int> unp;

    for (auto u: adj[v]) {
        if (!mark[u]) {
            par[u] = v;
            h[u] = h[v] + 1;
            dfs(u);
            dp[v] = (dp[v] * (dp[u] + 1)) % MOD;
        }
        else if (u != par[v] && h[u] > h[v]) {
            unp.pb(u);
        }
    }

    for (auto u: unp) {
        vector<ll> X;
        int last = n;
        while (u != par[v]) {
            X.pb((dp[u] * pw((dp[last] + 1), MOD - 2)) % MOD);
            if (X.back() < 0)
                X.back() += MOD;
            last = u;
            u = par[u];
        }

        dp[v] = 0;
        reverse(X.begin(), X.end());

        vector<ll> pref, P, suf;
        suf.pb(1);
        for (int i = X.size() - 1; i >= 1; i--) {
            suf.pb((suf.back() * X[i]) % MOD);
        }
        reverse(suf.begin(), suf.end());

        pref.pb(X[0]);
        for (int i = 1; i < X.size(); i++)
            pref.pb((pref.back() * X[i]) % MOD);

        P.resize(pref.size());
        for (int i = 0; i < P.size(); i++)
            P[i] = ((i ? P[i - 1] : 0) + pref[i]) % MOD;

        for (int i = 1; i < X.size(); i++) {
            dp[v] += (suf[i] * P[i - 1]) % MOD;
            if (dp[v] >= MOD)
                dp[v] -= MOD;
        }
    }
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[v].pb(u);
        adj[u].pb(v);
    }
    par[0] = -1;
    dfs(0);

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += dp[i];
        if (ans >= MOD)
            ans -= MOD;
    }
    cout << ans << '\n';

    return 0;
}

Compilation message

Main.cpp: In function 'void dfs(int)':
Main.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int i = 1; i < X.size(); i++)
      |                         ~~^~~~~~~~~~
Main.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for (int i = 0; i < P.size(); i++)
      |                         ~~^~~~~~~~~~
Main.cpp:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for (int i = 1; i < X.size(); i++) {
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2684 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2680 KB Output is correct
28 Correct 3 ms 2684 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2676 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2672 KB Output is correct
33 Correct 2 ms 2684 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 3 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2588 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 1 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 4 ms 3156 KB Output is correct
3 Correct 42 ms 7456 KB Output is correct
4 Correct 37 ms 7464 KB Output is correct
5 Correct 49 ms 7416 KB Output is correct
6 Correct 55 ms 7484 KB Output is correct
7 Correct 77 ms 27724 KB Output is correct
8 Correct 73 ms 20136 KB Output is correct
9 Correct 48 ms 20180 KB Output is correct
10 Correct 34 ms 8028 KB Output is correct
11 Correct 38 ms 7628 KB Output is correct
12 Correct 41 ms 7372 KB Output is correct
13 Correct 51 ms 7400 KB Output is correct
14 Correct 50 ms 16116 KB Output is correct
15 Correct 60 ms 30752 KB Output is correct
16 Correct 58 ms 33572 KB Output is correct
17 Correct 37 ms 7572 KB Output is correct
18 Correct 39 ms 7508 KB Output is correct
19 Correct 39 ms 7600 KB Output is correct
20 Correct 29 ms 7756 KB Output is correct
21 Correct 38 ms 7856 KB Output is correct
22 Correct 37 ms 7776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2684 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2680 KB Output is correct
28 Correct 3 ms 2684 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2676 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2672 KB Output is correct
33 Correct 2 ms 2684 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 3 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2588 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 1 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 4 ms 2900 KB Output is correct
53 Correct 4 ms 2904 KB Output is correct
54 Correct 3 ms 2900 KB Output is correct
55 Correct 3 ms 2944 KB Output is correct
56 Correct 5 ms 4052 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 3 ms 2900 KB Output is correct
59 Correct 4 ms 3412 KB Output is correct
60 Correct 3 ms 2900 KB Output is correct
61 Correct 3 ms 2948 KB Output is correct
62 Correct 5 ms 2912 KB Output is correct
63 Correct 3 ms 2944 KB Output is correct
64 Correct 3 ms 2900 KB Output is correct
65 Correct 3 ms 2900 KB Output is correct
66 Correct 4 ms 2952 KB Output is correct
67 Correct 4 ms 2944 KB Output is correct
68 Correct 5 ms 3796 KB Output is correct
69 Correct 4 ms 2948 KB Output is correct
70 Correct 4 ms 3028 KB Output is correct
71 Correct 4 ms 2944 KB Output is correct
72 Correct 4 ms 3028 KB Output is correct
73 Correct 3 ms 2888 KB Output is correct
74 Correct 4 ms 2948 KB Output is correct
75 Correct 4 ms 3540 KB Output is correct
76 Correct 4 ms 3028 KB Output is correct
77 Correct 4 ms 3924 KB Output is correct
78 Correct 4 ms 3156 KB Output is correct
79 Correct 6 ms 3796 KB Output is correct
80 Correct 5 ms 2944 KB Output is correct
81 Correct 4 ms 3028 KB Output is correct
82 Correct 4 ms 3156 KB Output is correct
83 Correct 4 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2676 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2684 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2680 KB Output is correct
28 Correct 3 ms 2684 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2676 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2672 KB Output is correct
33 Correct 2 ms 2684 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 3 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2588 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 1 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 1 ms 2644 KB Output is correct
53 Correct 4 ms 3156 KB Output is correct
54 Correct 42 ms 7456 KB Output is correct
55 Correct 37 ms 7464 KB Output is correct
56 Correct 49 ms 7416 KB Output is correct
57 Correct 55 ms 7484 KB Output is correct
58 Correct 77 ms 27724 KB Output is correct
59 Correct 73 ms 20136 KB Output is correct
60 Correct 48 ms 20180 KB Output is correct
61 Correct 34 ms 8028 KB Output is correct
62 Correct 38 ms 7628 KB Output is correct
63 Correct 41 ms 7372 KB Output is correct
64 Correct 51 ms 7400 KB Output is correct
65 Correct 50 ms 16116 KB Output is correct
66 Correct 60 ms 30752 KB Output is correct
67 Correct 58 ms 33572 KB Output is correct
68 Correct 37 ms 7572 KB Output is correct
69 Correct 39 ms 7508 KB Output is correct
70 Correct 39 ms 7600 KB Output is correct
71 Correct 29 ms 7756 KB Output is correct
72 Correct 38 ms 7856 KB Output is correct
73 Correct 37 ms 7776 KB Output is correct
74 Correct 4 ms 2900 KB Output is correct
75 Correct 4 ms 2904 KB Output is correct
76 Correct 3 ms 2900 KB Output is correct
77 Correct 3 ms 2944 KB Output is correct
78 Correct 5 ms 4052 KB Output is correct
79 Correct 3 ms 2900 KB Output is correct
80 Correct 3 ms 2900 KB Output is correct
81 Correct 4 ms 3412 KB Output is correct
82 Correct 3 ms 2900 KB Output is correct
83 Correct 3 ms 2948 KB Output is correct
84 Correct 5 ms 2912 KB Output is correct
85 Correct 3 ms 2944 KB Output is correct
86 Correct 3 ms 2900 KB Output is correct
87 Correct 3 ms 2900 KB Output is correct
88 Correct 4 ms 2952 KB Output is correct
89 Correct 4 ms 2944 KB Output is correct
90 Correct 5 ms 3796 KB Output is correct
91 Correct 4 ms 2948 KB Output is correct
92 Correct 4 ms 3028 KB Output is correct
93 Correct 4 ms 2944 KB Output is correct
94 Correct 4 ms 3028 KB Output is correct
95 Correct 3 ms 2888 KB Output is correct
96 Correct 4 ms 2948 KB Output is correct
97 Correct 4 ms 3540 KB Output is correct
98 Correct 4 ms 3028 KB Output is correct
99 Correct 4 ms 3924 KB Output is correct
100 Correct 4 ms 3156 KB Output is correct
101 Correct 6 ms 3796 KB Output is correct
102 Correct 5 ms 2944 KB Output is correct
103 Correct 4 ms 3028 KB Output is correct
104 Correct 4 ms 3156 KB Output is correct
105 Correct 4 ms 3028 KB Output is correct
106 Correct 60 ms 8648 KB Output is correct
107 Correct 51 ms 8752 KB Output is correct
108 Correct 49 ms 8780 KB Output is correct
109 Correct 53 ms 8816 KB Output is correct
110 Correct 37 ms 9084 KB Output is correct
111 Correct 49 ms 11384 KB Output is correct
112 Correct 96 ms 24536 KB Output is correct
113 Correct 81 ms 8824 KB Output is correct
114 Correct 57 ms 8676 KB Output is correct
115 Correct 30 ms 8772 KB Output is correct
116 Correct 72 ms 27156 KB Output is correct
117 Correct 75 ms 24200 KB Output is correct
118 Correct 38 ms 11072 KB Output is correct
119 Correct 72 ms 8860 KB Output is correct
120 Correct 67 ms 8852 KB Output is correct
121 Correct 75 ms 8828 KB Output is correct
122 Correct 66 ms 8908 KB Output is correct
123 Correct 43 ms 9016 KB Output is correct
124 Correct 35 ms 9044 KB Output is correct
125 Correct 31 ms 8884 KB Output is correct
126 Correct 68 ms 8624 KB Output is correct
127 Correct 62 ms 8732 KB Output is correct
128 Correct 58 ms 8724 KB Output is correct
129 Correct 57 ms 8732 KB Output is correct
130 Correct 35 ms 8960 KB Output is correct
131 Correct 47 ms 11396 KB Output is correct
132 Correct 78 ms 24128 KB Output is correct
133 Correct 65 ms 8908 KB Output is correct
134 Correct 51 ms 8652 KB Output is correct
135 Correct 32 ms 8880 KB Output is correct
136 Correct 65 ms 23040 KB Output is correct
137 Correct 82 ms 23500 KB Output is correct
138 Correct 40 ms 11012 KB Output is correct
139 Correct 66 ms 8984 KB Output is correct
140 Correct 63 ms 8848 KB Output is correct
141 Correct 62 ms 8824 KB Output is correct
142 Correct 85 ms 8900 KB Output is correct
143 Correct 46 ms 9044 KB Output is correct
144 Correct 35 ms 9036 KB Output is correct
145 Correct 34 ms 8888 KB Output is correct
146 Correct 70 ms 8704 KB Output is correct
147 Correct 67 ms 8708 KB Output is correct
148 Correct 70 ms 9996 KB Output is correct
149 Correct 68 ms 12648 KB Output is correct
150 Correct 80 ms 31732 KB Output is correct
151 Correct 77 ms 8908 KB Output is correct
152 Correct 74 ms 9088 KB Output is correct
153 Correct 73 ms 9412 KB Output is correct
154 Correct 66 ms 12552 KB Output is correct
155 Correct 38 ms 9192 KB Output is correct
156 Correct 90 ms 25864 KB Output is correct
157 Correct 84 ms 23640 KB Output is correct
158 Correct 70 ms 21388 KB Output is correct
159 Correct 44 ms 8640 KB Output is correct
160 Correct 63 ms 27372 KB Output is correct
161 Correct 56 ms 8840 KB Output is correct
162 Correct 46 ms 9172 KB Output is correct