Submission #796935

# Submission time Handle Problem Language Result Execution time Memory
796935 2023-07-28T23:36:24 Z ashcomp Subtree (INOI20_subtree) C++14
100 / 100
86 ms 33264 KB
#include<bits/stdc++.h>

using namespace std;
 
#define all(x) x.begin() , x.end()
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define pb push_back
#define qb pop_back
#define F first
#define S second
#define wall cerr<<"--------------------------------------"<<endl
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#pragma GCC optimize("Ofast")
 
typedef long long ll;
typedef double db;

const ll INF = 1e9;
const ll maxn = 1e5 + 10;
const ll mod = 1e9 + 7;

ll n , m , h[maxn] , dp[maxn] , p[maxn] , par[maxn] , other[maxn];
bool tp[maxn] , mark[maxn];
vector<ll> g[maxn] , ar[maxn] , sum[maxn] , pre[maxn] , suf[maxn];

void dfs0(ll v)
{
        //cout<<v<<" ";
        mark[v] = 1;
        for(auto u : g[v]){
                if(u==p[v])continue;
                if(mark[u]==1 && h[v]>h[u]){
                        par[v] = u;
                        tp[u] = true; 
                        continue;
                }
                if(mark[u]==1)continue;
                p[u] = v;
                h[u] = h[v] + 1;
                dfs0(u);
                if(par[v]==-1)par[v] = par[u];
                if(par[v]==v)par[v] = -1;
        }
        //cout<<v<<" ";
}

