답안 #1060548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1060548 2024-08-15T17:08:21 Z vjudge1 Subtree (INOI20_subtree) C++14
12 / 100
522 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pf push_front
#define fi first
#define se second
const ll mod = 1e9+7, mxn = 1e5+7;
ll n, m, pr[mxn], dp[mxn][2], mul[mxn<<1];
bool ck[mxn], vis[mxn];
vector<ll> cycle;
vector<vector<ll>> g(mxn);
void get_cycle(ll u, ll v)
{
    // cerr << u << '\n'; 
    cycle.pb(u); ck[u] = 1;
    if (u == v) return;
    get_cycle(pr[u],v);
}
void dfs(ll u, ll v)
{
    vis[u] = 1;
    for (ll i : g[u]) if (i != v) 
    {
        if (vis[i] && !cycle.size()) 
        {
            // cerr << u << ' ' << i << '\n';
            get_cycle(u,i);
        }
        else if (!vis[i])
        {
            pr[i] = u;
            dfs(i,u);
        }
    }
}
void dfs_dp(ll u, ll v)
{
    dp[u][1] = 1;
    for (ll i : g[u]) if (i != v && !ck[i])
    {
        dfs_dp(i,u);
        dp[u][1] *= dp[i][1]+1;
        dp[u][1] %= mod;
        dp[u][0] += (dp[i][0]+dp[i][1])%mod;
        if (dp[u][0] >= mod) dp[u][0] -= mod;
    }
}
ll bpow(ll a, ll b)
{
    if (!b) return 1;
    ll m = bpow(a,b/2);
    if (b&1) return (((m*m)%mod)*a)%mod;
    return (m*m)%mod;
}
signed main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr);
    cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        ll a, b; cin >> a >> b;
        g[a].pb(b); g[b].pb(a);        
    }
    dfs(1,1);
    if (!cycle.size()) cycle.pb(1);
    for (ll i : cycle) dfs_dp(i,i);
    ll ans = 0, id = 0; mul[0] = 1;
    for (ll i : cycle)
    {
        ans += (dp[i][0]+dp[i][1])%mod;
        if (ans >= mod) ans -= mod;
        id++; mul[id] = dp[i][1];
    }
    n = cycle.size();
    for (ll i = n+1; i <= 2*n; i++) mul[i] = mul[i-n];
    for (ll i = 1; i <= 2*n; i++) mul[i] = (mul[i]*mul[i-1])%mod;
    queue<ll> q;
    ll s = 0;
    for (ll i = 0; i < n-1; i++) 
    {
        ll inv = bpow(mul[i],mod-2);
        q.push(inv); s += inv;
        if (s >= mod) s -= mod;
    }
    for (ll i = n-1; i <= 2*n-2; i++)
    {
        ans += (s*mul[i])%mod;
        if (ans >= mod) ans -= mod;
        s -= q.front();
        if (ans < 0) ans += mod;
        ll inv = bpow(mul[i-2],mod-2);
        q.push(inv); s += inv;
        if (s >= mod) s -= mod;
    }   
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Runtime error 522 ms 1048576 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 5 ms 5212 KB Output is correct
3 Correct 31 ms 11516 KB Output is correct
4 Correct 26 ms 11684 KB Output is correct
5 Correct 28 ms 11592 KB Output is correct
6 Correct 29 ms 11500 KB Output is correct
7 Correct 34 ms 15632 KB Output is correct
8 Correct 32 ms 14244 KB Output is correct
9 Correct 31 ms 14420 KB Output is correct
10 Correct 37 ms 12120 KB Output is correct
11 Correct 44 ms 11428 KB Output is correct
12 Correct 29 ms 11100 KB Output is correct
13 Correct 49 ms 11048 KB Output is correct
14 Correct 23 ms 13520 KB Output is correct
15 Correct 37 ms 16300 KB Output is correct
16 Correct 32 ms 16824 KB Output is correct
17 Correct 39 ms 11092 KB Output is correct
18 Correct 38 ms 11348 KB Output is correct
19 Correct 31 ms 11100 KB Output is correct
20 Correct 21 ms 11736 KB Output is correct
21 Correct 28 ms 12020 KB Output is correct
22 Correct 28 ms 11796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Runtime error 522 ms 1048576 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Runtime error 522 ms 1048576 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -