Submission #782930

# Submission time Handle Problem Language Result Execution time Memory
782930 2023-07-14T11:35:52 Z Sami_Massah Subtree (INOI20_subtree) C++17
100 / 100
106 ms 19412 KB
#include <bits/stdc++.h>
using namespace std;


const int maxn = 1e5 + 12, mod = 1e9 + 7;
long long n, m, pd[maxn], h[maxn], dp[maxn], sum[maxn];
vector <int> conn[maxn], nums;
bitset <maxn> marked, in_nums;
void dfs(int u, int p = -1){

    marked[u] = 1;
    int me = -1;
    for(int v: conn[u])
        if(!marked[v]){
            pd[v] = u;
            h[v] = h[u] + 1;
            dfs(v, u);
        }
        else if(h[u] < h[v])
            me = v;
   // cout << u << ' ' << me << endl;
    if(me == -1){
        dp[u] = 1;
        for(int v: conn[u])
            if(h[u] < h[v]){
            //    cout << u << '-' << v << endl;
                dp[u] = (dp[u] * (dp[v] + 1)) % mod;
    }
        dp[u] %= mod;
        //cout << u << "->" << dp[u] << endl;
    }
    else{
        nums.clear();
    //    in_nums = 0;
        while(h[u] <= h[me] && me){
            nums.push_back(me);
            in_nums[me] = 1;
            me = pd[me];
        }
        for(int j = 0; j <= nums.size(); j++)
            sum[j] = 0;
        long long res = 1;
        for(int j = 0; j < nums.size(); j++){

            int nu = nums[j];
            for(int v: conn[nu])
                if(h[nu] < h[v] && !in_nums[v]){
                    res = res * (dp[v] + 1) % mod;
                    res %= mod;
                }
            sum[j] = (j == 0? 1: sum[j - 1]);
            sum[j] += res;
            sum[j] %= mod;
          //  cout << nu << ' ' << sum[j] << endl;

        }
       // cout << dp[2] << endl;
        res = 1;
        for(int j = nums.size() - 1; j >= 1; j--){
            int nu = nums[j];
            for(int v: conn[nu])
                if(h[nu] < h[v] && !in_nums[v]){
                    res = res * (dp[v] + 1) % mod;
                }
            res %= mod;
            if(j - 2 >= 0)
                dp[u] += res % mod * (sum[j - 2] % mod) % mod;
            dp[u] %= mod;
        }
        //cout << dp[2] << endl;
        dp[u] += res;
        dp[u] %= mod;
        for(int j: nums)
            in_nums[j] = 0;
    }


}
int main(){
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        conn[a].push_back(b);
        conn[b].push_back(a);
    }
   // cout << endl;
    dfs(1);
    long long ans = 0;
   // cout << endl;
    for(int i = 1; i <= n; i++){
        ans += dp[i];
        ans %= mod;
       // cout << i << ' ' << dp[i] << endl;
        }
    cout << ans << endl;









}

Compilation message

Main.cpp: In function 'void dfs(int, int)':
Main.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for(int j = 0; j <= nums.size(); j++)
      |                        ~~^~~~~~~~~~~~~~
Main.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for(int j = 0; j < nums.size(); j++){
      |                        ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2664 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2660 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2660 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2660 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2668 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 1 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 1 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2660 KB Output is correct
44 Correct 1 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 1 ms 2664 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 1 ms 2644 KB Output is correct
49 Correct 2 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 2 ms 2644 KB Output is correct
2 Correct 6 ms 3316 KB Output is correct
3 Correct 62 ms 9392 KB Output is correct
4 Correct 68 ms 9392 KB Output is correct
5 Correct 66 ms 9400 KB Output is correct
6 Correct 56 ms 9320 KB Output is correct
7 Correct 66 ms 17088 KB Output is correct
8 Correct 62 ms 14228 KB Output is correct
9 Correct 61 ms 14308 KB Output is correct
10 Correct 56 ms 9868 KB Output is correct
11 Correct 63 ms 9484 KB Output is correct
12 Correct 66 ms 9248 KB Output is correct
13 Correct 55 ms 9332 KB Output is correct
14 Correct 55 ms 12772 KB Output is correct
15 Correct 93 ms 17472 KB Output is correct
16 Correct 70 ms 19412 KB Output is correct
17 Correct 55 ms 9240 KB Output is correct
18 Correct 56 ms 9308 KB Output is correct
19 Correct 70 ms 9320 KB Output is correct
20 Correct 52 ms 9612 KB Output is correct
21 Correct 52 ms 9732 KB Output is correct
22 Correct 56 ms 9696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2664 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2660 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2660 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2660 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2668 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 1 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 1 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2660 KB Output is correct
44 Correct 1 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 1 ms 2664 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 1 ms 2644 KB Output is correct
49 Correct 2 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 6 ms 2912 KB Output is correct
53 Correct 4 ms 2900 KB Output is correct
54 Correct 4 ms 2900 KB Output is correct
55 Correct 4 ms 2904 KB Output is correct
56 Correct 6 ms 3284 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 5 ms 2900 KB Output is correct
59 Correct 4 ms 3028 KB Output is correct
60 Correct 6 ms 2900 KB Output is correct
61 Correct 4 ms 2972 KB Output is correct
62 Correct 4 ms 2900 KB Output is correct
63 Correct 6 ms 2984 KB Output is correct
64 Correct 4 ms 2900 KB Output is correct
65 Correct 5 ms 3028 KB Output is correct
66 Correct 4 ms 2900 KB Output is correct
67 Correct 5 ms 2900 KB Output is correct
68 Correct 5 ms 3156 KB Output is correct
69 Correct 4 ms 2900 KB Output is correct
70 Correct 3 ms 2900 KB Output is correct
71 Correct 4 ms 2900 KB Output is correct
72 Correct 4 ms 2900 KB Output is correct
73 Correct 3 ms 2900 KB Output is correct
74 Correct 4 ms 2924 KB Output is correct
75 Correct 4 ms 3156 KB Output is correct
76 Correct 4 ms 2900 KB Output is correct
77 Correct 4 ms 3284 KB Output is correct
78 Correct 4 ms 2928 KB Output is correct
79 Correct 4 ms 3204 KB Output is correct
80 Correct 4 ms 2900 KB Output is correct
81 Correct 3 ms 2900 KB Output is correct
82 Correct 4 ms 2900 KB Output is correct
83 Correct 3 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2664 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2660 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2660 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2660 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2668 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 1 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 1 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2660 KB Output is correct
44 Correct 1 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 1 ms 2664 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 1 ms 2644 KB Output is correct
49 Correct 2 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 2 ms 2644 KB Output is correct
53 Correct 6 ms 3316 KB Output is correct
54 Correct 62 ms 9392 KB Output is correct
55 Correct 68 ms 9392 KB Output is correct
56 Correct 66 ms 9400 KB Output is correct
57 Correct 56 ms 9320 KB Output is correct
58 Correct 66 ms 17088 KB Output is correct
59 Correct 62 ms 14228 KB Output is correct
60 Correct 61 ms 14308 KB Output is correct
61 Correct 56 ms 9868 KB Output is correct
62 Correct 63 ms 9484 KB Output is correct
63 Correct 66 ms 9248 KB Output is correct
64 Correct 55 ms 9332 KB Output is correct
65 Correct 55 ms 12772 KB Output is correct
66 Correct 93 ms 17472 KB Output is correct
67 Correct 70 ms 19412 KB Output is correct
68 Correct 55 ms 9240 KB Output is correct
69 Correct 56 ms 9308 KB Output is correct
70 Correct 70 ms 9320 KB Output is correct
71 Correct 52 ms 9612 KB Output is correct
72 Correct 52 ms 9732 KB Output is correct
73 Correct 56 ms 9696 KB Output is correct
74 Correct 6 ms 2912 KB Output is correct
75 Correct 4 ms 2900 KB Output is correct
76 Correct 4 ms 2900 KB Output is correct
77 Correct 4 ms 2904 KB Output is correct
78 Correct 6 ms 3284 KB Output is correct
79 Correct 3 ms 2900 KB Output is correct
80 Correct 5 ms 2900 KB Output is correct
81 Correct 4 ms 3028 KB Output is correct
82 Correct 6 ms 2900 KB Output is correct
83 Correct 4 ms 2972 KB Output is correct
84 Correct 4 ms 2900 KB Output is correct
85 Correct 6 ms 2984 KB Output is correct
86 Correct 4 ms 2900 KB Output is correct
87 Correct 5 ms 3028 KB Output is correct
88 Correct 4 ms 2900 KB Output is correct
89 Correct 5 ms 2900 KB Output is correct
90 Correct 5 ms 3156 KB Output is correct
91 Correct 4 ms 2900 KB Output is correct
92 Correct 3 ms 2900 KB Output is correct
93 Correct 4 ms 2900 KB Output is correct
94 Correct 4 ms 2900 KB Output is correct
95 Correct 3 ms 2900 KB Output is correct
96 Correct 4 ms 2924 KB Output is correct
97 Correct 4 ms 3156 KB Output is correct
98 Correct 4 ms 2900 KB Output is correct
99 Correct 4 ms 3284 KB Output is correct
100 Correct 4 ms 2928 KB Output is correct
101 Correct 4 ms 3204 KB Output is correct
102 Correct 4 ms 2900 KB Output is correct
103 Correct 3 ms 2900 KB Output is correct
104 Correct 4 ms 2900 KB Output is correct
105 Correct 3 ms 2900 KB Output is correct
106 Correct 59 ms 9348 KB Output is correct
107 Correct 67 ms 9524 KB Output is correct
108 Correct 63 ms 9440 KB Output is correct
109 Correct 64 ms 9576 KB Output is correct
110 Correct 63 ms 9796 KB Output is correct
111 Correct 60 ms 10744 KB Output is correct
112 Correct 106 ms 15656 KB Output is correct
113 Correct 61 ms 9468 KB Output is correct
114 Correct 63 ms 9388 KB Output is correct
115 Correct 50 ms 9668 KB Output is correct
116 Correct 66 ms 16592 KB Output is correct
117 Correct 94 ms 15548 KB Output is correct
118 Correct 57 ms 10436 KB Output is correct
119 Correct 65 ms 9420 KB Output is correct
120 Correct 69 ms 9468 KB Output is correct
121 Correct 69 ms 9480 KB Output is correct
122 Correct 70 ms 9512 KB Output is correct
123 Correct 54 ms 9764 KB Output is correct
124 Correct 52 ms 9776 KB Output is correct
125 Correct 51 ms 9720 KB Output is correct
126 Correct 63 ms 9440 KB Output is correct
127 Correct 63 ms 9496 KB Output is correct
128 Correct 65 ms 9484 KB Output is correct
129 Correct 64 ms 9576 KB Output is correct
130 Correct 69 ms 9984 KB Output is correct
131 Correct 64 ms 10652 KB Output is correct
132 Correct 72 ms 15472 KB Output is correct
133 Correct 61 ms 9464 KB Output is correct
134 Correct 79 ms 9448 KB Output is correct
135 Correct 53 ms 9616 KB Output is correct
136 Correct 85 ms 14956 KB Output is correct
137 Correct 74 ms 15116 KB Output is correct
138 Correct 50 ms 10472 KB Output is correct
139 Correct 68 ms 9384 KB Output is correct
140 Correct 63 ms 9504 KB Output is correct
141 Correct 69 ms 9528 KB Output is correct
142 Correct 69 ms 9572 KB Output is correct
143 Correct 51 ms 9812 KB Output is correct
144 Correct 51 ms 9808 KB Output is correct
145 Correct 53 ms 9712 KB Output is correct
146 Correct 87 ms 9356 KB Output is correct
147 Correct 70 ms 9320 KB Output is correct
148 Correct 71 ms 9888 KB Output is correct
149 Correct 74 ms 10944 KB Output is correct
150 Correct 91 ms 18272 KB Output is correct
151 Correct 87 ms 9508 KB Output is correct
152 Correct 101 ms 9748 KB Output is correct
153 Correct 78 ms 9772 KB Output is correct
154 Correct 63 ms 10916 KB Output is correct
155 Correct 69 ms 9768 KB Output is correct
156 Correct 80 ms 16072 KB Output is correct
157 Correct 93 ms 15156 KB Output is correct
158 Correct 82 ms 14248 KB Output is correct
159 Correct 75 ms 9296 KB Output is correct
160 Correct 90 ms 16664 KB Output is correct
161 Correct 62 ms 9416 KB Output is correct
162 Correct 63 ms 9800 KB Output is correct