답안 #634633

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
634633 2022-08-24T16:13:15 Z MohammadAghil Subtree (INOI20_subtree) C++17
100 / 100
168 ms 36328 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target ("avx2")
using namespace std;

typedef long long ll;
typedef pair<int, int> pp;

#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define pb push_back
#define ff first
#define ss second

void dbg(){
    cerr << endl;
}
template<typename Head, typename... Tail> void dbg(Head h, Tail... t){
    cerr << " " << h << ",";
    dbg(t...);
}
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">:", dbg(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void IOS(){
    cin.tie(0) -> sync_with_stdio(0);
//    #ifndef ONLINE_JUDGE
//        freopen("inp.txt", "r", stdin);
//        freopen("out.txt", "w", stdout);
//    #endif
}

const ll mod = 1e9 + 7, maxn = 1e5 + 5, lg = 19, inf = ll(1e9) + 5;

vector<int> adj[maxn], lis[maxn];
vector<pp>  adj2[maxn];
bool vis[maxn], king[maxn];
int cnt_cycle, grp[maxn];

pp dfs(int r, int p){
    pp res = {-1, -1};
    vis[r] = true;
    for(int c: adj[r]) if(c - p){
        if(vis[c]){
            if(res.ff + 1) continue;
            res = {c, cnt_cycle++};
            grp[r] = res.ss;
            lis[grp[r]].pb(r);
        }else{
            pp ret = dfs(c, r);
            if(ret.ff + 1){
                assert(res.ff == -1);
                grp[r] = ret.ss;
                res = ret;
                lis[grp[r]].pb(r);
            }
        }
    }
    if(res.ff == r) res = {-1, -1};
    return res;
}

ll slv(vector<ll> a){
    if(sz(a) < 3) return a[0];
    int n = sz(a);
    vector<ll> suf(n + 1);
    suf[n] = 1;
    per(i,n-1,0){
        suf[i] = suf[i+1]*a[i]%mod;
    }
    ll cr = 0, mul = a[0];
    rep(i,2,n+1) cr += suf[i], cr %= mod;
    ll res = 0;
    rep(i,0,n-1){
        res = (res + cr*mul)%mod;
        cr = (cr - suf[i + 2] + mod)%mod;
        mul = mul*a[i + 1]%mod;
    }
    return res;
}

ll slv2(vector<ll> a){
    ll res = 0;
    ll cr = 0;
    rep(i,0,sz(a)){
        cr = (cr + 1)*a[i]%mod;
        res = (res + cr)%mod;
    }
    return res;
}

ll dp[maxn], dp2[maxn], ans;
void dfs2(int r, int p){
    for(auto[c, u]: adj2[r]){
        if(c - p){
            king[u] = true;
            dfs2(c, r);
        }
    }
    int pos = 0, ptr = 0;
    for(int u: lis[r]){
        dp2[u] = 1;
        for(int c: adj[u]) if(grp[u] - grp[c] && grp[c] - p){
            dp2[u] = dp2[u]*(dp[c] + 1)%mod;
        }
        if(king[u]) pos = ptr;
        ptr++;
    }
    vector<ll> tmp, tmp2;
    rep(i,pos,sz(lis[r])) tmp.pb(dp2[lis[r][i]]);
    rep(i,0,pos) tmp.pb(dp2[lis[r][i]]);
    rep(i,pos+1,sz(lis[r])) dp[lis[r][i]] = dp2[lis[r][i]], tmp2.pb(dp[lis[r][i]]);
    rep(i,0,pos) dp[lis[r][i]] = dp2[lis[r][i]], tmp2.pb(dp[lis[r][i]]);
    dp[lis[r][pos]] = slv(tmp);
    ans = (ans + dp[lis[r][pos]] + slv2(tmp2))%mod;
}

void chk(int r, int p){
    vis[r] = false;
    for(auto[c, y]: adj2[r]) if(c - p){
        assert(vis[c]);
        chk(c, r);
    }
}

int main(){ IOS();
    int n, m; cin >> n >> m;
    rep(i,0,m){
        int u, v; cin >> u >> v; u--, v--;
        adj[u].pb(v), adj[v].pb(u);
    }
    fill(grp, grp + n, -1);
    dfs(0, -1);
    rep(i,0,n){
        if(grp[i] == -1) {
            grp[i] = cnt_cycle++;
            lis[grp[i]].pb(i);
        }
    }
    rep(i,0,n){
        for(int c: adj[i]){
            if(grp[i] - grp[c]){
                adj2[grp[i]].pb({grp[c], c});
            }
        }
    }
    king[0] = true;
    dfs2(grp[0], -1);
    chk(0, -1);
//    rep(i,0,n){
//        er(i+1, dp[i], dp2[i], king[i], grp[i]);
//    }
//    er(slv2({1, 2, 3, 4}));
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7344 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7396 KB Output is correct
6 Correct 5 ms 7352 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7328 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7396 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 4 ms 7376 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7336 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 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 4 ms 7380 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 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 7384 KB Output is correct
40 Correct 4 ms 7380 KB Output is correct
41 Correct 4 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 4 ms 7380 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 4 ms 7380 KB Output is correct
46 Correct 4 ms 7360 KB Output is correct
47 Correct 4 ms 7376 KB Output is correct
48 Correct 4 ms 7376 KB Output is correct
49 Correct 4 ms 7388 KB Output is correct
50 Correct 4 ms 7384 KB Output is correct
51 Correct 5 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 10 ms 8580 KB Output is correct
3 Correct 119 ms 19624 KB Output is correct
4 Correct 118 ms 19624 KB Output is correct
5 Correct 112 ms 19600 KB Output is correct
6 Correct 114 ms 19592 KB Output is correct
7 Correct 140 ms 32604 KB Output is correct
8 Correct 136 ms 27852 KB Output is correct
9 Correct 132 ms 28088 KB Output is correct
10 Correct 98 ms 20616 KB Output is correct
11 Correct 102 ms 19732 KB Output is correct
12 Correct 125 ms 18964 KB Output is correct
13 Correct 144 ms 18988 KB Output is correct
14 Correct 95 ms 25280 KB Output is correct
15 Correct 133 ms 34636 KB Output is correct
16 Correct 168 ms 36328 KB Output is correct
17 Correct 133 ms 19020 KB Output is correct
18 Correct 135 ms 19068 KB Output is correct
19 Correct 124 ms 18944 KB Output is correct
20 Correct 89 ms 19988 KB Output is correct
21 Correct 107 ms 20136 KB Output is correct
22 Correct 105 ms 20128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7344 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7396 KB Output is correct
6 Correct 5 ms 7352 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7328 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7396 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 4 ms 7376 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7336 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 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 4 ms 7380 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 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 7384 KB Output is correct
40 Correct 4 ms 7380 KB Output is correct
41 Correct 4 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 4 ms 7380 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 4 ms 7380 KB Output is correct
46 Correct 4 ms 7360 KB Output is correct
47 Correct 4 ms 7376 KB Output is correct
48 Correct 4 ms 7376 KB Output is correct
49 Correct 4 ms 7388 KB Output is correct
50 Correct 4 ms 7384 KB Output is correct
51 Correct 5 ms 7380 KB Output is correct
52 Correct 6 ms 8020 KB Output is correct
53 Correct 6 ms 7892 KB Output is correct
54 Correct 7 ms 7896 KB Output is correct
55 Correct 6 ms 8020 KB Output is correct
56 Correct 8 ms 8184 KB Output is correct
57 Correct 7 ms 7976 KB Output is correct
58 Correct 6 ms 8020 KB Output is correct
59 Correct 7 ms 8176 KB Output is correct
60 Correct 7 ms 8092 KB Output is correct
61 Correct 9 ms 7892 KB Output is correct
62 Correct 11 ms 7764 KB Output is correct
63 Correct 8 ms 8032 KB Output is correct
64 Correct 7 ms 8032 KB Output is correct
65 Correct 7 ms 8020 KB Output is correct
66 Correct 7 ms 7876 KB Output is correct
67 Correct 7 ms 8032 KB Output is correct
68 Correct 8 ms 8096 KB Output is correct
69 Correct 7 ms 7900 KB Output is correct
70 Correct 6 ms 8020 KB Output is correct
71 Correct 6 ms 7764 KB Output is correct
72 Correct 8 ms 7908 KB Output is correct
73 Correct 6 ms 8036 KB Output is correct
74 Correct 8 ms 7764 KB Output is correct
75 Correct 7 ms 7892 KB Output is correct
76 Correct 8 ms 7892 KB Output is correct
77 Correct 8 ms 8288 KB Output is correct
78 Correct 6 ms 7764 KB Output is correct
79 Correct 7 ms 7988 KB Output is correct
80 Correct 7 ms 7764 KB Output is correct
81 Correct 7 ms 7908 KB Output is correct
82 Correct 6 ms 7892 KB Output is correct
83 Correct 7 ms 7904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7344 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7396 KB Output is correct
6 Correct 5 ms 7352 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7328 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7396 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 4 ms 7376 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7336 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 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 4 ms 7380 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 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 7384 KB Output is correct
40 Correct 4 ms 7380 KB Output is correct
41 Correct 4 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 4 ms 7380 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 4 ms 7380 KB Output is correct
46 Correct 4 ms 7360 KB Output is correct
47 Correct 4 ms 7376 KB Output is correct
48 Correct 4 ms 7376 KB Output is correct
49 Correct 4 ms 7388 KB Output is correct
50 Correct 4 ms 7384 KB Output is correct
51 Correct 5 ms 7380 KB Output is correct
52 Correct 4 ms 7380 KB Output is correct
53 Correct 10 ms 8580 KB Output is correct
54 Correct 119 ms 19624 KB Output is correct
55 Correct 118 ms 19624 KB Output is correct
56 Correct 112 ms 19600 KB Output is correct
57 Correct 114 ms 19592 KB Output is correct
58 Correct 140 ms 32604 KB Output is correct
59 Correct 136 ms 27852 KB Output is correct
60 Correct 132 ms 28088 KB Output is correct
61 Correct 98 ms 20616 KB Output is correct
62 Correct 102 ms 19732 KB Output is correct
63 Correct 125 ms 18964 KB Output is correct
64 Correct 144 ms 18988 KB Output is correct
65 Correct 95 ms 25280 KB Output is correct
66 Correct 133 ms 34636 KB Output is correct
67 Correct 168 ms 36328 KB Output is correct
68 Correct 133 ms 19020 KB Output is correct
69 Correct 135 ms 19068 KB Output is correct
70 Correct 124 ms 18944 KB Output is correct
71 Correct 89 ms 19988 KB Output is correct
72 Correct 107 ms 20136 KB Output is correct
73 Correct 105 ms 20128 KB Output is correct
74 Correct 6 ms 8020 KB Output is correct
75 Correct 6 ms 7892 KB Output is correct
76 Correct 7 ms 7896 KB Output is correct
77 Correct 6 ms 8020 KB Output is correct
78 Correct 8 ms 8184 KB Output is correct
79 Correct 7 ms 7976 KB Output is correct
80 Correct 6 ms 8020 KB Output is correct
81 Correct 7 ms 8176 KB Output is correct
82 Correct 7 ms 8092 KB Output is correct
83 Correct 9 ms 7892 KB Output is correct
84 Correct 11 ms 7764 KB Output is correct
85 Correct 8 ms 8032 KB Output is correct
86 Correct 7 ms 8032 KB Output is correct
87 Correct 7 ms 8020 KB Output is correct
88 Correct 7 ms 7876 KB Output is correct
89 Correct 7 ms 8032 KB Output is correct
90 Correct 8 ms 8096 KB Output is correct
91 Correct 7 ms 7900 KB Output is correct
92 Correct 6 ms 8020 KB Output is correct
93 Correct 6 ms 7764 KB Output is correct
94 Correct 8 ms 7908 KB Output is correct
95 Correct 6 ms 8036 KB Output is correct
96 Correct 8 ms 7764 KB Output is correct
97 Correct 7 ms 7892 KB Output is correct
98 Correct 8 ms 7892 KB Output is correct
99 Correct 8 ms 8288 KB Output is correct
100 Correct 6 ms 7764 KB Output is correct
101 Correct 7 ms 7988 KB Output is correct
102 Correct 7 ms 7764 KB Output is correct
103 Correct 7 ms 7908 KB Output is correct
104 Correct 6 ms 7892 KB Output is correct
105 Correct 7 ms 7904 KB Output is correct
106 Correct 113 ms 20216 KB Output is correct
107 Correct 116 ms 19788 KB Output is correct
108 Correct 127 ms 19516 KB Output is correct
109 Correct 106 ms 19292 KB Output is correct
110 Correct 92 ms 21344 KB Output is correct
111 Correct 111 ms 21632 KB Output is correct
112 Correct 107 ms 22468 KB Output is correct
113 Correct 105 ms 17220 KB Output is correct
114 Correct 131 ms 18388 KB Output is correct
115 Correct 83 ms 21004 KB Output is correct
116 Correct 98 ms 25152 KB Output is correct
117 Correct 106 ms 23292 KB Output is correct
118 Correct 111 ms 21544 KB Output is correct
119 Correct 94 ms 17268 KB Output is correct
120 Correct 99 ms 17096 KB Output is correct
121 Correct 94 ms 16772 KB Output is correct
122 Correct 104 ms 16672 KB Output is correct
123 Correct 96 ms 21576 KB Output is correct
124 Correct 93 ms 21324 KB Output is correct
125 Correct 84 ms 21204 KB Output is correct
126 Correct 111 ms 20144 KB Output is correct
127 Correct 138 ms 19784 KB Output is correct
128 Correct 103 ms 19416 KB Output is correct
129 Correct 105 ms 19340 KB Output is correct
130 Correct 106 ms 21468 KB Output is correct
131 Correct 105 ms 21708 KB Output is correct
132 Correct 97 ms 22440 KB Output is correct
133 Correct 86 ms 17236 KB Output is correct
134 Correct 123 ms 18492 KB Output is correct
135 Correct 99 ms 21056 KB Output is correct
136 Correct 91 ms 22992 KB Output is correct
137 Correct 102 ms 22880 KB Output is correct
138 Correct 108 ms 21460 KB Output is correct
139 Correct 103 ms 17440 KB Output is correct
140 Correct 96 ms 17000 KB Output is correct
141 Correct 97 ms 16712 KB Output is correct
142 Correct 91 ms 16588 KB Output is correct
143 Correct 101 ms 21536 KB Output is correct
144 Correct 108 ms 21420 KB Output is correct
145 Correct 87 ms 21184 KB Output is correct
146 Correct 72 ms 15156 KB Output is correct
147 Correct 53 ms 14464 KB Output is correct
148 Correct 68 ms 14760 KB Output is correct
149 Correct 51 ms 15928 KB Output is correct
150 Correct 70 ms 24028 KB Output is correct
151 Correct 65 ms 15232 KB Output is correct
152 Correct 97 ms 16328 KB Output is correct
153 Correct 76 ms 16328 KB Output is correct
154 Correct 75 ms 18068 KB Output is correct
155 Correct 119 ms 21148 KB Output is correct
156 Correct 71 ms 19336 KB Output is correct
157 Correct 56 ms 18776 KB Output is correct
158 Correct 54 ms 18200 KB Output is correct
159 Correct 131 ms 19788 KB Output is correct
160 Correct 146 ms 31328 KB Output is correct
161 Correct 126 ms 19532 KB Output is correct
162 Correct 92 ms 20932 KB Output is correct