void dfs1(ll v)
{
        mark[v] = 1;
        dp[v] = 1;
        other[v] = 1;
        for(auto u : g[v]){
                if(u==p[v] || mark[u]==1){
                        continue;
                }
                dfs1(u);
                if(par[u]==-1 || (par[u]!=par[v] && par[u]!=v)){
                        //cout<<v<<" , "<<u<<"\n";
                        other[v] = (other[v] *(dp[u]+1))%mod;
                }
                dp[v] = (dp[v] * (dp[u]+1))%mod;
        }
        if(par[v]!=-1){
                ar[par[v]].push_back(other[v]);
        }
        if(tp[v]==1){
                dp[v] = 0;
                ar[v].push_back(other[v]);
                /**/
                ll sz = ar[v].size();
                for(int i=1; i<=sz; i++){
                        suf[v].push_back(0);
                        pre[v].push_back(0);
                        sum[v].push_back(1);
                }
                suf[v][sz-1]=ar[v][sz-1];
                for(int i=sz-2; i>=0; i--){
                        suf[v][i] = (suf[v][i+1]*ar[v][i])%mod;
                }
                pre[v][0] = ar[v][0];
                for(int i=1; i<sz; i++){
                        pre[v][i] = (pre[v][i-1]*ar[v][i])%mod;
                }
                sum[v][0]=1;
                for(int i=1; i<sz; i++){
                        sum[v][i] = (sum[v][i-1]+pre[v][i-1])%mod;
                }
                for(int i=0; i<sz-1; i++){
                        dp[v] = (dp[v] + (sum[v][i]*suf[v][i+1])%mod)%mod;
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
        cin>>n>>m;
        for(int i=1; i<=m; i++){
                int v,u; cin>>v>>u;
                g[v].push_back(u);
                g[u].push_back(v);
        }
        for(int i=1; i<=n; i++){
                p[i] = -1;
                par[i] = -1;
        }
        dfs0(1);//cout<<"\n";
        for(int i=1; i<=n; i++){
                mark[i] = 0;
        }
        dfs1(1);//cout<<"\n";
        // for(int i=1; i<=n; i++){
        //         cout<<i<<" : "<<dp[i]<<"\n";
        // }
        ll ans = 0;
        for(int i=1; i<=n; i++){
                ans = (ans + dp[i])%mod;
        }
        cout<<ans;
        return 0;
}
/*
10 10
1 2
2 3
3 4
4 5
5 6
4 7
3 8
2 9
1 10
1 5
*/
/*
5 4
1 2
1 3
2 4
2 5

ans = 17;
*/
/*
8 9
1 2
2 3
3 1
3 4
4 5
5 6
6 7
7 4
7 8

ans = 52;
*/
/*
by
          _____                    _____                    _____                    _____                  
         /\    \                  /\    \                  /\    \                  /\    \                 
        /::\    \                /::\    \                /::\____\                /::\    \                
       /::::\    \              /::::\    \              /:::/    /                \:::\    \               
      /::::::\    \            /::::::\    \            /:::/    /                  \:::\    \              
     /:::/\:::\    \          /:::/\:::\    \          /:::/    /                    \:::\    \             
    /:::/__\:::\    \        /:::/__\:::\    \        /:::/____/                      \:::\    \            
   /::::\   \:::\    \       \:::\   \:::\    \      /::::\    \                      /::::\    \           
  /::::::\   \:::\    \    ___\:::\   \:::\    \    /::::::\    \   _____    ____    /::::::\    \          
 /:::/\:::\   \:::\    \  /\   \:::\   \:::\    \  /:::/\:::\    \ /\    \  /\   \  /:::/\:::\    \         
/:::/  \:::\   \:::\____\/::\   \:::\   \:::\____\/:::/  \:::\    /::\____\/::\   \/:::/  \:::\____\        
\::/    \:::\  /:::/    /\:::\   \:::\   \::/    /\::/    \:::\  /:::/    /\:::\  /:::/    \::/    /        
 \/____/ \:::\/:::/    /  \:::\   \:::\   \/____/  \/____/ \:::\/:::/    /  \:::\/:::/    / \/____/         
          \::::::/    /    \:::\   \:::\    \               \::::::/    /    \::::::/    /                  
           \::::/    /      \:::\   \:::\____\               \::::/    /      \::::/____/                   
           /:::/    /        \:::\  /:::/    /               /:::/    /        \:::\    \                   
          /:::/    /          \:::\/:::/    /               /:::/    /          \:::\    \                  
         /:::/    /            \::::::/    /               /:::/    /            \:::\    \                 
        /:::/    /              \::::/    /               /:::/    /              \:::\____\                
        \::/    /                \::/    /                \::/    /                \::/    /                
         \/____/                  \/____/                  \/____/                  \/____/                 
                                                                                                            
*/
//NOGHRE SHODANAM BAD NIST :_)
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 6 ms 12092 KB Output is correct
6 Correct 5 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 6 ms 12016 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 6 ms 12080 KB Output is correct
17 Correct 6 ms 12080 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 12080 KB Output is correct
22 Correct 6 ms 11988 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 6 ms 12008 KB Output is correct
26 Correct 6 ms 11988 KB Output is correct
27 Correct 6 ms 11988 KB Output is correct
28 Correct 6 ms 12080 KB Output is correct
29 Correct 6 ms 11988 KB Output is correct
30 Correct 6 ms 11988 KB Output is correct
31 Correct 6 ms 12080 KB Output is correct
32 Correct 6 ms 11988 KB Output is correct
33 Correct 6 ms 12116 KB Output is correct
34 Correct 6 ms 12076 KB Output is correct
35 Correct 6 ms 11988 KB Output is correct
36 Correct 6 ms 12032 KB Output is correct
37 Correct 6 ms 12084 KB Output is correct
38 Correct 6 ms 11988 KB Output is correct
39 Correct 6 ms 11988 KB Output is correct
40 Correct 6 ms 11988 KB Output is correct
41 Correct 6 ms 12080 KB Output is correct
42 Correct 6 ms 12076 KB Output is correct
43 Correct 6 ms 11988 KB Output is correct
44 Correct 6 ms 12088 KB Output is correct
45 Correct 6 ms 11988 KB Output is correct
46 Correct 6 ms 11988 KB Output is correct
47 Correct 6 ms 12092 KB Output is correct
48 Correct 6 ms 11988 KB Output is correct
49 Correct 6 ms 12024 KB Output is correct
50 Correct 6 ms 11988 KB Output is correct
51 Correct 6 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 9 ms 12876 KB Output is correct
3 Correct 41 ms 19788 KB Output is correct
4 Correct 42 ms 19712 KB Output is correct
5 Correct 41 ms 19752 KB Output is correct
6 Correct 42 ms 19732 KB Output is correct
7 Correct 65 ms 28596 KB Output is correct
8 Correct 50 ms 25332 KB Output is correct
9 Correct 50 ms 25420 KB Output is correct
10 Correct 35 ms 20440 KB Output is correct
11 Correct 35 ms 19712 KB Output is correct
12 Correct 51 ms 19180 KB Output is correct
13 Correct 43 ms 19224 KB Output is correct
14 Correct 41 ms 23452 KB Output is correct
15 Correct 59 ms 29628 KB Output is correct
16 Correct 54 ms 30816 KB Output is correct
17 Correct 46 ms 19152 KB Output is correct
18 Correct 43 ms 19280 KB Output is correct
19 Correct 43 ms 19240 KB Output is correct
20 Correct 34 ms 20004 KB Output is correct
21 Correct 33 ms 20096 KB Output is correct
22 Correct 33 ms 20000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 6 ms 12092 KB Output is correct
6 Correct 5 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 6 ms 12016 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 6 ms 12080 KB Output is correct
17 Correct 6 ms 12080 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 12080 KB Output is correct
22 Correct 6 ms 11988 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 6 ms 12008 KB Output is correct
26 Correct 6 ms 11988 KB Output is correct
27 Correct 6 ms 11988 KB Output is correct
28 Correct 6 ms 12080 KB Output is correct
29 Correct 6 ms 11988 KB Output is correct
30 Correct 6 ms 11988 KB Output is correct
31 Correct 6 ms 12080 KB Output is correct
32 Correct 6 ms 11988 KB Output is correct
33 Correct 6 ms 12116 KB Output is correct
34 Correct 6 ms 12076 KB Output is correct
35 Correct 6 ms 11988 KB Output is correct
36 Correct 6 ms 12032 KB Output is correct
37 Correct 6 ms 12084 KB Output is correct
38 Correct 6 ms 11988 KB Output is correct
39 Correct 6 ms 11988 KB Output is correct
40 Correct 6 ms 11988 KB Output is correct
41 Correct 6 ms 12080 KB Output is correct
42 Correct 6 ms 12076 KB Output is correct
43 Correct 6 ms 11988 KB Output is correct
44 Correct 6 ms 12088 KB Output is correct
45 Correct 6 ms 11988 KB Output is correct
46 Correct 6 ms 11988 KB Output is correct
47 Correct 6 ms 12092 KB Output is correct
48 Correct 6 ms 11988 KB Output is correct
49 Correct 6 ms 12024 KB Output is correct
50 Correct 6 ms 11988 KB Output is correct
51 Correct 6 ms 11988 KB Output is correct
52 Correct 11 ms 12620 KB Output is correct
53 Correct 8 ms 12500 KB Output is correct
54 Correct 8 ms 12628 KB Output is correct
55 Correct 8 ms 12532 KB Output is correct
56 Correct 9 ms 13268 KB Output is correct
57 Correct 7 ms 12436 KB Output is correct
58 Correct 7 ms 12500 KB Output is correct
59 Correct 7 ms 12784 KB Output is correct
60 Correct 7 ms 12500 KB Output is correct
61 Correct 9 ms 12604 KB Output is correct
62 Correct 8 ms 12756 KB Output is correct
63 Correct 7 ms 12504 KB Output is correct
64 Correct 8 ms 12468 KB Output is correct
65 Correct 7 ms 12500 KB Output is correct
66 Correct 8 ms 12628 KB Output is correct
67 Correct 8 ms 12472 KB Output is correct
68 Correct 8 ms 13140 KB Output is correct
69 Correct 8 ms 12500 KB Output is correct
70 Correct 7 ms 12500 KB Output is correct
71 Correct 8 ms 12628 KB Output is correct
72 Correct 8 ms 12756 KB Output is correct
73 Correct 7 ms 12548 KB Output is correct
74 Correct 8 ms 12720 KB Output is correct
75 Correct 10 ms 12992 KB Output is correct
76 Correct 9 ms 12732 KB Output is correct
77 Correct 8 ms 13120 KB Output is correct
78 Correct 8 ms 12884 KB Output is correct
79 Correct 8 ms 13116 KB Output is correct
80 Correct 10 ms 12916 KB Output is correct
81 Correct 7 ms 12608 KB Output is correct
82 Correct 8 ms 12588 KB Output is correct
83 Correct 7 ms 12628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 5 ms 11988 KB Output is correct
5 Correct 6 ms 12092 KB Output is correct
6 Correct 5 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 6 ms 11988 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 6 ms 12016 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 7 ms 11988 KB Output is correct
16 Correct 6 ms 12080 KB Output is correct
17 Correct 6 ms 12080 KB Output is correct
18 Correct 6 ms 11988 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 12080 KB Output is correct
22 Correct 6 ms 11988 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 7 ms 11988 KB Output is correct
25 Correct 6 ms 12008 KB Output is correct
26 Correct 6 ms 11988 KB Output is correct
27 Correct 6 ms 11988 KB Output is correct
28 Correct 6 ms 12080 KB Output is correct
29 Correct 6 ms 11988 KB Output is correct
30 Correct 6 ms 11988 KB Output is correct
31 Correct 6 ms 12080 KB Output is correct
32 Correct 6 ms 11988 KB Output is correct
33 Correct 6 ms 12116 KB Output is correct
34 Correct 6 ms 12076 KB Output is correct
35 Correct 6 ms 11988 KB Output is correct
36 Correct 6 ms 12032 KB Output is correct
37 Correct 6 ms 12084 KB Output is correct
38 Correct 6 ms 11988 KB Output is correct
39 Correct 6 ms 11988 KB Output is correct
40 Correct 6 ms 11988 KB Output is correct
41 Correct 6 ms 12080 KB Output is correct
42 Correct 6 ms 12076 KB Output is correct
43 Correct 6 ms 11988 KB Output is correct
44 Correct 6 ms 12088 KB Output is correct
45 Correct 6 ms 11988 KB Output is correct
46 Correct 6 ms 11988 KB Output is correct
47 Correct 6 ms 12092 KB Output is correct
48 Correct 6 ms 11988 KB Output is correct
49 Correct 6 ms 12024 KB Output is correct
50 Correct 6 ms 11988 KB Output is correct
51 Correct 6 ms 11988 KB Output is correct
52 Correct 6 ms 11988 KB Output is correct
53 Correct 9 ms 12876 KB Output is correct
54 Correct 41 ms 19788 KB Output is correct
55 Correct 42 ms 19712 KB Output is correct
56 Correct 41 ms 19752 KB Output is correct
57 Correct 42 ms 19732 KB Output is correct
58 Correct 65 ms 28596 KB Output is correct
59 Correct 50 ms 25332 KB Output is correct
60 Correct 50 ms 25420 KB Output is correct
61 Correct 35 ms 20440 KB Output is correct
62 Correct 35 ms 19712 KB Output is correct
63 Correct 51 ms 19180 KB Output is correct
64 Correct 43 ms 19224 KB Output is correct
65 Correct 41 ms 23452 KB Output is correct
66 Correct 59 ms 29628 KB Output is correct
67 Correct 54 ms 30816 KB Output is correct
68 Correct 46 ms 19152 KB Output is correct
69 Correct 43 ms 19280 KB Output is correct
70 Correct 43 ms 19240 KB Output is correct
71 Correct 34 ms 20004 KB Output is correct
72 Correct 33 ms 20096 KB Output is correct
73 Correct 33 ms 20000 KB Output is correct
74 Correct 11 ms 12620 KB Output is correct
75 Correct 8 ms 12500 KB Output is correct
76 Correct 8 ms 12628 KB Output is correct
77 Correct 8 ms 12532 KB Output is correct
78 Correct 9 ms 13268 KB Output is correct
79 Correct 7 ms 12436 KB Output is correct
80 Correct 7 ms 12500 KB Output is correct
81 Correct 7 ms 12784 KB Output is correct
82 Correct 7 ms 12500 KB Output is correct
83 Correct 9 ms 12604 KB Output is correct
84 Correct 8 ms 12756 KB Output is correct
85 Correct 7 ms 12504 KB Output is correct
86 Correct 8 ms 12468 KB Output is correct
87 Correct 7 ms 12500 KB Output is correct
88 Correct 8 ms 12628 KB Output is correct
89 Correct 8 ms 12472 KB Output is correct
90 Correct 8 ms 13140 KB Output is correct
91 Correct 8 ms 12500 KB Output is correct
92 Correct 7 ms 12500 KB Output is correct
93 Correct 8 ms 12628 KB Output is correct
94 Correct 8 ms 12756 KB Output is correct
95 Correct 7 ms 12548 KB Output is correct
96 Correct 8 ms 12720 KB Output is correct
97 Correct 10 ms 12992 KB Output is correct
98 Correct 9 ms 12732 KB Output is correct
99 Correct 8 ms 13120 KB Output is correct
100 Correct 8 ms 12884 KB Output is correct
101 Correct 8 ms 13116 KB Output is correct
102 Correct 10 ms 12916 KB Output is correct
103 Correct 7 ms 12608 KB Output is correct
104 Correct 8 ms 12588 KB Output is correct
105 Correct 7 ms 12628 KB Output is correct
106 Correct 45 ms 21804 KB Output is correct
107 Correct 55 ms 22572 KB Output is correct
108 Correct 56 ms 23088 KB Output is correct
109 Correct 57 ms 23328 KB Output is correct
110 Correct 35 ms 21364 KB Output is correct
111 Correct 46 ms 23344 KB Output is correct
112 Correct 82 ms 32348 KB Output is correct
113 Correct 54 ms 23528 KB Output is correct
114 Correct 61 ms 23220 KB Output is correct
115 Correct 33 ms 21024 KB Output is correct
116 Correct 61 ms 31708 KB Output is correct
117 Correct 70 ms 31160 KB Output is correct
118 Correct 40 ms 22536 KB Output is correct
119 Correct 57 ms 23416 KB Output is correct
120 Correct 64 ms 24676 KB Output is correct
121 Correct 71 ms 25640 KB Output is correct
122 Correct 80 ms 26276 KB Output is correct
123 Correct 37 ms 21400 KB Output is correct
124 Correct 34 ms 21196 KB Output is correct
125 Correct 34 ms 21096 KB Output is correct
126 Correct 48 ms 21800 KB Output is correct
127 Correct 52 ms 22560 KB Output is correct
128 Correct 54 ms 23092 KB Output is correct
129 Correct 56 ms 23452 KB Output is correct
130 Correct 35 ms 21428 KB Output is correct
131 Correct 45 ms 23352 KB Output is correct
132 Correct 82 ms 32144 KB Output is correct
133 Correct 55 ms 23580 KB Output is correct
134 Correct 58 ms 23248 KB Output is correct
135 Correct 31 ms 21076 KB Output is correct
136 Correct 62 ms 29828 KB Output is correct
137 Correct 73 ms 30796 KB Output is correct
138 Correct 36 ms 22504 KB Output is correct
139 Correct 61 ms 23400 KB Output is correct
140 Correct 66 ms 24712 KB Output is correct
141 Correct 71 ms 25568 KB Output is correct
142 Correct 78 ms 26276 KB Output is correct
143 Correct 38 ms 21388 KB Output is correct
144 Correct 34 ms 21168 KB Output is correct
145 Correct 31 ms 21052 KB Output is correct
146 Correct 86 ms 26572 KB Output is correct
147 Correct 54 ms 25160 KB Output is correct
148 Correct 50 ms 25140 KB Output is correct
149 Correct 49 ms 25756 KB Output is correct
150 Correct 54 ms 33264 KB Output is correct
151 Correct 68 ms 26320 KB Output is correct
152 Correct 85 ms 27992 KB Output is correct
153 Correct 84 ms 28016 KB Output is correct
154 Correct 53 ms 24496 KB Output is correct
155 Correct 36 ms 21708 KB Output is correct
156 Correct 55 ms 31576 KB Output is correct
157 Correct 54 ms 30572 KB Output is correct
158 Correct 59 ms 29688 KB Output is correct
159 Correct 62 ms 20900 KB Output is correct
160 Correct 53 ms 29348 KB Output is correct
161 Correct 44 ms 21068 KB Output is correct
162 Correct 42 ms 21764 KB Output is correct