Submission #418630

# Submission time Handle Problem Language Result Execution time Memory
418630 2021-06-05T16:05:40 Z parsabahrami Subtree (INOI20_subtree) C++17
50 / 100
2000 ms 33360 KB
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
 
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 = 1e5 + 10, MOD = 1e9 + 7;
int dp[N], M[N], dp2[N], up[N], P[N], H[N], rt[N], B[N], fr[N], to[N], n, m; vector<int> vec[N], adj[N];
int pd[N];

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) 
                tmp.push_back(w);
            else up[v] = u;
    for (int &u : tmp) 
        DFS(u, v);
}

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;
        ret = (ret + S) % MOD;
    }
    return ret;
}

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) pd[i] = (pd[i] + pd[i - 1]) % MOD;
    }
    //if (SZ(A) > 1) printf("wtf %d\n", ret);
    S = 1;
    for (int i = SZ(A) - 1; i > 1; i--) {
        S = 1ll * S * A[i] % MOD;
        ret = (ret + 1ll * S * pd[i - 2] % MOD) % MOD;
    }
    //if (SZ(A) > 1) printf("wtf2 %d\n", ret);
    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;
        dp[v] = (dp[v] - S + MOD) % MOD;
    }
    if (~p) {
        A.pop_back(); 
        dp2[v] = (dp[v] - calc_dp2(A) + MOD) % MOD;
    }
}

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) ret = (ret + dp[i]) % MOD;
    printf("%d\n", ret);
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:111:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         int u, v; scanf("%d%d", &u, &v);
      |                   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5004 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 5012 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 5136 KB Output is correct
