Submission #636000

# Submission time Handle Problem Language Result Execution time Memory
636000 2022-08-27T14:11:26 Z K4YAN Subtree (INOI20_subtree) C++17
100 / 100
109 ms 32708 KB
//Be Name Khoda

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define all(x) x.begin(), x.end()

const ll mxn = 1e5 + 16, INF = 1e16 + 16, md = 1e9 + 7;
ll n, m, q, w, cnt;
ll dp[mxn], blg[mxn], par[mxn], dpall[mxn], cpar[mxn];
vector<ll> adj[mxn], C[mxn], cdp[mxn];
bitset<mxn> mark, dor;

inline ll tav(ll a, ll b) {
    ll res = 1;
    while(b > 0) {
        if(b & 1) res = res * a % md;
        a = a * a % md, b >>= 1;
    } return res;
}

inline void input() {

    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        cin >> q >> w;
        adj[q].push_back(w);
        adj[w].push_back(q);
    }

}

void DFS(int v) {
    mark.set(v);
    for(auto u : adj[v]) {
        if(u == par[v]) continue;
        if(!mark[u]) {
            par[u] = v;
            DFS(u);
        } else {
            if(dor[v]) continue;
            cnt++, q = v;
            while(q != u) {
                C[cnt].push_back(q);
                dor.set(q); blg[q] = cnt;
                q = par[q];
                
            } C[cnt].push_back(u);
            dor.set(u); blg[u] = cnt;
            continue;
        }
    }
    if(!dor[v]) {
        cnt++;
        dor.set(v); blg[v] = cnt;
        C[cnt].push_back(v);
    }
}
void SAGZAN(int cy, int v) {
    mark.set(cy);
    bool barg = 1;
    if(v != -1) {
        int id = 0;
        for(auto u : C[cy]) {
            if(u == v) {
                break;
            } id++;
        }
        vector<ll> f;
        f.push_back(C[cy][id]);
        for(int i = id + 1; i < int(C[cy].size()); i++) {
            f.push_back(C[cy][i]);
        } for(int i = 0; i < id; i++) {
            f.push_back(C[cy][i]);
        } C[cy] = f;
    }
    for(auto x : C[cy]) {
        for(auto u : adj[x]) {
            if(mark[blg[u]]) continue;
            barg = 0;
            cpar[blg[u]] = cy;
            SAGZAN(blg[u], u);
        }
    }
    if(barg) {
        q = int(C[cy].size());
        if(q == 1) {
            dpall[cy] = 1;
            dp[C[cy][0]] = 1;
            return;
        }
        dpall[cy] = q * (q - 1) % md;
        for(auto u : C[cy]) {
            dp[u] = (q * (q - 1) / 2) % md;
            dp[u] %= md;
        } return;
    } 
    q = int(C[cy].size());
    if(q == 1) {
        for(auto u : adj[C[cy][0]]) {
            if(blg[u] == cpar[cy]) continue;
            q *= (dp[u] + 1);
            q %= md;
        } dp[C[cy][0]] = dpall[cy] = q;
        return;
    }

    ll sum = 0, ans = 0; w = 1;
    vector<ll> ps;
    for(auto x : C[cy]) {
        q = 1;
        for(auto u : adj[x]) {
            if(blg[u] == cpar[cy]) continue;
            if(blg[u] == cy) continue;
            q *= (dp[u] + 1);
            q %= md;
        } cdp[cy].push_back(q);
        w = w * q % md;
        sum = (sum + w) % md;
        ps.push_back(sum);
    } ans = ps[int(ps.size()) - 2];
    w = 1, sum = ps[int(ps.size()) - 2];
    for(int i = int(C[cy].size()) - 1; i > 1; i--) {
        sum = (sum - (w * (ps[i - 1] - ps[i - 2]) % md) + md) % md;
        sum = sum * cdp[cy][i] % md;
        w = w * cdp[cy][i] % md;
        ans = (ans + sum) % md;
    } if(v > 0) dp[v] = ans;
    ans = sum = ps[int(ps.size()) - 2], w = 1;
    for(int i = int(C[cy].size()) - 1; i > 0; i--) {
        if(i == 1) {
            sum = (sum - ps[i - 1] * w % md + md) % md;    
        }
        else sum = (sum - (ps[i - 1] - ps[i - 2]) * w % md + md) % md;
        sum = (sum * cdp[cy][i] % md + cdp[cy][i]) % md;
        w = w * cdp[cy][i] % md;
        ans = (ans + sum) % md;
    } dpall[cy] = ans;
    return;
 
}

inline void solve() {

    DFS(1);
    mark.reset();
    fill(dp, dp + mxn, 1);
    SAGZAN(1, -1);
    ll ans = 0;
    for(int i = 1; i <= cnt; i++) {
        ans += dpall[i];
        ans %= md;
    } cout << ans << endl;
    return;

}

int main() {

    ios::sync_with_stdio(false); cin.tie(NULL);

    input(), solve();

    return 0;

}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 4 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 5 ms 8144 KB Output is correct
10 Correct 4 ms 8168 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 6 ms 8128 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 5 ms 8148 KB Output is correct
15 Correct 4 ms 8144 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8068 KB Output is correct
18 Correct 4 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 4 ms 8148 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 4 ms 8148 KB Output is correct
23 Correct 4 ms 8148 KB Output is correct
24 Correct 5 ms 8132 KB Output is correct
25 Correct 4 ms 8148 KB Output is correct
26 Correct 4 ms 8148 KB Output is correct
27 Correct 4 ms 8148 KB Output is correct
28 Correct 4 ms 8148 KB Output is correct
29 Correct 4 ms 8148 KB Output is correct
30 Correct 5 ms 8148 KB Output is correct
31 Correct 6 ms 8148 KB Output is correct
32 Correct 5 ms 8148 KB Output is correct
33 Correct 5 ms 8148 KB Output is correct
34 Correct 5 ms 8148 KB Output is correct
35 Correct 6 ms 8144 KB Output is correct
36 Correct 4 ms 8148 KB Output is correct
37 Correct 5 ms 8148 KB Output is correct
38 Correct 4 ms 8148 KB Output is correct
39 Correct 5 ms 8276 KB Output is correct
40 Correct 4 ms 8148 KB Output is correct
41 Correct 4 ms 8148 KB Output is correct
42 Correct 4 ms 8148 KB Output is correct
43 Correct 4 ms 8148 KB Output is correct
44 Correct 4 ms 8148 KB Output is correct
45 Correct 4 ms 8144 KB Output is correct
46 Correct 4 ms 8148 KB Output is correct
47 Correct 5 ms 8240 KB Output is correct
48 Correct 4 ms 8144 KB Output is correct
49 Correct 4 ms 8148 KB Output is correct
50 Correct 4 ms 8148 KB Output is correct
51 Correct 4 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8152 KB Output is correct
2 Correct 11 ms 9172 KB Output is correct
3 Correct 81 ms 19304 KB Output is correct
4 Correct 78 ms 19320 KB Output is correct
5 Correct 73 ms 19284 KB Output is correct
6 Correct 72 ms 19316 KB Output is correct
7 Correct 87 ms 28932 KB Output is correct
8 Correct 86 ms 25420 KB Output is correct
9 Correct 84 ms 25644 KB Output is correct
10 Correct 73 ms 19924 KB Output is correct
11 Correct 60 ms 19260 KB Output is correct
12 Correct 71 ms 18816 KB Output is correct
13 Correct 72 ms 18764 KB Output is correct
14 Correct 72 ms 23480 KB Output is correct
15 Correct 100 ms 31444 KB Output is correct
16 Correct 101 ms 32708 KB Output is correct
17 Correct 69 ms 18764 KB Output is correct
18 Correct 79 ms 18776 KB Output is correct
19 Correct 71 ms 18776 KB Output is correct
20 Correct 63 ms 19528 KB Output is correct
21 Correct 80 ms 19544 KB Output is correct
22 Correct 56 ms 19460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 4 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 5 ms 8144 KB Output is correct
10 Correct 4 ms 8168 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 6 ms 8128 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 5 ms 8148 KB Output is correct
15 Correct 4 ms 8144 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8068 KB Output is correct
18 Correct 4 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 4 ms 8148 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 4 ms 8148 KB Output is correct
23 Correct 4 ms 8148 KB Output is correct
24 Correct 5 ms 8132 KB Output is correct
25 Correct 4 ms 8148 KB Output is correct
26 Correct 4 ms 8148 KB Output is correct
27 Correct 4 ms 8148 KB Output is correct
28 Correct 4 ms 8148 KB Output is correct
29 Correct 4 ms 8148 KB Output is correct
30 Correct 5 ms 8148 KB Output is correct
31 Correct 6 ms 8148 KB Output is correct
32 Correct 5 ms 8148 KB Output is correct
33 Correct 5 ms 8148 KB Output is correct
34 Correct 5 ms 8148 KB Output is correct
35 Correct 6 ms 8144 KB Output is correct
36 Correct 4 ms 8148 KB Output is correct
37 Correct 5 ms 8148 KB Output is correct
38 Correct 4 ms 8148 KB Output is correct
39 Correct 5 ms 8276 KB Output is correct
40 Correct 4 ms 8148 KB Output is correct
41 Correct 4 ms 8148 KB Output is correct
42 Correct 4 ms 8148 KB Output is correct
43 Correct 4 ms 8148 KB Output is correct
44 Correct 4 ms 8148 KB Output is correct
45 Correct 4 ms 8144 KB Output is correct
46 Correct 4 ms 8148 KB Output is correct
47 Correct 5 ms 8240 KB Output is correct
48 Correct 4 ms 8144 KB Output is correct
49 Correct 4 ms 8148 KB Output is correct
50 Correct 4 ms 8148 KB Output is correct
51 Correct 4 ms 8148 KB Output is correct
52 Correct 6 ms 8676 KB Output is correct
53 Correct 7 ms 8628 KB Output is correct
54 Correct 7 ms 8672 KB Output is correct
55 Correct 6 ms 8752 KB Output is correct
56 Correct 9 ms 9044 KB Output is correct
57 Correct 6 ms 8620 KB Output is correct
58 Correct 6 ms 8744 KB Output is correct
59 Correct 6 ms 8796 KB Output is correct
60 Correct 6 ms 8660 KB Output is correct
61 Correct 8 ms 8664 KB Output is correct
62 Correct 8 ms 8660 KB Output is correct
63 Correct 6 ms 8732 KB Output is correct
64 Correct 7 ms 8668 KB Output is correct
65 Correct 6 ms 8660 KB Output is correct
66 Correct 7 ms 8624 KB Output is correct
67 Correct 6 ms 8748 KB Output is correct
68 Correct 7 ms 8892 KB Output is correct
69 Correct 6 ms 8668 KB Output is correct
70 Correct 6 ms 8660 KB Output is correct
71 Correct 7 ms 8664 KB Output is correct
72 Correct 7 ms 8604 KB Output is correct
73 Correct 7 ms 8660 KB Output is correct
74 Correct 6 ms 8532 KB Output is correct
75 Correct 7 ms 8800 KB Output is correct
76 Correct 7 ms 8660 KB Output is correct
77 Correct 7 ms 8916 KB Output is correct
78 Correct 7 ms 8660 KB Output is correct
79 Correct 7 ms 8916 KB Output is correct
80 Correct 6 ms 8532 KB Output is correct
81 Correct 6 ms 8660 KB Output is correct
82 Correct 6 ms 8672 KB Output is correct
83 Correct 6 ms 8660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 4 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 5 ms 8144 KB Output is correct
10 Correct 4 ms 8168 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 6 ms 8128 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 5 ms 8148 KB Output is correct
15 Correct 4 ms 8144 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8068 KB Output is correct
18 Correct 4 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 4 ms 8148 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 4 ms 8148 KB Output is correct
23 Correct 4 ms 8148 KB Output is correct
24 Correct 5 ms 8132 KB Output is correct
25 Correct 4 ms 8148 KB Output is correct
26 Correct 4 ms 8148 KB Output is correct
27 Correct 4 ms 8148 KB Output is correct
28 Correct 4 ms 8148 KB Output is correct
29 Correct 4 ms 8148 KB Output is correct
30 Correct 5 ms 8148 KB Output is correct
31 Correct 6 ms 8148 KB Output is correct
32 Correct 5 ms 8148 KB Output is correct
33 Correct 5 ms 8148 KB Output is correct
34 Correct 5 ms 8148 KB Output is correct
35 Correct 6 ms 8144 KB Output is correct
36 Correct 4 ms 8148 KB Output is correct
37 Correct 5 ms 8148 KB Output is correct
38 Correct 4 ms 8148 KB Output is correct
39 Correct 5 ms 8276 KB Output is correct
40 Correct 4 ms 8148 KB Output is correct
41 Correct 4 ms 8148 KB Output is correct
42 Correct 4 ms 8148 KB Output is correct
43 Correct 4 ms 8148 KB Output is correct
44 Correct 4 ms 8148 KB Output is correct
45 Correct 4 ms 8144 KB Output is correct
46 Correct 4 ms 8148 KB Output is correct
47 Correct 5 ms 8240 KB Output is correct
48 Correct 4 ms 8144 KB Output is correct
49 Correct 4 ms 8148 KB Output is correct
50 Correct 4 ms 8148 KB Output is correct
51 Correct 4 ms 8148 KB Output is correct
52 Correct 5 ms 8152 KB Output is correct
53 Correct 11 ms 9172 KB Output is correct
54 Correct 81 ms 19304 KB Output is correct
55 Correct 78 ms 19320 KB Output is correct
56 Correct 73 ms 19284 KB Output is correct
57 Correct 72 ms 19316 KB Output is correct
58 Correct 87 ms 28932 KB Output is correct
59 Correct 86 ms 25420 KB Output is correct
60 Correct 84 ms 25644 KB Output is correct
61 Correct 73 ms 19924 KB Output is correct
62 Correct 60 ms 19260 KB Output is correct
63 Correct 71 ms 18816 KB Output is correct
64 Correct 72 ms 18764 KB Output is correct
65 Correct 72 ms 23480 KB Output is correct
66 Correct 100 ms 31444 KB Output is correct
67 Correct 101 ms 32708 KB Output is correct
68 Correct 69 ms 18764 KB Output is correct
69 Correct 79 ms 18776 KB Output is correct
70 Correct 71 ms 18776 KB Output is correct
71 Correct 63 ms 19528 KB Output is correct
72 Correct 80 ms 19544 KB Output is correct
73 Correct 56 ms 19460 KB Output is correct
74 Correct 6 ms 8676 KB Output is correct
75 Correct 7 ms 8628 KB Output is correct
76 Correct 7 ms 8672 KB Output is correct
77 Correct 6 ms 8752 KB Output is correct
78 Correct 9 ms 9044 KB Output is correct
79 Correct 6 ms 8620 KB Output is correct
80 Correct 6 ms 8744 KB Output is correct
81 Correct 6 ms 8796 KB Output is correct
82 Correct 6 ms 8660 KB Output is correct
83 Correct 8 ms 8664 KB Output is correct
84 Correct 8 ms 8660 KB Output is correct
85 Correct 6 ms 8732 KB Output is correct
86 Correct 7 ms 8668 KB Output is correct
87 Correct 6 ms 8660 KB Output is correct
88 Correct 7 ms 8624 KB Output is correct
89 Correct 6 ms 8748 KB Output is correct
90 Correct 7 ms 8892 KB Output is correct
91 Correct 6 ms 8668 KB Output is correct
92 Correct 6 ms 8660 KB Output is correct
93 Correct 7 ms 8664 KB Output is correct
94 Correct 7 ms 8604 KB Output is correct
95 Correct 7 ms 8660 KB Output is correct
96 Correct 6 ms 8532 KB Output is correct
97 Correct 7 ms 8800 KB Output is correct
98 Correct 7 ms 8660 KB Output is correct
99 Correct 7 ms 8916 KB Output is correct
100 Correct 7 ms 8660 KB Output is correct
101 Correct 7 ms 8916 KB Output is correct
102 Correct 6 ms 8532 KB Output is correct
103 Correct 6 ms 8660 KB Output is correct
104 Correct 6 ms 8672 KB Output is correct
105 Correct 6 ms 8660 KB Output is correct
106 Correct 72 ms 19216 KB Output is correct
107 Correct 75 ms 19144 KB Output is correct
108 Correct 90 ms 19044 KB Output is correct
109 Correct 78 ms 19076 KB Output is correct
110 Correct 65 ms 19544 KB Output is correct
111 Correct 83 ms 20552 KB Output is correct
112 Correct 103 ms 23656 KB Output is correct
113 Correct 87 ms 17768 KB Output is correct
114 Correct 105 ms 18508 KB Output is correct
115 Correct 54 ms 19348 KB Output is correct
116 Correct 82 ms 23896 KB Output is correct
117 Correct 93 ms 23380 KB Output is correct
118 Correct 72 ms 20056 KB Output is correct
119 Correct 109 ms 17840 KB Output is correct
120 Correct 100 ms 18024 KB Output is correct
121 Correct 78 ms 18252 KB Output is correct
122 Correct 108 ms 18492 KB Output is correct
123 Correct 91 ms 19664 KB Output is correct
124 Correct 62 ms 19668 KB Output is correct
125 Correct 49 ms 19480 KB Output is correct
126 Correct 73 ms 19136 KB Output is correct
127 Correct 76 ms 19100 KB Output is correct
128 Correct 83 ms 19156 KB Output is correct
129 Correct 87 ms 19120 KB Output is correct
130 Correct 64 ms 19532 KB Output is correct
131 Correct 73 ms 20464 KB Output is correct
132 Correct 90 ms 23456 KB Output is correct
133 Correct 99 ms 17712 KB Output is correct
134 Correct 75 ms 18476 KB Output is correct
135 Correct 51 ms 19504 KB Output is correct
136 Correct 76 ms 22476 KB Output is correct
137 Correct 84 ms 23072 KB Output is correct
138 Correct 60 ms 20048 KB Output is correct
139 Correct 85 ms 17816 KB Output is correct
140 Correct 72 ms 18076 KB Output is correct
141 Correct 77 ms 18212 KB Output is correct
142 Correct 83 ms 18468 KB Output is correct
143 Correct 64 ms 19660 KB Output is correct
144 Correct 80 ms 19612 KB Output is correct
145 Correct 50 ms 19460 KB Output is correct
146 Correct 75 ms 16936 KB Output is correct
147 Correct 52 ms 15820 KB Output is correct
148 Correct 55 ms 16012 KB Output is correct
149 Correct 56 ms 17008 KB Output is correct
150 Correct 91 ms 25360 KB Output is correct
151 Correct 95 ms 16852 KB Output is correct
152 Correct 81 ms 18304 KB Output is correct
153 Correct 106 ms 18268 KB Output is correct
154 Correct 74 ms 18320 KB Output is correct
155 Correct 61 ms 19664 KB Output is correct
156 Correct 85 ms 21580 KB Output is correct
157 Correct 61 ms 20872 KB Output is correct
158 Correct 85 ms 20232 KB Output is correct
159 Correct 73 ms 18636 KB Output is correct
160 Correct 99 ms 26796 KB Output is correct
161 Correct 85 ms 18544 KB Output is correct
162 Correct 62 ms 19172 KB Output is correct