답안 #795462

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
795462 2023-07-27T10:06:14 Z MISM06 Subtree (INOI20_subtree) C++14
100 / 100
144 ms 33108 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < int , pii > piii;
typedef pair < ll , ll > pll;
typedef pair < ll , pll > plll;
#define pb  push_back
#define all(x)  x.begin(), x.end()
#define sze     size()
#define F first
#define S second
#define kids    int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1;
#define wall__ cout << "________________________________________\n";

const ll N = 1e5 + 10, mod = 1e9 + 7;

inline void add (ll &x, ll y) {
    x += y; if (x >= mod) x -= mod;
    if (x < 0) x += mod;
}
inline ll Sum (ll x, ll y) {
    x += y; if (x >= mod) x -= mod;
    if (x < 0) x += mod;
    return x;
}
inline ll mul (ll x, ll y) {
    return x * y % mod;
}

int n, m, par[N], beg[N], h[N], id[N];
vector < int > g[N];
bool mark[N];
ll dp[N], pd[N];
vector < ll > pre[N], suf[N];

void dfs0 (int v, int p) {
    par[v] = p;
    mark[v] = 1;
    h[v] = h[p] + 1;
    for (int i = 0; i < g[v].sze; i++) {
        int u = g[v][i];
        if (u == p) continue;
        if (mark[u]) {
            beg[v] = u;
            continue;
        }
        id[u] = i;
        dfs0(u, v);
    }
}

ll get (int v, int i) {
    if (g[v].sze == 1) return 1;
    if (i == 0) return suf[v][1];
    if (i == g[v].sze - 1)  return pre[v][i - 1];
    return mul(pre[v][i - 1], suf[v][i + 1]);
}

void dfs1 (int v, int p) {
    for (auto u : g[v]) {
        if (par[u] != v) {
            pre[v].pb(1);
            suf[v].pb(1);
            continue;
        }
        dfs1(u, v);
        pre[v].pb(dp[u] + 1);
        suf[v].pb(dp[u] + 1); 
    }
    // cout << v << " : \n";
    for (int i = 0; i < g[v].sze; i++) {
        if (i == 0) continue;
        pre[v][i] = mul(pre[v][i - 1], pre[v][i]);
    }
    for (int i = g[v].sze - 1; i >= 0; --i) {
        if (i == g[v].sze - 1) continue;
        suf[v][i] = mul(suf[v][i + 1], suf[v][i]);
    }
    
    if (h[beg[v]] < h[v]) {
        dp[v] = suf[v][0];
        return;
    }
    int b = beg[v];
    
    pd[b] = dp[b];
    int x = par[b], y = b;
    while (h[x] >= h[v]) {
        pd[x] = mul(pd[y], get(x, id[y]));
        y = x; x = par[x];
    }
    vector < int > o;
    o.pb(b);
    x = par[b]; y = b;
    while (h[x] >= h[v]) {
        o.pb(x);
        add(pd[x], pd[y]);
        y = x; x = par[x];
    }
    ll res = 0, t = 0;
    ll s = 1;
    for (int i = o.sze - 1; i >= 1; --i) {
        int x = o[i], y = o[i - 1];
        s = mul(s, get(x, id[y]));
        add(t, s);
        if (i > 1) add(res, mul(s, pd[o[i - 2]]));
    }
    add(dp[v], t);
    // cout << "res = " << res << '\n';
    add(dp[v], res);

}

void solve () {
    
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int v, u; cin >> v >> u;
        g[v].pb(u);
        g[u].pb(v);
    }
    if (n == 1) {
        cout << 1 << '\n';
        return;
    }
    dfs0(1, 0);
    dfs1(1, 0);
    // cout << "dp : "; for (int i = 1; i <= n; i++) cout << dp[i] << " ";
    // cout << '\n';
    ll ans = 0;
    for (int i = 1; i <= n; i++) add(ans, dp[i]);
    cout << ans << '\n';

}   

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    solve();
    return 0;
}

Compilation message

