# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1171442 | Zero_OP | Star Trek (CEOI20_startrek) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r)-1; i >= (l); --i)
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
typedef long long ll;
using db = double;
using ull = unsigned long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<db>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vpl = vector<pl>;
void setIO(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif
}
const int MAX = 1e5 + 5;
const int mod = 1e9 + 7;
struct mint{
int v;
mint() : v(0) {}
mint(int v) : v(v) {}
mint& operator += (const mint& o){
v += o.v;
if(v >= mod) v -= mod;
return *this;
}
mint& operator -= (const mint& o){
v -= o.v;
if(v < 0) v += mod;
return *this;
}
mint& operator *= (const mint& o){
v = 1LL * v * o.v % mod;
return *this;
}
mint power(ll n) const {
mint res(1), base = *this;
for(; n > 0; n >>= 1, base *= base) if(n & 1) res *= base;
return res;
}
mint inv() const {
return power(mod - 2);
}
mint& operator /= (const mint& o){
return *this *= o.inv();
}
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; }
friend ostream& operator << (ostream& out, const mint& o){ return out << o.v; }
};
int N;
ll D;
vi adj[MAX * 2];
namespace subtask2{
bool dp[MAX * 2];
void dfs(int u, int p){
dp[u] = false;
for(auto v : adj[u]) if(v != p){
dfs(v, u);
if(!dp[v]) dp[u] = 1;
}
}
bool check(){
dfs(0, -1);
return dp[0];
}
void solve(){
FOR(i, 0, N){
for(auto j : adj[i]) adj[i+N].pb(j+N);
}
int ans = 0;
FOR(i, 0, N){
FOR(j, 0, N){
adj[i].pb(j+N);
// cout << dbg(i) << dbg(j+N) << '\n';
ans += check();
adj[i].pop_back();
}
}
cout << ans << '\n';
}
}
namespace subtaskD1{
bool dp[MAX], if_start_from[MAX], above[MAX];
int num_zeroes[MAX], num_ones[MAX];
void calc(int u, int p){
dp[u] = false;
for(auto v : adj[u]) if(v != p){
calc(v, u);
if(!dp[v]) dp[u] = true;
num_zeroes[u] += (dp[v] == 0);
num_ones[u] += (dp[v] == 1);
}
}
void reroot(int u, int p){
vi pref, suff;
for(auto v : adj[u]) if(v != p){
pref.pb(dp[v]);
suff.pb(dp[v]);
}
for(int i = 0, j = sz(suff)-1; i < sz(pref); ++i, --j){
if(i > 0) pref[i] &= pref[i-1];
if(j+1<sz(suff)) suff[j] &= suff[j+1];
}
int i = 0;
for(auto v : adj[u]) if(v != p){
int val = above[u];
if(i>0 && !pref[i-1]) val = true;
if(i+1<sz(suff) && !suff[i+1]) val = true;
// cout << dbg(v) << dbg(val) << dbg(above[u]) << '\n';
above[v] = !val;
if_start_from[v] = dp[v];
if(!dp[v] && !val) if_start_from[v] = true;
++i;
reroot(v, u);
}
}
int n_win_roots, n_lose_roots;
void calc_ways(int u, int p){
for(auto v : adj[u]) if(v != p){
//we can only change from dp[u] = false to dp[u] = true
if(dp[u]){
}
}
}
void solve(){
calc(0, -1);
if_start_from[0] = dp[0];
above[0] = false;
reroot(0, -1);
n_win_roots = 0, n_lose_roots = 0;
FOR(i, 0, N){
if(!if_start_from[i]) ++n_lose_roots;
else ++n_win_roots;
// calc(i, -1);
// cout << dbg(i) << dbg(if_start_from[i]) << dbg(dp[i]) << '\n';
// assert(if_start_from[i] == dp[i]);
}
calc_ways(0, -1);
}
}
int main(){
setIO();
cin >> N >> D;
FOR(i, 0, N-1){
int u, v;
cin >> u >> v;
--u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
if(N == 2){
cout << mint(4).power(D) << '\n';
return 0;
}
if(D == 1){
return subtaskD1::solve(), 0;
}
return 0;
}
caladflasflfhlalkaslkakhlafhlkssd