Submission #379522

# Submission time Handle Problem Language Result Execution time Memory
379522 2021-03-18T12:08:46 Z ACmachine Star Trek (CEOI20_startrek) C++17
0 / 100
1 ms 364 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;

#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)

const double EPS = 1e-9;
const int MOD = 1e9+7;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

void DBG(){cout << "]" << endl;}
template<typename T, typename ...U> void DBG(const T& head, const U... args){ cout << head << "; "; DBG(args...); }
#define dbg(...) cout << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__);
#define chk() cout << "Check at line(" << __LINE__ << ") hit." << endl;

template<class T, unsigned int U>
ostream& operator<<(ostream& out, const array<T, U> &v){out << "[";  REP(i, U) out << v[i] << ", ";  out << "]"; return out;}
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "[";  REP(i, v.size()) out << v[i] << ", ";  out << "]"; return out;}

template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
const int mod = (int)1e9+7;
struct Mint{
    int val;
    Mint(){ val = 0; }
    Mint(long long v){
        val = (-mod <= v && v < mod) ? v : v%mod;
        if(val < 0) val+= mod;
    }
    // operators
    Mint operator-(){return Mint(-val);}
    Mint& operator+=(const Mint& other){ val+=other.val; if(val >= mod) val-=mod; return *this;}
    Mint& operator-=(const Mint& other){ val-=other.val; if(val < 0) val+=mod; return *this;}
    Mint& operator*=(const Mint& other){ val=((long long)val*other.val)%mod; return *this;}
    friend Mint binpow(Mint a, long long p){
        Mint res(1);
        while(p > 0){
            if(p&1) res*=a; a*=a; p>>=1;
        }
        return res;
    }
    friend Mint inv(Mint a){ return binpow(a, mod-2); }
    Mint& operator/=(const Mint &other){ return (*this)*=inv(other);}

    friend Mint operator+(Mint a, const Mint& b){ return a+=b; }
    friend Mint operator-(Mint a, const Mint& b){ return a-=b; }
    friend Mint operator*(Mint a, const Mint& b){ return a*=b; }
    friend Mint operator/(Mint a, const Mint& b){ return a/=b; }

    bool operator==(const Mint &other){return val == other.val;}
    bool operator!=(const Mint &other){return val != other.val;}
    bool operator<(const Mint &other){return val < other.val;}
    // io
    friend ostream& operator<<(ostream &out, Mint &a){ return out << a.val; }
};
int main(){
 	ios_base::sync_with_stdio(false);
 	cin.tie(NULL); cout.tie(NULL);
    ll n, d;
    cin >> n >> d;
    vector<vector<int>> g(n);
    REP(i, n - 1){
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].pb(v);
        g[v].pb(u);
    }
    auto solve_subtask1 = [&](){
        if(d % 2 == 0){
            Mint ans = binpow(Mint(2), d);
            cout << ans << "\n";
        }
        else{
            cout << 0 << "\n";
        }
    };
    auto solve = [&](){
        vector<int> dp(n, 0);
        function<void(int, int)> dfs = [&](int v, int p){
            for(int x : g[v]){
                if(x == p) continue;
                dfs(x, v);
                dp[v] |= (!dp[x] ? 1 : 0);
            }
        };
        vector<int> new_dp = dp;
        function<void(int, int)> reroot = [&](int v, int p){
            vector<int> pref(g[v].size(), 0);
            vector<int> suf(g[v].size(), 0);
            int cnt = 0;
            for(int x : g[v]){
                if(!dp[x])
                    pref[cnt] = 1;
                if(cnt != 0)
                    pref[cnt] |= pref[cnt - 1];
                cnt++;
            }
            reverse(all(g[v]));
            cnt = 0;
            for(int x : g[v]){
                if(!dp[x])
                    suf[cnt] = 1;
                if(cnt != 0)
                    suf[cnt] |= suf[cnt - 1];
                cnt++;
            }
            int prevdp = dp[v];
            new_dp[v] = pref[(int)g[v].size() - 1]; // ans;
            reverse(all(suf));
            reverse(all(g[v]));
            REP(i, g[v].size()){
                if(g[v][i] == p) continue;
                dp[v] = pref[i - 1] | suf[i + 1];
                reroot(g[v][i], v);
            }
            dp[v] = prevdp;
        };
        dfs(0, -1);
        reroot(0, -1);
        int n_winning = 0;
        int n_losing = 0;
        REP(i, n){
            if(new_dp[i] == 0)
                n_losing++;
            else
                n_winning++;
        }
        vector<array<Mint, 2>> dp_placed(n);
        function<void(int, int)> rec = [&](int v, int p){
            int cnt_losing = 0;
            for(int x : g[v]){
                if(x == p) continue;
                rec(x, v);
                // how many winning?
                if(dp[x]) cnt_losing++;
                dp_placed[v][1] += dp_placed[x][0];
            }
            if(cnt_losing == 0){
                for(int x : g[v]){
                    if(x == p) continue;
                    dp_placed[v][0] += dp_placed[x][1];
                }
            }
            else if(cnt_losing > 1){
                dp_placed[v][0] = Mint(0);
            }
            else{
                for(int x : g[v]){
                    if(dp[x]){
                        dp_placed[v][0] = dp_placed[x][1];
                    }
                }
            }
            // now what if I place edge
            if(dp[v]){ // winning
                dp_placed[v][1] += Mint(n);
            }
            else{
                dp_placed[v][1] += Mint(n_losing);
                dp_placed[v][0] += Mint(n_winning);
            }
        };
        rec(0, -1);
        cout << dp_placed[0][1] << "\n";
    };
    if(n == 2){
        solve_subtask1();
    }
    else{
        solve();
    }
    return 0;
}

Compilation message

startrek.cpp: In function 'Mint binpow(Mint, long long int)':
startrek.cpp:72:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   72 |             if(p&1) res*=a; a*=a; p>>=1;
      |             ^~
startrek.cpp:72:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   72 |             if(p&1) res*=a; a*=a; p>>=1;
      |                             ^
startrek.cpp: In lambda function:
startrek.cpp:19:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
      |                                        ^
startrek.cpp:21:18: note: in expansion of macro 'FOR'
   21 | #define REP(i,b) FOR(i,0,b,1)
      |                  ^~~
startrek.cpp:146:13: note: in expansion of macro 'REP'
  146 |             REP(i, g[v].size()){
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct