답안 #418678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
418678 2021-06-05T18:07:23 Z parsabahrami Subtree (INOI20_subtree) C++17
100 / 100
257 ms 51160 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O2", "fast-math") 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 5e5 + 10, MOD = 1e9 + 7;
int dp[N], M[N], dp2[N], up[N], P[N], H[N], rt[N], fr[N], to[N], n, m; vector<int> vec[N], adj[N];
int pd[N];

void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
    if (x < 0) x += MOD;
}

int Find(int v) { return !rt[v] ? v : rt[v] = Find(rt[v]); }

void Union(int u, int v) {
    u = Find(u), v = Find(v);
    if (u == v) return;
    vec[v].push_back(u);
    rt[u] = v;
}

void preDFS(int v, int p = -1) {
    M[v] = 1; P[v] = p;
    for (int id : adj[v]) {
        int u = fr[id] ^ to[id] ^ v;
        if (!M[u]) {
            H[u] = H[v] + 1; preDFS(u, v);
        } else if (u != p && H[u] < H[v]) {
            int w = v;
            while (w != u) 
                Union(P[w], w), w = P[w];
        }
    }
}

void DFS(int v, int p = -1) {
    vector<int> tmp;
    for (int &u : vec[v]) 
        for (int &w : adj[u])  
            if (w != p) 
                DFS(w, v);
            else up[v] = u;
}

inline int calc_dp2(vector<int> &A) {
    int ret = 0, S = 0;
    for (int i = 0; i < SZ(A); i++) {
        S = A[i] * 1ll * (S + 1) % MOD;
        add(ret, S);
    }
    return ret;
}

inline int calc_dp1(vector<int> &A) {
    int ret = calc_dp2(A), S = 1;
    fill(pd, pd + SZ(A) + 1, 0);
    for (int i = 0; i < SZ(A); i++) {
        S = 1ll * S * A[i] % MOD;
        pd[i] = S;
        if (i > 0) add(pd[i], pd[i - 1]);
    }
    S = 1;
    for (int i = SZ(A) - 1; i > 1; i--) {
        S = 1ll * S * A[i] % MOD;
        add(ret, 1ll * S * pd[i - 2] % MOD);
    }
    return ret;
}

