답안 #635627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635627 2022-08-26T14:03:08 Z 406 Subtree (INOI20_subtree) C++17
12 / 100
73 ms 33744 KB
#include <bits/stdc++.h>
using ll = long long;
using namespace std;

#define forn(a, b) for (int a = 0; a < (b); a++)
#define rofn(a, b) for (int a = (b) - 1; a >= 0; a--)
#define LOG(a) (31 - __builtin_clz(a))
#define chmx(a, b) a = max(a, b)
#define chmn(a, b) a = min(a, b)
#define pb push_back
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1,*p2;

inline int read() {
	int x = 0, f = 1; char c;
	while(!isdigit(c = gc())) if(c=='-') f=-1;
	while(isdigit(c)) x = (x<<3)+(x<<1)+(c^48), c = gc();
	return x * f;
}

const ll INFL = 1ll << 60;
const int INF = 1 << 29;
const int N = 1e5 + 50;
const int MOD = 1e9 + 7;

ll pw(ll a, ll b) {
    a %= MOD;
    b %= (MOD - 1);
    ll re = 1;
    while (b) {
        if (b & 1)
            re = (re * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return re;
}

ll dp[N];
int h[N], par[N], n, m;
bitset<N> mark;
vector<int> adj[N];

void dfs(int v) {
    mark[v] = true;
    dp[v] = 1;
    vector<int> unp;

    for (auto u: adj[v]) {
        if (!mark[u]) {
            par[u] = v;
            h[u] = h[v] + 1;
            dfs(u);
            dp[v] = (dp[v] * (dp[u] + 1)) % MOD;
        }
        else if (u != par[v] && h[u] > h[v]) {
            unp.pb(u);
        }
    }

    for (auto u: unp) {
        vector<ll> X;
        int last = n;
        while (u != par[v]) {
            X.pb((dp[u] * pw((dp[last] + 1), MOD - 2)) % MOD);
            if (X.back() < 0)
                X.back() += MOD;
            last = u;
            u = par[u];
        }

        dp[v] = 0;
        reverse(X.begin(), X.end());

        vector<ll> pref, P, suf;
        suf.pb(1);
        for (int i = X.size(); i >= 2; i--) {
            suf.pb((suf.back() * X[i - 1]) % MOD);
        }
        reverse(suf.begin(), suf.end());

        pref.pb(X[0]);
        for (int i = 1; i < X.size(); i++)
            pref.pb((pref.back() * X[i]) % MOD);

        P.resize(pref.size());
        for (int i = 0; i < P.size(); i++)
            P[i] = ((i ? P[i - 1] : 0) + pref[i]) % MOD;

        for (int i = 2; i < X.size(); i++) {
            dp[v] += (suf[i] * P[i - 2]) % MOD;
            if (dp[v] >= MOD)
                dp[v] -= MOD;
        }

        ll kol = suf.front();
        for (int i = 1; i < X.size(); i++) {
            dp[v] += (kol * pw(X[i], MOD - 2)) % MOD;
            if (dp[v] >= MOD)
                dp[v] -= MOD;
        }
    }
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[v].pb(u);
        adj[u].pb(v);
    }
    par[0] = -1;
    dfs(0);

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += dp[i];
        if (ans >= MOD)
            ans -= MOD;
    }
    cout << ans << '\n';

    return 0;
}

Compilation message

Main.cpp: In function 'void dfs(int)':
Main.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int i = 1; i < X.size(); i++)
      |                         ~~^~~~~~~~~~
Main.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for (int i = 0; i < P.size(); i++)
      |                         ~~^~~~~~~~~~
Main.cpp:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         for (int i = 2; i < X.size(); i++) {
      |                         ~~^~~~~~~~~~
Main.cpp:97:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for (int i = 1; i < X.size(); i++) {
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2676 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Incorrect 2 ms 2676 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 5 ms 3156 KB Output is correct
3 Correct 65 ms 7612 KB Output is correct
4 Correct 66 ms 7548 KB Output is correct
5 Correct 56 ms 7612 KB Output is correct
6 Correct 39 ms 7500 KB Output is correct
7 Correct 56 ms 27880 KB Output is correct
8 Correct 54 ms 20336 KB Output is correct
9 Correct 56 ms 20260 KB Output is correct
10 Correct 55 ms 8028 KB Output is correct
11 Correct 39 ms 7748 KB Output is correct
12 Correct 41 ms 7500 KB Output is correct
13 Correct 46 ms 7596 KB Output is correct
14 Correct 48 ms 16144 KB Output is correct
15 Correct 66 ms 30844 KB Output is correct
16 Correct 73 ms 33744 KB Output is correct
17 Correct 47 ms 7624 KB Output is correct
18 Correct 48 ms 7628 KB Output is correct
19 Correct 41 ms 7676 KB Output is correct
20 Correct 34 ms 7796 KB Output is correct
21 Correct 35 ms 7956 KB Output is correct
22 Correct 34 ms 7808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2676 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Incorrect 2 ms 2676 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2676 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Incorrect 2 ms 2676 KB Output isn't correct
9 Halted 0 ms 0 KB -