20 Correct 3 ms 5008 KB Output is correct
21 Correct 3 ms 5008 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4940 KB Output is correct
24 Correct 4 ms 4940 KB Output is correct
25 Correct 3 ms 4940 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 3 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 3 ms 4940 KB Output is correct
30 Correct 3 ms 4940 KB Output is correct
31 Correct 3 ms 4940 KB Output is correct
32 Correct 3 ms 5012 KB Output is correct
33 Correct 3 ms 4940 KB Output is correct
34 Correct 3 ms 4940 KB Output is correct
35 Correct 3 ms 4940 KB Output is correct
36 Correct 3 ms 4940 KB Output is correct
37 Correct 3 ms 4940 KB Output is correct
38 Correct 3 ms 4924 KB Output is correct
39 Correct 3 ms 4940 KB Output is correct
40 Correct 3 ms 5004 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 4940 KB Output is correct
43 Correct 3 ms 4940 KB Output is correct
44 Correct 3 ms 5004 KB Output is correct
45 Correct 3 ms 5004 KB Output is correct
46 Correct 4 ms 4940 KB Output is correct
47 Correct 3 ms 4940 KB Output is correct
48 Correct 3 ms 4940 KB Output is correct
49 Correct 3 ms 5012 KB Output is correct
50 Correct 3 ms 4940 KB Output is correct
51 Correct 4 ms 5004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 12 ms 5992 KB Output is correct
3 Correct 161 ms 15948 KB Output is correct
4 Correct 178 ms 16012 KB Output is correct
5 Correct 159 ms 16008 KB Output is correct
6 Correct 156 ms 15940 KB Output is correct
7 Correct 211 ms 29604 KB Output is correct
8 Correct 189 ms 24404 KB Output is correct
9 Correct 182 ms 24448 KB Output is correct
10 Correct 126 ms 16612 KB Output is correct
11 Correct 148 ms 16300 KB Output is correct
12 Correct 160 ms 16068 KB Output is correct
13 Correct 161 ms 15900 KB Output is correct
14 Correct 176 ms 22272 KB Output is correct
15 Correct 210 ms 31736 KB Output is correct
16 Correct 206 ms 33360 KB Output is correct
17 Correct 172 ms 15940 KB Output is correct
18 Correct 185 ms 16120 KB Output is correct
19 Correct 169 ms 16076 KB Output is correct
20 Correct 125 ms 16616 KB Output is correct
21 Correct 121 ms 16576 KB Output is correct
22 Correct 124 ms 16548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5004 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 5012 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 5136 KB Output is correct
20 Correct 3 ms 5008 KB Output is correct
21 Correct 3 ms 5008 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4940 KB Output is correct
24 Correct 4 ms 4940 KB Output is correct
25 Correct 3 ms 4940 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 3 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 3 ms 4940 KB Output is correct
30 Correct 3 ms 4940 KB Output is correct
31 Correct 3 ms 4940 KB Output is correct
32 Correct 3 ms 5012 KB Output is correct
33 Correct 3 ms 4940 KB Output is correct
34 Correct 3 ms 4940 KB Output is correct
35 Correct 3 ms 4940 KB Output is correct
36 Correct 3 ms 4940 KB Output is correct
37 Correct 3 ms 4940 KB Output is correct
38 Correct 3 ms 4924 KB Output is correct
39 Correct 3 ms 4940 KB Output is correct
40 Correct 3 ms 5004 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 4940 KB Output is correct
43 Correct 3 ms 4940 KB Output is correct
44 Correct 3 ms 5004 KB Output is correct
45 Correct 3 ms 5004 KB Output is correct
46 Correct 4 ms 4940 KB Output is correct
47 Correct 3 ms 4940 KB Output is correct
48 Correct 3 ms 4940 KB Output is correct
49 Correct 3 ms 5012 KB Output is correct
50 Correct 3 ms 4940 KB Output is correct
51 Correct 4 ms 5004 KB Output is correct
52 Correct 7 ms 5536 KB Output is correct
53 Correct 7 ms 5572 KB Output is correct
54 Correct 7 ms 5472 KB Output is correct
55 Correct 6 ms 5580 KB Output is correct
56 Correct 8 ms 5964 KB Output is correct
57 Correct 7 ms 5580 KB Output is correct
58 Correct 6 ms 5660 KB Output is correct
59 Correct 7 ms 5708 KB Output is correct
60 Correct 6 ms 5580 KB Output is correct
61 Correct 8 ms 5580 KB Output is correct
62 Correct 7 ms 5536 KB Output is correct
63 Correct 6 ms 5580 KB Output is correct
64 Correct 6 ms 5580 KB Output is correct
65 Correct 7 ms 5580 KB Output is correct
66 Correct 8 ms 5720 KB Output is correct
67 Correct 7 ms 5580 KB Output is correct
68 Correct 8 ms 5920 KB Output is correct
69 Correct 7 ms 5592 KB Output is correct
70 Correct 6 ms 5580 KB Output is correct
71 Correct 7 ms 5580 KB Output is correct
72 Correct 7 ms 5580 KB Output is correct
73 Correct 6 ms 5580 KB Output is correct
74 Correct 6 ms 5528 KB Output is correct
75 Correct 6 ms 5836 KB Output is correct
76 Correct 7 ms 5532 KB Output is correct
77 Correct 8 ms 5968 KB Output is correct
78 Correct 6 ms 5580 KB Output is correct
79 Correct 7 ms 5836 KB Output is correct
80 Correct 7 ms 5528 KB Output is correct
81 Correct 6 ms 5580 KB Output is correct
82 Correct 7 ms 5580 KB Output is correct
83 Correct 7 ms 5536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5004 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 5012 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4956 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 5008 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 3 ms 5008 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 5136 KB Output is correct
20 Correct 3 ms 5008 KB Output is correct
21 Correct 3 ms 5008 KB Output is correct
22 Correct 3 ms 4940 KB Output is correct
23 Correct 3 ms 4940 KB Output is correct
24 Correct 4 ms 4940 KB Output is correct
25 Correct 3 ms 4940 KB Output is correct
26 Correct 3 ms 4940 KB Output is correct
27 Correct 3 ms 4940 KB Output is correct
28 Correct 3 ms 4940 KB Output is correct
29 Correct 3 ms 4940 KB Output is correct
30 Correct 3 ms 4940 KB Output is correct
31 Correct 3 ms 4940 KB Output is correct
32 Correct 3 ms 5012 KB Output is correct
33 Correct 3 ms 4940 KB Output is correct
34 Correct 3 ms 4940 KB Output is correct
35 Correct 3 ms 4940 KB Output is correct
36 Correct 3 ms 4940 KB Output is correct
37 Correct 3 ms 4940 KB Output is correct
38 Correct 3 ms 4924 KB Output is correct
39 Correct 3 ms 4940 KB Output is correct
40 Correct 3 ms 5004 KB Output is correct
41 Correct 3 ms 5068 KB Output is correct
42 Correct 3 ms 4940 KB Output is correct
43 Correct 3 ms 4940 KB Output is correct
44 Correct 3 ms 5004 KB Output is correct
45 Correct 3 ms 5004 KB Output is correct
46 Correct 4 ms 4940 KB Output is correct
47 Correct 3 ms 4940 KB Output is correct
48 Correct 3 ms 4940 KB Output is correct
49 Correct 3 ms 5012 KB Output is correct
50 Correct 3 ms 4940 KB Output is correct
51 Correct 4 ms 5004 KB Output is correct
52 Correct 3 ms 4940 KB Output is correct
53 Correct 12 ms 5992 KB Output is correct
54 Correct 161 ms 15948 KB Output is correct
55 Correct 178 ms 16012 KB Output is correct
56 Correct 159 ms 16008 KB Output is correct
57 Correct 156 ms 15940 KB Output is correct
58 Correct 211 ms 29604 KB Output is correct
59 Correct 189 ms 24404 KB Output is correct
60 Correct 182 ms 24448 KB Output is correct
61 Correct 126 ms 16612 KB Output is correct
62 Correct 148 ms 16300 KB Output is correct
63 Correct 160 ms 16068 KB Output is correct
64 Correct 161 ms 15900 KB Output is correct
65 Correct 176 ms 22272 KB Output is correct
66 Correct 210 ms 31736 KB Output is correct
67 Correct 206 ms 33360 KB Output is correct
68 Correct 172 ms 15940 KB Output is correct
69 Correct 185 ms 16120 KB Output is correct
70 Correct 169 ms 16076 KB Output is correct
71 Correct 125 ms 16616 KB Output is correct
72 Correct 121 ms 16576 KB Output is correct
73 Correct 124 ms 16548 KB Output is correct
74 Correct 7 ms 5536 KB Output is correct
75 Correct 7 ms 5572 KB Output is correct
76 Correct 7 ms 5472 KB Output is correct
77 Correct 6 ms 5580 KB Output is correct
78 Correct 8 ms 5964 KB Output is correct
79 Correct 7 ms 5580 KB Output is correct
80 Correct 6 ms 5660 KB Output is correct
81 Correct 7 ms 5708 KB Output is correct
82 Correct 6 ms 5580 KB Output is correct
83 Correct 8 ms 5580 KB Output is correct
84 Correct 7 ms 5536 KB Output is correct
85 Correct 6 ms 5580 KB Output is correct
86 Correct 6 ms 5580 KB Output is correct
87 Correct 7 ms 5580 KB Output is correct
88 Correct 8 ms 5720 KB Output is correct
89 Correct 7 ms 5580 KB Output is correct
90 Correct 8 ms 5920 KB Output is correct
91 Correct 7 ms 5592 KB Output is correct
92 Correct 6 ms 5580 KB Output is correct
93 Correct 7 ms 5580 KB Output is correct
94 Correct 7 ms 5580 KB Output is correct
95 Correct 6 ms 5580 KB Output is correct
96 Correct 6 ms 5528 KB Output is correct
97 Correct 6 ms 5836 KB Output is correct
98 Correct 7 ms 5532 KB Output is correct
99 Correct 8 ms 5968 KB Output is correct
100 Correct 6 ms 5580 KB Output is correct
101 Correct 7 ms 5836 KB Output is correct
102 Correct 7 ms 5528 KB Output is correct
103 Correct 6 ms 5580 KB Output is correct
104 Correct 7 ms 5580 KB Output is correct
105 Correct 7 ms 5536 KB Output is correct
106 Execution timed out 2087 ms 15028 KB Time limit exceeded
107 Halted 0 ms 0 KB -