void dpDFS(int v, int p = -1) {
    vector<int> A;
    if (~p) {
        vector<int> tmp;
        while (vec[v].back() != up[v]) 
            tmp.push_back(vec[v].back()), vec[v].pop_back();
        vec[v].swap(tmp);
        for (int &x : tmp) 
            vec[v].push_back(x);
    }
    for (int &u : vec[v]) {
        A.push_back(1);
        for (int &w : adj[u]) 
            if (w != p) dpDFS(w, v), A.back() = A.back() * 1ll * (dp2[w] + 1) % MOD;
    }
    dp[v] = calc_dp1(A);
    if (SZ(vec[v]) > 1) {
        int S = 1;
        for (int &x : A) 
            S = 1ll * S * x % MOD;
        add(dp[v], -S);
    }
    if (~p) {
        A.pop_back(); 
        add(dp2[v], dp[v] - calc_dp2(A));
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) 
        vec[i] = {i};
    for (int i = 1; i <= m; i++) {
        int u, v; scanf("%d%d", &u, &v);
        adj[fr[i] = u].push_back(i);
        adj[to[i] = v].push_back(i);
    }
    preDFS(1);  
    for (int i = 1; i <= n; i++) 
        Find(i);
    for (int i = 1; i <= n; i++) 
        if (!rt[i]) rt[i] = i;
        
    for (int i = 1; i <= n; i++) {
        vector<int> tm;
        for (int id : adj[i]) { 
            int u = fr[id] ^ to[id] ^ i;
            if (rt[i] != rt[u]) tm.push_back(rt[u]);
        }
        adj[i].swap(tm);
    }
    DFS(rt[1]); 
    dpDFS(rt[1]); 
    int ret = 0;
    for (int i = 1; i <= n; i++) 
        if (rt[i] == i) add(ret, dp[i]);
    printf("%d\n", ret); 
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:112:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 15 ms 23756 KB Output is correct
4 Correct 13 ms 23756 KB Output is correct
5 Correct 13 ms 23792 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 14 ms 23856 KB Output is correct
10 Correct 13 ms 23796 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 14 ms 23860 KB Output is correct
15 Correct 13 ms 23832 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23856 KB Output is correct
18 Correct 14 ms 23824 KB Output is correct
19 Correct 13 ms 23844 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 14 ms 23848 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 13 ms 23860 KB Output is correct
25 Correct 13 ms 23760 KB Output is correct
26 Correct 13 ms 23756 KB Output is correct
27 Correct 13 ms 23756 KB Output is correct
28 Correct 14 ms 23756 KB Output is correct
29 Correct 14 ms 23744 KB Output is correct
30 Correct 14 ms 23756 KB Output is correct
31 Correct 14 ms 23832 KB Output is correct
32 Correct 13 ms 23856 KB Output is correct
33 Correct 14 ms 23772 KB Output is correct
34 Correct 13 ms 23792 KB Output is correct
35 Correct 13 ms 23756 KB Output is correct
36 Correct 15 ms 23824 KB Output is correct
37 Correct 14 ms 23844 KB Output is correct
38 Correct 13 ms 23824 KB Output is correct
39 Correct 13 ms 23756 KB Output is correct
40 Correct 14 ms 23864 KB Output is correct
41 Correct 15 ms 23860 KB Output is correct
42 Correct 13 ms 23864 KB Output is correct
43 Correct 14 ms 23756 KB Output is correct
44 Correct 14 ms 23780 KB Output is correct
45 Correct 15 ms 23744 KB Output is correct
46 Correct 13 ms 23836 KB Output is correct
47 Correct 14 ms 23840 KB Output is correct
48 Correct 16 ms 23756 KB Output is correct
49 Correct 14 ms 23756 KB Output is correct
50 Correct 13 ms 23756 KB Output is correct
51 Correct 14 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23792 KB Output is correct
2 Correct 22 ms 24756 KB Output is correct
3 Correct 161 ms 33804 KB Output is correct
4 Correct 165 ms 33732 KB Output is correct
5 Correct 160 ms 33692 KB Output is correct
6 Correct 157 ms 33732 KB Output is correct
7 Correct 207 ms 47208 KB Output is correct
8 Correct 257 ms 42180 KB Output is correct
9 Correct 218 ms 42096 KB Output is correct
10 Correct 148 ms 34260 KB Output is correct
11 Correct 143 ms 33956 KB Output is correct
12 Correct 167 ms 33708 KB Output is correct
13 Correct 176 ms 33860 KB Output is correct
14 Correct 168 ms 39432 KB Output is correct
15 Correct 210 ms 49220 KB Output is correct
16 Correct 229 ms 51160 KB Output is correct
17 Correct 229 ms 33740 KB Output is correct
18 Correct 194 ms 33748 KB Output is correct
19 Correct 184 ms 33760 KB Output is correct
20 Correct 132 ms 34252 KB Output is correct
21 Correct 139 ms 34396 KB Output is correct
22 Correct 128 ms 34020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 15 ms 23756 KB Output is correct
4 Correct 13 ms 23756 KB Output is correct
5 Correct 13 ms 23792 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 14 ms 23856 KB Output is correct
10 Correct 13 ms 23796 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 14 ms 23860 KB Output is correct
15 Correct 13 ms 23832 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23856 KB Output is correct
18 Correct 14 ms 23824 KB Output is correct
19 Correct 13 ms 23844 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 14 ms 23848 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 13 ms 23860 KB Output is correct
25 Correct 13 ms 23760 KB Output is correct
26 Correct 13 ms 23756 KB Output is correct
27 Correct 13 ms 23756 KB Output is correct
28 Correct 14 ms 23756 KB Output is correct
29 Correct 14 ms 23744 KB Output is correct
30 Correct 14 ms 23756 KB Output is correct
31 Correct 14 ms 23832 KB Output is correct
32 Correct 13 ms 23856 KB Output is correct
33 Correct 14 ms 23772 KB Output is correct
34 Correct 13 ms 23792 KB Output is correct
35 Correct 13 ms 23756 KB Output is correct
36 Correct 15 ms 23824 KB Output is correct
37 Correct 14 ms 23844 KB Output is correct
38 Correct 13 ms 23824 KB Output is correct
39 Correct 13 ms 23756 KB Output is correct
40 Correct 14 ms 23864 KB Output is correct
41 Correct 15 ms 23860 KB Output is correct
42 Correct 13 ms 23864 KB Output is correct
43 Correct 14 ms 23756 KB Output is correct
44 Correct 14 ms 23780 KB Output is correct
45 Correct 15 ms 23744 KB Output is correct
46 Correct 13 ms 23836 KB Output is correct
47 Correct 14 ms 23840 KB Output is correct
48 Correct 16 ms 23756 KB Output is correct
49 Correct 14 ms 23756 KB Output is correct
50 Correct 13 ms 23756 KB Output is correct
51 Correct 14 ms 23756 KB Output is correct
52 Correct 17 ms 24328 KB Output is correct
53 Correct 17 ms 24268 KB Output is correct
54 Correct 17 ms 24268 KB Output is correct
55 Correct 17 ms 24284 KB Output is correct
56 Correct 18 ms 24756 KB Output is correct
57 Correct 17 ms 24272 KB Output is correct
58 Correct 17 ms 24396 KB Output is correct
59 Correct 17 ms 24468 KB Output is correct
60 Correct 16 ms 24396 KB Output is correct
61 Correct 17 ms 24296 KB Output is correct
62 Correct 17 ms 24376 KB Output is correct
63 Correct 16 ms 24380 KB Output is correct
64 Correct 16 ms 24396 KB Output is correct
65 Correct 17 ms 24264 KB Output is correct
66 Correct 16 ms 24356 KB Output is correct
67 Correct 17 ms 24344 KB Output is correct
68 Correct 18 ms 24652 KB Output is correct
69 Correct 19 ms 24268 KB Output is correct
70 Correct 16 ms 24424 KB Output is correct
71 Correct 17 ms 24376 KB Output is correct
72 Correct 17 ms 24268 KB Output is correct
73 Correct 17 ms 24316 KB Output is correct
74 Correct 17 ms 24396 KB Output is correct
75 Correct 19 ms 24520 KB Output is correct
76 Correct 17 ms 24292 KB Output is correct
77 Correct 18 ms 24652 KB Output is correct
78 Correct 16 ms 24384 KB Output is correct
79 Correct 16 ms 24628 KB Output is correct
80 Correct 16 ms 24396 KB Output is correct
81 Correct 16 ms 24416 KB Output is correct
82 Correct 18 ms 24312 KB Output is correct
83 Correct 17 ms 24400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 15 ms 23756 KB Output is correct
4 Correct 13 ms 23756 KB Output is correct
5 Correct 13 ms 23792 KB Output is correct
6 Correct 13 ms 23756 KB Output is correct
7 Correct 13 ms 23796 KB Output is correct
8 Correct 14 ms 23756 KB Output is correct
9 Correct 14 ms 23856 KB Output is correct
10 Correct 13 ms 23796 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 13 ms 23828 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 14 ms 23860 KB Output is correct
15 Correct 13 ms 23832 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23856 KB Output is correct
18 Correct 14 ms 23824 KB Output is correct
19 Correct 13 ms 23844 KB Output is correct
20 Correct 13 ms 23756 KB Output is correct
21 Correct 13 ms 23756 KB Output is correct
22 Correct 14 ms 23848 KB Output is correct
23 Correct 14 ms 23756 KB Output is correct
24 Correct 13 ms 23860 KB Output is correct
25 Correct 13 ms 23760 KB Output is correct
26 Correct 13 ms 23756 KB Output is correct
27 Correct 13 ms 23756 KB Output is correct
28 Correct 14 ms 23756 KB Output is correct
29 Correct 14 ms 23744 KB Output is correct
30 Correct 14 ms 23756 KB Output is correct
31 Correct 14 ms 23832 KB Output is correct
32 Correct 13 ms 23856 KB Output is correct
33 Correct 14 ms 23772 KB Output is correct
34 Correct 13 ms 23792 KB Output is correct
35 Correct 13 ms 23756 KB Output is correct
36 Correct 15 ms 23824 KB Output is correct
37 Correct 14 ms 23844 KB Output is correct
38 Correct 13 ms 23824 KB Output is correct
39 Correct 13 ms 23756 KB Output is correct
40 Correct 14 ms 23864 KB Output is correct
41 Correct 15 ms 23860 KB Output is correct
42 Correct 13 ms 23864 KB Output is correct
43 Correct 14 ms 23756 KB Output is correct
44 Correct 14 ms 23780 KB Output is correct
45 Correct 15 ms 23744 KB Output is correct
46 Correct 13 ms 23836 KB Output is correct
47 Correct 14 ms 23840 KB Output is correct
48 Correct 16 ms 23756 KB Output is correct
49 Correct 14 ms 23756 KB Output is correct
50 Correct 13 ms 23756 KB Output is correct
51 Correct 14 ms 23756 KB Output is correct
52 Correct 15 ms 23792 KB Output is correct
53 Correct 22 ms 24756 KB Output is correct
54 Correct 161 ms 33804 KB Output is correct
55 Correct 165 ms 33732 KB Output is correct
56 Correct 160 ms 33692 KB Output is correct
57 Correct 157 ms 33732 KB Output is correct
58 Correct 207 ms 47208 KB Output is correct
59 Correct 257 ms 42180 KB Output is correct
60 Correct 218 ms 42096 KB Output is correct
61 Correct 148 ms 34260 KB Output is correct
62 Correct 143 ms 33956 KB Output is correct
63 Correct 167 ms 33708 KB Output is correct
64 Correct 176 ms 33860 KB Output is correct
65 Correct 168 ms 39432 KB Output is correct
66 Correct 210 ms 49220 KB Output is correct
67 Correct 229 ms 51160 KB Output is correct
68 Correct 229 ms 33740 KB Output is correct
69 Correct 194 ms 33748 KB Output is correct
70 Correct 184 ms 33760 KB Output is correct
71 Correct 132 ms 34252 KB Output is correct
72 Correct 139 ms 34396 KB Output is correct
73 Correct 128 ms 34020 KB Output is correct
74 Correct 17 ms 24328 KB Output is correct
75 Correct 17 ms 24268 KB Output is correct
76 Correct 17 ms 24268 KB Output is correct
77 Correct 17 ms 24284 KB Output is correct
78 Correct 18 ms 24756 KB Output is correct
79 Correct 17 ms 24272 KB Output is correct
80 Correct 17 ms 24396 KB Output is correct
81 Correct 17 ms 24468 KB Output is correct
82 Correct 16 ms 24396 KB Output is correct
83 Correct 17 ms 24296 KB Output is correct
84 Correct 17 ms 24376 KB Output is correct
85 Correct 16 ms 24380 KB Output is correct
86 Correct 16 ms 24396 KB Output is correct
87 Correct 17 ms 24264 KB Output is correct
88 Correct 16 ms 24356 KB Output is correct
89 Correct 17 ms 24344 KB Output is correct
90 Correct 18 ms 24652 KB Output is correct
91 Correct 19 ms 24268 KB Output is correct
92 Correct 16 ms 24424 KB Output is correct
93 Correct 17 ms 24376 KB Output is correct
94 Correct 17 ms 24268 KB Output is correct
95 Correct 17 ms 24316 KB Output is correct
96 Correct 17 ms 24396 KB Output is correct
97 Correct 19 ms 24520 KB Output is correct
98 Correct 17 ms 24292 KB Output is correct
99 Correct 18 ms 24652 KB Output is correct
100 Correct 16 ms 24384 KB Output is correct
101 Correct 16 ms 24628 KB Output is correct
102 Correct 16 ms 24396 KB Output is correct
103 Correct 16 ms 24416 KB Output is correct
104 Correct 18 ms 24312 KB Output is correct
105 Correct 17 ms 24400 KB Output is correct
106 Correct 161 ms 33800 KB Output is correct
107 Correct 167 ms 35064 KB Output is correct
108 Correct 179 ms 35092 KB Output is correct
109 Correct 170 ms 35156 KB Output is correct
110 Correct 153 ms 35236 KB Output is correct
111 Correct 157 ms 36320 KB Output is correct
112 Correct 174 ms 41412 KB Output is correct
113 Correct 147 ms 35272 KB Output is correct
114 Correct 167 ms 35224 KB Output is correct
115 Correct 136 ms 35196 KB Output is correct
116 Correct 177 ms 42736 KB Output is correct
117 Correct 169 ms 41476 KB Output is correct
118 Correct 135 ms 36240 KB Output is correct
119 Correct 152 ms 35248 KB Output is correct
120 Correct 164 ms 35420 KB Output is correct
121 Correct 200 ms 35396 KB Output is correct
122 Correct 154 ms 35412 KB Output is correct
123 Correct 137 ms 35396 KB Output is correct
124 Correct 176 ms 35404 KB Output is correct
125 Correct 131 ms 35576 KB Output is correct
126 Correct 162 ms 34944 KB Output is correct
127 Correct 176 ms 35064 KB Output is correct
128 Correct 159 ms 35016 KB Output is correct
129 Correct 162 ms 35140 KB Output is correct
130 Correct 138 ms 35304 KB Output is correct
131 Correct 157 ms 36164 KB Output is correct
132 Correct 176 ms 41292 KB Output is correct
133 Correct 158 ms 35228 KB Output is correct
134 Correct 163 ms 35344 KB Output is correct
135 Correct 134 ms 35164 KB Output is correct
136 Correct 173 ms 40864 KB Output is correct
137 Correct 203 ms 41260 KB Output is correct
138 Correct 145 ms 36028 KB Output is correct
139 Correct 150 ms 35184 KB Output is correct
140 Correct 158 ms 35396 KB Output is correct
141 Correct 155 ms 35452 KB Output is correct
142 Correct 184 ms 35456 KB Output is correct
143 Correct 139 ms 35208 KB Output is correct
144 Correct 138 ms 35544 KB Output is correct
145 Correct 127 ms 35268 KB Output is correct
146 Correct 129 ms 35716 KB Output is correct
147 Correct 105 ms 35392 KB Output is correct
148 Correct 120 ms 35580 KB Output is correct
149 Correct 116 ms 36804 KB Output is correct
150 Correct 151 ms 44196 KB Output is correct
151 Correct 157 ms 35444 KB Output is correct
152 Correct 166 ms 35452 KB Output is correct
153 Correct 147 ms 35612 KB Output is correct
154 Correct 126 ms 36676 KB Output is correct
155 Correct 129 ms 35328 KB Output is correct
156 Correct 101 ms 41968 KB Output is correct
157 Correct 104 ms 41256 KB Output is correct
158 Correct 111 ms 40420 KB Output is correct
159 Correct 169 ms 34884 KB Output is correct
160 Correct 219 ms 46368 KB Output is correct
161 Correct 180 ms 35052 KB Output is correct
162 Correct 135 ms 35324 KB Output is correct