Submission #634237

# Submission time Handle Problem Language Result Execution time Memory
634237 2022-08-24T07:36:46 Z Mahdi Subtree (INOI20_subtree) C++17
100 / 100
163 ms 22820 KB
#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
const int N=1e5+5, M=1e9+7;
int n, m, dis[N], up[N], dn[N], dp[N], pd[N], a[N], b[N], c[N];
vector<int>g[N], kd[N];
bool mk[N];
pii be[N];

inline int tav(int x, int y){
    int res=1;
    while(y){
        if(y&1)
            res=1LL*res*x%M;
        x=1LL*x*x%M;
        y>>=1;
    }
    return res;
}

inline int rev(const int &x){
    return tav(x, M-2);
}

void dfs(const int &v, const int &p){
    mk[v]=1;
    be[v]={-1, -1};
    for(int u: g[v]){
        if(!mk[u]){
            dis[u]=dis[v]+1;
            kd[v].push_back(u);
            dfs(u, v);
            if(be[u].S!=-1 && up[be[u].S]!=u)
                be[v]={u, be[u].S};
        }
        else if(u!=p){
            if(dis[u]>dis[v])
                dn[v]=u;
            else
                up[v]=u;
        }
    }
    if(up[v]!=-1)
        be[p]={v, v};
}

void dfs(const int &v){
    for(int u: kd[v])
        dfs(u);
    if(dn[v]==-1){
        ll A=0, B=1;
        for(int u: kd[v]){
            A+=dp[u];
            B=B*(pd[u]+1)%M;
        }
        A+=B;
        dp[v]=A%M;
        pd[v]=B;
        if(be[v].S!=-1){
            int u=be[v].F;
            ll bc=1LL*pd[v]*rev(pd[u]+1)%M;
            a[v]=bc*(a[u]+1)%M;
            a[v]+=c[u];
            if(a[v]>=M)
                a[v]-=M;
            b[v]=b[u]*bc%M;
            c[v]=c[u]+b[v];
            if(c[v]>=M)
                c[v]-=M;
        }
        else if(up[v]!=-1){
            a[v]=0;
            c[v]=pd[v];
            b[v]=pd[v];
        }
    }
    else{
        ll A=0, B=1;
        for(int u: kd[v]){
            A+=dp[u];
            if(u!=be[v].F)
                B=B*(pd[u]+1)%M;
            else
                B=B*(a[u]+1)%M;
        }
        A+=B;
        pd[v]=B;
        dp[v]=A%M;
    }
}

int main(){
    cin>>n>>m;
    for(int i=0;i<m;++i){
        int u, v;
        cin>>u>>v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }   
    memset(up, -1, sizeof(up));
    memset(dn, -1, sizeof(dn));
    dfs(0, -1);
    dfs(0);
    cout<<dp[0]<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5716 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 3 ms 5716 KB Output is correct
10 Correct 3 ms 5716 KB Output is correct
11 Correct 4 ms 5716 KB Output is correct
12 Correct 3 ms 5716 KB Output is correct
13 Correct 4 ms 5716 KB Output is correct
14 Correct 3 ms 5716 KB Output is correct
15 Correct 4 ms 5716 KB Output is correct
16 Correct 3 ms 5716 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5716 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 3 ms 5716 KB Output is correct
21 Correct 3 ms 5716 KB Output is correct
22 Correct 3 ms 5716 KB Output is correct
23 Correct 3 ms 5716 KB Output is correct
24 Correct 3 ms 5716 KB Output is correct
25 Correct 3 ms 5716 KB Output is correct
26 Correct 3 ms 5716 KB Output is correct
27 Correct 3 ms 5716 KB Output is correct
28 Correct 3 ms 5716 KB Output is correct
29 Correct 3 ms 5716 KB Output is correct
30 Correct 3 ms 5716 KB Output is correct
31 Correct 3 ms 5716 KB Output is correct
32 Correct 3 ms 5716 KB Output is correct
33 Correct 3 ms 5716 KB Output is correct
34 Correct 3 ms 5716 KB Output is correct
35 Correct 3 ms 5716 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 3 ms 5716 KB Output is correct
38 Correct 3 ms 5716 KB Output is correct
39 Correct 3 ms 5716 KB Output is correct
40 Correct 3 ms 5808 KB Output is correct
41 Correct 3 ms 5716 KB Output is correct
42 Correct 3 ms 5716 KB Output is correct
43 Correct 3 ms 5716 KB Output is correct
44 Correct 3 ms 5812 KB Output is correct
45 Correct 3 ms 5716 KB Output is correct
46 Correct 4 ms 5716 KB Output is correct
47 Correct 3 ms 5716 KB Output is correct
48 Correct 4 ms 5716 KB Output is correct
49 Correct 3 ms 5716 KB Output is correct
50 Correct 3 ms 5716 KB Output is correct
51 Correct 4 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 9 ms 6484 KB Output is correct
3 Correct 95 ms 12572 KB Output is correct
4 Correct 101 ms 12796 KB Output is correct
5 Correct 118 ms 12668 KB Output is correct
6 Correct 114 ms 12644 KB Output is correct
7 Correct 109 ms 20532 KB Output is correct
8 Correct 105 ms 17440 KB Output is correct
9 Correct 103 ms 16824 KB Output is correct
10 Correct 73 ms 12208 KB Output is correct
11 Correct 82 ms 12588 KB Output is correct
12 Correct 122 ms 13840 KB Output is correct
13 Correct 102 ms 14108 KB Output is correct
14 Correct 83 ms 15400 KB Output is correct
15 Correct 115 ms 21540 KB Output is correct
16 Correct 128 ms 22812 KB Output is correct
17 Correct 124 ms 14024 KB Output is correct
18 Correct 104 ms 14112 KB Output is correct
19 Correct 110 ms 14096 KB Output is correct
20 Correct 83 ms 12228 KB Output is correct
21 Correct 75 ms 11980 KB Output is correct
22 Correct 74 ms 11772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5716 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 3 ms 5716 KB Output is correct
10 Correct 3 ms 5716 KB Output is correct
11 Correct 4 ms 5716 KB Output is correct
12 Correct 3 ms 5716 KB Output is correct
13 Correct 4 ms 5716 KB Output is correct
14 Correct 3 ms 5716 KB Output is correct
15 Correct 4 ms 5716 KB Output is correct
16 Correct 3 ms 5716 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5716 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 3 ms 5716 KB Output is correct
21 Correct 3 ms 5716 KB Output is correct
22 Correct 3 ms 5716 KB Output is correct
23 Correct 3 ms 5716 KB Output is correct
24 Correct 3 ms 5716 KB Output is correct
25 Correct 3 ms 5716 KB Output is correct
26 Correct 3 ms 5716 KB Output is correct
27 Correct 3 ms 5716 KB Output is correct
28 Correct 3 ms 5716 KB Output is correct
29 Correct 3 ms 5716 KB Output is correct
30 Correct 3 ms 5716 KB Output is correct
31 Correct 3 ms 5716 KB Output is correct
32 Correct 3 ms 5716 KB Output is correct
33 Correct 3 ms 5716 KB Output is correct
34 Correct 3 ms 5716 KB Output is correct
35 Correct 3 ms 5716 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 3 ms 5716 KB Output is correct
38 Correct 3 ms 5716 KB Output is correct
39 Correct 3 ms 5716 KB Output is correct
40 Correct 3 ms 5808 KB Output is correct
41 Correct 3 ms 5716 KB Output is correct
42 Correct 3 ms 5716 KB Output is correct
43 Correct 3 ms 5716 KB Output is correct
44 Correct 3 ms 5812 KB Output is correct
45 Correct 3 ms 5716 KB Output is correct
46 Correct 4 ms 5716 KB Output is correct
47 Correct 3 ms 5716 KB Output is correct
48 Correct 4 ms 5716 KB Output is correct
49 Correct 3 ms 5716 KB Output is correct
50 Correct 3 ms 5716 KB Output is correct
51 Correct 4 ms 5716 KB Output is correct
52 Correct 7 ms 6100 KB Output is correct
53 Correct 9 ms 6176 KB Output is correct
54 Correct 9 ms 6228 KB Output is correct
55 Correct 7 ms 6180 KB Output is correct
56 Correct 9 ms 6612 KB Output is correct
57 Correct 8 ms 6176 KB Output is correct
58 Correct 6 ms 6100 KB Output is correct
59 Correct 6 ms 6356 KB Output is correct
60 Correct 6 ms 6228 KB Output is correct
61 Correct 7 ms 6300 KB Output is correct
62 Correct 8 ms 6228 KB Output is correct
63 Correct 6 ms 6176 KB Output is correct
64 Correct 6 ms 6100 KB Output is correct
65 Correct 8 ms 6228 KB Output is correct
66 Correct 10 ms 6148 KB Output is correct
67 Correct 7 ms 6228 KB Output is correct
68 Correct 8 ms 6564 KB Output is correct
69 Correct 9 ms 6304 KB Output is correct
70 Correct 8 ms 6144 KB Output is correct
71 Correct 7 ms 6228 KB Output is correct
72 Correct 9 ms 6304 KB Output is correct
73 Correct 7 ms 6100 KB Output is correct
74 Correct 8 ms 6332 KB Output is correct
75 Correct 8 ms 6428 KB Output is correct
76 Correct 7 ms 6228 KB Output is correct
77 Correct 7 ms 6612 KB Output is correct
78 Correct 8 ms 6300 KB Output is correct
79 Correct 7 ms 6612 KB Output is correct
80 Correct 8 ms 6300 KB Output is correct
81 Correct 7 ms 6304 KB Output is correct
82 Correct 8 ms 6356 KB Output is correct
83 Correct 6 ms 6176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5716 KB Output is correct
2 Correct 3 ms 5716 KB Output is correct
3 Correct 3 ms 5716 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5716 KB Output is correct
6 Correct 3 ms 5716 KB Output is correct
7 Correct 3 ms 5716 KB Output is correct
8 Correct 3 ms 5716 KB Output is correct
9 Correct 3 ms 5716 KB Output is correct
10 Correct 3 ms 5716 KB Output is correct
11 Correct 4 ms 5716 KB Output is correct
12 Correct 3 ms 5716 KB Output is correct
13 Correct 4 ms 5716 KB Output is correct
14 Correct 3 ms 5716 KB Output is correct
15 Correct 4 ms 5716 KB Output is correct
16 Correct 3 ms 5716 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5716 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 3 ms 5716 KB Output is correct
21 Correct 3 ms 5716 KB Output is correct
22 Correct 3 ms 5716 KB Output is correct
23 Correct 3 ms 5716 KB Output is correct
24 Correct 3 ms 5716 KB Output is correct
25 Correct 3 ms 5716 KB Output is correct
26 Correct 3 ms 5716 KB Output is correct
27 Correct 3 ms 5716 KB Output is correct
28 Correct 3 ms 5716 KB Output is correct
29 Correct 3 ms 5716 KB Output is correct
30 Correct 3 ms 5716 KB Output is correct
31 Correct 3 ms 5716 KB Output is correct
32 Correct 3 ms 5716 KB Output is correct
33 Correct 3 ms 5716 KB Output is correct
34 Correct 3 ms 5716 KB Output is correct
35 Correct 3 ms 5716 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 3 ms 5716 KB Output is correct
38 Correct 3 ms 5716 KB Output is correct
39 Correct 3 ms 5716 KB Output is correct
40 Correct 3 ms 5808 KB Output is correct
41 Correct 3 ms 5716 KB Output is correct
42 Correct 3 ms 5716 KB Output is correct
43 Correct 3 ms 5716 KB Output is correct
44 Correct 3 ms 5812 KB Output is correct
45 Correct 3 ms 5716 KB Output is correct
46 Correct 4 ms 5716 KB Output is correct
47 Correct 3 ms 5716 KB Output is correct
48 Correct 4 ms 5716 KB Output is correct
49 Correct 3 ms 5716 KB Output is correct
50 Correct 3 ms 5716 KB Output is correct
51 Correct 4 ms 5716 KB Output is correct
52 Correct 3 ms 5716 KB Output is correct
53 Correct 9 ms 6484 KB Output is correct
54 Correct 95 ms 12572 KB Output is correct
55 Correct 101 ms 12796 KB Output is correct
56 Correct 118 ms 12668 KB Output is correct
57 Correct 114 ms 12644 KB Output is correct
58 Correct 109 ms 20532 KB Output is correct
59 Correct 105 ms 17440 KB Output is correct
60 Correct 103 ms 16824 KB Output is correct
61 Correct 73 ms 12208 KB Output is correct
62 Correct 82 ms 12588 KB Output is correct
63 Correct 122 ms 13840 KB Output is correct
64 Correct 102 ms 14108 KB Output is correct
65 Correct 83 ms 15400 KB Output is correct
66 Correct 115 ms 21540 KB Output is correct
67 Correct 128 ms 22812 KB Output is correct
68 Correct 124 ms 14024 KB Output is correct
69 Correct 104 ms 14112 KB Output is correct
70 Correct 110 ms 14096 KB Output is correct
71 Correct 83 ms 12228 KB Output is correct
72 Correct 75 ms 11980 KB Output is correct
73 Correct 74 ms 11772 KB Output is correct
74 Correct 7 ms 6100 KB Output is correct
75 Correct 9 ms 6176 KB Output is correct
76 Correct 9 ms 6228 KB Output is correct
77 Correct 7 ms 6180 KB Output is correct
78 Correct 9 ms 6612 KB Output is correct
79 Correct 8 ms 6176 KB Output is correct
80 Correct 6 ms 6100 KB Output is correct
81 Correct 6 ms 6356 KB Output is correct
82 Correct 6 ms 6228 KB Output is correct
83 Correct 7 ms 6300 KB Output is correct
84 Correct 8 ms 6228 KB Output is correct
85 Correct 6 ms 6176 KB Output is correct
86 Correct 6 ms 6100 KB Output is correct
87 Correct 8 ms 6228 KB Output is correct
88 Correct 10 ms 6148 KB Output is correct
89 Correct 7 ms 6228 KB Output is correct
90 Correct 8 ms 6564 KB Output is correct
91 Correct 9 ms 6304 KB Output is correct
92 Correct 8 ms 6144 KB Output is correct
93 Correct 7 ms 6228 KB Output is correct
94 Correct 9 ms 6304 KB Output is correct
95 Correct 7 ms 6100 KB Output is correct
96 Correct 8 ms 6332 KB Output is correct
97 Correct 8 ms 6428 KB Output is correct
98 Correct 7 ms 6228 KB Output is correct
99 Correct 7 ms 6612 KB Output is correct
100 Correct 8 ms 6300 KB Output is correct
101 Correct 7 ms 6612 KB Output is correct
102 Correct 8 ms 6300 KB Output is correct
103 Correct 7 ms 6304 KB Output is correct
104 Correct 8 ms 6356 KB Output is correct
105 Correct 6 ms 6176 KB Output is correct
106 Correct 127 ms 15020 KB Output is correct
107 Correct 112 ms 15032 KB Output is correct
108 Correct 133 ms 15144 KB Output is correct
109 Correct 144 ms 15128 KB Output is correct
110 Correct 77 ms 13900 KB Output is correct
111 Correct 103 ms 15276 KB Output is correct
112 Correct 138 ms 21308 KB Output is correct
113 Correct 145 ms 16436 KB Output is correct
114 Correct 119 ms 16052 KB Output is correct
115 Correct 79 ms 13312 KB Output is correct
116 Correct 126 ms 22496 KB Output is correct
117 Correct 140 ms 21324 KB Output is correct
118 Correct 82 ms 15216 KB Output is correct
119 Correct 121 ms 16376 KB Output is correct
120 Correct 135 ms 16560 KB Output is correct
121 Correct 141 ms 16288 KB Output is correct
122 Correct 130 ms 16292 KB Output is correct
123 Correct 76 ms 13296 KB Output is correct
124 Correct 75 ms 13044 KB Output is correct
125 Correct 78 ms 13388 KB Output is correct
126 Correct 136 ms 15052 KB Output is correct
127 Correct 107 ms 15052 KB Output is correct
128 Correct 110 ms 15120 KB Output is correct
129 Correct 127 ms 15184 KB Output is correct
130 Correct 77 ms 13856 KB Output is correct
131 Correct 95 ms 15384 KB Output is correct
132 Correct 135 ms 21164 KB Output is correct
133 Correct 116 ms 16384 KB Output is correct
134 Correct 124 ms 16164 KB Output is correct
135 Correct 83 ms 13412 KB Output is correct
136 Correct 129 ms 21104 KB Output is correct
137 Correct 128 ms 21040 KB Output is correct
138 Correct 78 ms 15368 KB Output is correct
139 Correct 143 ms 16452 KB Output is correct
140 Correct 147 ms 16296 KB Output is correct
141 Correct 146 ms 16300 KB Output is correct
142 Correct 148 ms 16332 KB Output is correct
143 Correct 98 ms 13396 KB Output is correct
144 Correct 81 ms 13128 KB Output is correct
145 Correct 74 ms 13376 KB Output is correct
146 Correct 148 ms 16268 KB Output is correct
147 Correct 126 ms 16388 KB Output is correct
148 Correct 145 ms 16808 KB Output is correct
149 Correct 128 ms 17588 KB Output is correct
150 Correct 128 ms 22820 KB Output is correct
151 Correct 139 ms 16436 KB Output is correct
152 Correct 163 ms 16156 KB Output is correct
153 Correct 130 ms 16276 KB Output is correct
154 Correct 121 ms 17108 KB Output is correct
155 Correct 85 ms 14416 KB Output is correct
156 Correct 141 ms 21964 KB Output is correct
157 Correct 143 ms 21284 KB Output is correct
158 Correct 139 ms 20576 KB Output is correct
159 Correct 120 ms 16316 KB Output is correct
160 Correct 130 ms 22532 KB Output is correct
161 Correct 108 ms 16500 KB Output is correct
162 Correct 84 ms 14616 KB Output is correct