Submission #931800

# Submission time Handle Problem Language Result Execution time Memory
931800 2024-02-22T11:01:51 Z LucaIlie Star Trek (CEOI20_startrek) C++17
7 / 100
1 ms 4700 KB
#include <bits/stdc++.h>

#define int long long

using namespace std;

const int MAX_N = 1e5;
const int MAX_D = 1e5;
const int MOD = 1e9 + 7;
bool canWin[MAX_N + 1];
int countChildrenLosers[MAX_N + 1], add[MAX_N + 1], reach[MAX_N + 1], dp[MAX_D][3];
vector<int> adj[MAX_N + 1];

void dfsGame( int u, int p ) {
    canWin[u] = false;
    countChildrenLosers[u] = 0;
    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        dfsGame( v, u );
        canWin[u] |= (!canWin[v]);
        countChildrenLosers[u] += (!canWin[v]);
    }
}

void dfsCalcAdd( int u, int p ) {
    if ( p == 0 ) {
        add[u] = (canWin[u] ? -1 : 1);
        reach[u] = u;
    } else {
        if ( canWin[u] ) {
            if ( canWin[p] ) {
                add[u] = -1;
                reach[u] = u;
            } else {
                add[u] = add[p] - 1;
                reach[u] = reach[p];
            }
        } else {
            if ( countChildrenLosers[p] >= 2 ) {
                add[u] = 1;
                reach[u] = u;
            } else {
                add[u] = add[p] + 1;
                reach[u] = reach[p];
            }
        }
    }

    for ( int v: adj[u] ) {
        if ( v == p )
            continue;
        dfsCalcAdd( v, u );
    }
}

int lgPut( int x, int n ) {
    if ( n == 0 )
        return 1;

    int p = lgPut( x, n / 2 );
    p = (long long)p * p % MOD;
    if ( n % 2 == 1 )
        p = (long long)p * x % MOD;

    return p;
}

signed main() {
    int n, d;

    cin >> n >> d;
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back( v );
        adj[v].push_back( u );
    }

    cout << lgPut( 4, d ) << "\n";
    return 0;

    int pom = 0;
    int winners = 0;
    for ( int v = 1; v <= n; v++ ) {
        dfsGame( v, 0 );
        winners += canWin[v];
        int w = 0;
        for  ( int u = 1; u <= n; u++ )
            w += canWin[u];
        if (pom == 0)
            pom = w;
        if ( pom != w )
            exit( 1 );
    }

    dfsGame( 1, 0 );
    dfsCalcAdd( 1, 0 );

    int countAdd1 = 0;
    for ( int v = 1; v <= n; v++ )
        countAdd1 += (add[v] == 1);

    dp[0][0] = 1;
    for ( int i = 1; i < d; i++ ) {
        for ( int a = 0; a < 2; a++ ) {
            int add1 = countAdd1 * (n - (winners + a)) % MOD;
            int add0 = (n * n - add1) % MOD;

            dp[i][0] = (dp[i][0] + dp[i - 1][a] * add0) % MOD;
            dp[i][1] = (dp[i][1] + dp[i - 1][a] * add1) % MOD;
        }
        printf( "%d %d\n", dp[i][0], dp[i][1] );

    }

    int ans = 0;
    for ( int a = 0; a < 2; a++ ) {
        for ( int v = 1; v <= n; v++ ) {
            if ( canWin[1] ) {
                if ( canWin[v] )
                    ans = (ans + n * dp[d - 1][a]) % MOD;
                else {
                    ans = (ans + (winners + a) * dp[d - 1][a]) % MOD;
                    if ( reach[v] != 1 )
                        ans = (ans + (n - winners - a) * dp[d - 1][a]) % MOD;
                }
            } else {
                if ( !canWin[v] ) {
                    if ( reach[v] == 1 )
                        ans = (ans + (n - winners - a) * dp[d - 1][a]) % MOD;
                }
            }
        }
    }

    cout << ans << "\n";

    return 0;
}

Compilation message

startrek.cpp: In function 'int main()':
startrek.cpp:113:19: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
  113 |         printf( "%d %d\n", dp[i][0], dp[i][1] );
      |                  ~^        ~~~~~~~~
      |                   |               |
      |                   int             long long int
      |                  %lld
startrek.cpp:113:22: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long long int' [-Wformat=]
  113 |         printf( "%d %d\n", dp[i][0], dp[i][1] );
      |                     ~^               ~~~~~~~~
      |                      |                      |
      |                      int                    long long int
      |                     %lld
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Incorrect 1 ms 4700 KB Output isn't correct
3 Halted 0 ms 0 KB -