Main.cpp: In function 'void dfs0(int, int)':
Main.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i = 0; i < g[v].sze; i++) {
      |                       ^
Main.cpp: In function 'll get(int, int)':
Main.cpp:57:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     if (i == g[v].sze - 1)  return pre[v][i - 1];
      |         ~~^~~~~~~~~~~~~~~
Main.cpp: In function 'void dfs1(int, int)':
Main.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for (int i = 0; i < g[v].sze; i++) {
      |                       ^
Main.cpp:78:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         if (i == g[v].sze - 1) continue;
      |             ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7376 KB Output is correct
4 Correct 4 ms 7356 KB Output is correct
5 Correct 3 ms 7388 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7376 KB Output is correct
11 Correct 3 ms 7384 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7300 KB Output is correct
17 Correct 3 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 3 ms 7380 KB Output is correct
20 Correct 3 ms 7284 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 3 ms 7384 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 3 ms 7380 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 4 ms 7380 KB Output is correct
30 Correct 3 ms 7380 KB Output is correct
31 Correct 4 ms 7380 KB Output is correct
32 Correct 3 ms 7380 KB Output is correct
33 Correct 3 ms 7380 KB Output is correct
34 Correct 4 ms 7384 KB Output is correct
35 Correct 3 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7380 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7376 KB Output is correct
41 Correct 4 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7360 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 5 ms 7380 KB Output is correct
46 Correct 3 ms 7380 KB Output is correct
47 Correct 4 ms 7380 KB Output is correct
48 Correct 3 ms 7380 KB Output is correct
49 Correct 3 ms 7380 KB Output is correct
50 Correct 3 ms 7380 KB Output is correct
51 Correct 4 ms 7376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 8 ms 8676 KB Output is correct
3 Correct 113 ms 21216 KB Output is correct
4 Correct 76 ms 21180 KB Output is correct
5 Correct 68 ms 21200 KB Output is correct
6 Correct 69 ms 21200 KB Output is correct
7 Correct 121 ms 30392 KB Output is correct
8 Correct 137 ms 27148 KB Output is correct
9 Correct 76 ms 27596 KB Output is correct
10 Correct 64 ms 22732 KB Output is correct
11 Correct 75 ms 21296 KB Output is correct
12 Correct 84 ms 20172 KB Output is correct
13 Correct 103 ms 20000 KB Output is correct
14 Correct 67 ms 25708 KB Output is correct
15 Correct 109 ms 31860 KB Output is correct
16 Correct 91 ms 33108 KB Output is correct
17 Correct 106 ms 20044 KB Output is correct
18 Correct 89 ms 20116 KB Output is correct
19 Correct 67 ms 20056 KB Output is correct
20 Correct 55 ms 21868 KB Output is correct
21 Correct 68 ms 21976 KB Output is correct
22 Correct 75 ms 21952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7376 KB Output is correct
4 Correct 4 ms 7356 KB Output is correct
5 Correct 3 ms 7388 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7376 KB Output is correct
11 Correct 3 ms 7384 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7300 KB Output is correct
17 Correct 3 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 3 ms 7380 KB Output is correct
20 Correct 3 ms 7284 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 3 ms 7384 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 3 ms 7380 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 4 ms 7380 KB Output is correct
30 Correct 3 ms 7380 KB Output is correct
31 Correct 4 ms 7380 KB Output is correct
32 Correct 3 ms 7380 KB Output is correct
33 Correct 3 ms 7380 KB Output is correct
34 Correct 4 ms 7384 KB Output is correct
35 Correct 3 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7380 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7376 KB Output is correct
41 Correct 4 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7360 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 5 ms 7380 KB Output is correct
46 Correct 3 ms 7380 KB Output is correct
47 Correct 4 ms 7380 KB Output is correct
48 Correct 3 ms 7380 KB Output is correct
49 Correct 3 ms 7380 KB Output is correct
50 Correct 3 ms 7380 KB Output is correct
51 Correct 4 ms 7376 KB Output is correct
52 Correct 5 ms 8148 KB Output is correct
53 Correct 5 ms 8160 KB Output is correct
54 Correct 5 ms 8148 KB Output is correct
55 Correct 5 ms 8148 KB Output is correct
56 Correct 6 ms 8660 KB Output is correct
57 Correct 7 ms 8032 KB Output is correct
58 Correct 6 ms 8032 KB Output is correct
59 Correct 5 ms 8276 KB Output is correct
60 Correct 5 ms 8160 KB Output is correct
61 Correct 6 ms 8020 KB Output is correct
62 Correct 6 ms 8076 KB Output is correct
63 Correct 6 ms 8132 KB Output is correct
64 Correct 7 ms 8020 KB Output is correct
65 Correct 6 ms 8148 KB Output is correct
66 Correct 6 ms 8148 KB Output is correct
67 Correct 6 ms 8148 KB Output is correct
68 Correct 6 ms 8532 KB Output is correct
69 Correct 5 ms 8020 KB Output is correct
70 Correct 5 ms 8160 KB Output is correct
71 Correct 6 ms 8032 KB Output is correct
72 Correct 6 ms 8164 KB Output is correct
73 Correct 5 ms 8120 KB Output is correct
74 Correct 6 ms 8020 KB Output is correct
75 Correct 6 ms 8404 KB Output is correct
76 Correct 8 ms 8232 KB Output is correct
77 Correct 6 ms 8540 KB Output is correct
78 Correct 6 ms 8160 KB Output is correct
79 Correct 6 ms 8532 KB Output is correct
80 Correct 6 ms 8148 KB Output is correct
81 Correct 5 ms 8148 KB Output is correct
82 Correct 5 ms 8028 KB Output is correct
83 Correct 5 ms 8160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7376 KB Output is correct
4 Correct 4 ms 7356 KB Output is correct
5 Correct 3 ms 7388 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7376 KB Output is correct
11 Correct 3 ms 7384 KB Output is correct
12 Correct 4 ms 7372 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7300 KB Output is correct
17 Correct 3 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 3 ms 7380 KB Output is correct
20 Correct 3 ms 7284 KB Output is correct
21 Correct 3 ms 7380 KB Output is correct
22 Correct 3 ms 7384 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 3 ms 7380 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 4 ms 7380 KB Output is correct
30 Correct 3 ms 7380 KB Output is correct
31 Correct 4 ms 7380 KB Output is correct
32 Correct 3 ms 7380 KB Output is correct
33 Correct 3 ms 7380 KB Output is correct
34 Correct 4 ms 7384 KB Output is correct
35 Correct 3 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7380 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7376 KB Output is correct
41 Correct 4 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7360 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 5 ms 7380 KB Output is correct
46 Correct 3 ms 7380 KB Output is correct
47 Correct 4 ms 7380 KB Output is correct
48 Correct 3 ms 7380 KB Output is correct
49 Correct 3 ms 7380 KB Output is correct
50 Correct 3 ms 7380 KB Output is correct
51 Correct 4 ms 7376 KB Output is correct
52 Correct 3 ms 7380 KB Output is correct
53 Correct 8 ms 8676 KB Output is correct
54 Correct 113 ms 21216 KB Output is correct
55 Correct 76 ms 21180 KB Output is correct
56 Correct 68 ms 21200 KB Output is correct
57 Correct 69 ms 21200 KB Output is correct
58 Correct 121 ms 30392 KB Output is correct
59 Correct 137 ms 27148 KB Output is correct
60 Correct 76 ms 27596 KB Output is correct
61 Correct 64 ms 22732 KB Output is correct
62 Correct 75 ms 21296 KB Output is correct
63 Correct 84 ms 20172 KB Output is correct
64 Correct 103 ms 20000 KB Output is correct
65 Correct 67 ms 25708 KB Output is correct
66 Correct 109 ms 31860 KB Output is correct
67 Correct 91 ms 33108 KB Output is correct
68 Correct 106 ms 20044 KB Output is correct
69 Correct 89 ms 20116 KB Output is correct
70 Correct 67 ms 20056 KB Output is correct
71 Correct 55 ms 21868 KB Output is correct
72 Correct 68 ms 21976 KB Output is correct
73 Correct 75 ms 21952 KB Output is correct
74 Correct 5 ms 8148 KB Output is correct
75 Correct 5 ms 8160 KB Output is correct
76 Correct 5 ms 8148 KB Output is correct
77 Correct 5 ms 8148 KB Output is correct
78 Correct 6 ms 8660 KB Output is correct
79 Correct 7 ms 8032 KB Output is correct
80 Correct 6 ms 8032 KB Output is correct
81 Correct 5 ms 8276 KB Output is correct
82 Correct 5 ms 8160 KB Output is correct
83 Correct 6 ms 8020 KB Output is correct
84 Correct 6 ms 8076 KB Output is correct
85 Correct 6 ms 8132 KB Output is correct
86 Correct 7 ms 8020 KB Output is correct
87 Correct 6 ms 8148 KB Output is correct
88 Correct 6 ms 8148 KB Output is correct
89 Correct 6 ms 8148 KB Output is correct
90 Correct 6 ms 8532 KB Output is correct
91 Correct 5 ms 8020 KB Output is correct
92 Correct 5 ms 8160 KB Output is correct
93 Correct 6 ms 8032 KB Output is correct
94 Correct 6 ms 8164 KB Output is correct
95 Correct 5 ms 8120 KB Output is correct
96 Correct 6 ms 8020 KB Output is correct
97 Correct 6 ms 8404 KB Output is correct
98 Correct 8 ms 8232 KB Output is correct
99 Correct 6 ms 8540 KB Output is correct
100 Correct 6 ms 8160 KB Output is correct
101 Correct 6 ms 8532 KB Output is correct
102 Correct 6 ms 8148 KB Output is correct
103 Correct 5 ms 8148 KB Output is correct
104 Correct 5 ms 8028 KB Output is correct
105 Correct 5 ms 8160 KB Output is correct
106 Correct 120 ms 22556 KB Output is correct
107 Correct 79 ms 22732 KB Output is correct
108 Correct 74 ms 22788 KB Output is correct
109 Correct 77 ms 22988 KB Output is correct
110 Correct 75 ms 22988 KB Output is correct
111 Correct 85 ms 24932 KB Output is correct
112 Correct 121 ms 30420 KB Output is correct
113 Correct 78 ms 21580 KB Output is correct
114 Correct 110 ms 21976 KB Output is correct
115 Correct 64 ms 21752 KB Output is correct
116 Correct 108 ms 30760 KB Output is correct
117 Correct 99 ms 29784 KB Output is correct
118 Correct 65 ms 24088 KB Output is correct
119 Correct 74 ms 21600 KB Output is correct
120 Correct 91 ms 21976 KB Output is correct
121 Correct 117 ms 22440 KB Output is correct
122 Correct 82 ms 22652 KB Output is correct
123 Correct 57 ms 22620 KB Output is correct
124 Correct 66 ms 22044 KB Output is correct
125 Correct 80 ms 21976 KB Output is correct
126 Correct 119 ms 22576 KB Output is correct
127 Correct 72 ms 22812 KB Output is correct
128 Correct 74 ms 22912 KB Output is correct
129 Correct 78 ms 23004 KB Output is correct
130 Correct 114 ms 23128 KB Output is correct
131 Correct 88 ms 24892 KB Output is correct
132 Correct 95 ms 30068 KB Output is correct
133 Correct 74 ms 21632 KB Output is correct
134 Correct 91 ms 22016 KB Output is correct
135 Correct 66 ms 21732 KB Output is correct
136 Correct 126 ms 28688 KB Output is correct
137 Correct 112 ms 29404 KB Output is correct
138 Correct 58 ms 23996 KB Output is correct
139 Correct 73 ms 21596 KB Output is correct
140 Correct 83 ms 22012 KB Output is correct
141 Correct 78 ms 22404 KB Output is correct
142 Correct 136 ms 22716 KB Output is correct
143 Correct 76 ms 22584 KB Output is correct
144 Correct 62 ms 22036 KB Output is correct
145 Correct 53 ms 21952 KB Output is correct
146 Correct 94 ms 22164 KB Output is correct
147 Correct 72 ms 21324 KB Output is correct
148 Correct 144 ms 21856 KB Output is correct
149 Correct 80 ms 22896 KB Output is correct
150 Correct 90 ms 31308 KB Output is correct
151 Correct 84 ms 22072 KB Output is correct
152 Correct 118 ms 23064 KB Output is correct
153 Correct 141 ms 23208 KB Output is correct
154 Correct 79 ms 23236 KB Output is correct
155 Correct 63 ms 23268 KB Output is correct
156 Correct 96 ms 29328 KB Output is correct
157 Correct 98 ms 28228 KB Output is correct
158 Correct 127 ms 27220 KB Output is correct
159 Correct 89 ms 21188 KB Output is correct
160 Correct 110 ms 30580 KB Output is correct
161 Correct 75 ms 21192 KB Output is correct
162 Correct 70 ms 23060 KB Output is correct