답안 #1005118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1005118 2024-06-22T07:29:59 Z AlperenT_ Star Trek (CEOI20_startrek) C++17
100 / 100
49 ms 28652 KB
#include <bits/stdc++.h>
 

#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second 
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define ld long double
#define int long long
#define sz(v) (int)v.size()
#define rep(i , a , b) for(int i=a;i <= b;i++)
#define per(i, a , b) for(int i=a;i >= b;i--)
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 2e5  ,  N = 1e5 +1 , lg = 20 , maxq = 202 , sq = 100   , inf = 1e8 +100 , maxk = 2022 , mod = 1e9 + 7;
int dp[maxn][2] , s[maxn][2] , par[maxn] , ans[maxn] , ted[maxn]  ; 
vector <int> G[maxn] ;

int po(int a, int b){
  if(b==0)return 1 ;
  int v = po(a,b/2) ;
  v =v*v % mod ; 
  if(b&1) v = v*a % mod ;
  return v ;
}

void down(int v, int p =0){
  dp[v][0] = 0;int sum =0 ; 
  for(int u : G[v]){
    if(u == p)continue ;
    down(u , v) ;
    dp[v][0] |= !dp[u][0] ;  
    sum += !dp[u][0 ];
  }
  if(dp[v][0] == 0){
    s[v][0] = 1; 
    for(int u : G[v]){
      if(u == p)continue ;
      s[v][0] += s[u][0] ; 
    }
  }else{
    if(sum==1)
    for(int u : G[v]){
      if(u == p || dp[u][0] == 1)continue ; 
      s[v][0] += s[u][0] ;
    }
  }
} 
void up(int v ,int p=0){
  int t[2] , f[2] ; 
  t[0] = t[1] = f[0] = f[1] =0 ;
  if(p != 0){
    t[dp[v][1]] ++ ;
    f[dp[v][1]] += s[v][1] ;
  }
  for(int u : G[v]){
    if(u == p)continue ;
    t[dp[u][0]]++;
    f[dp[u][0]]+=s[u][0] ; 
  } 
  for(int u : G[v]){
    if(u == p)continue  ;
    t[dp[u][0]]--;
    f[dp[u][0]]-=s[u][0] ;
    if(t[0] == 0){
      dp[u][1] = 0 ;
      s[u][1] = 1 + f[1] ;
    }else{
      dp[u][1] = 1 ; 
      if(t[0] == 1){
        s[u][1] += f[0] ; 
      }
      
    }
    t[dp[u][0]]++;
    f[dp[u][0]]+=s[u][0] ;
  }
  if(t[0] == 0){
    ans[v] = 0 ;
    ted[v] = f[1] + 1  ; 
  }else{
    ans[v] =1 ;
    if(t[0] == 1)ted[v] = f[0] ; 
  }
  for(int u : G[v]){
    if(u ==p)continue ;
    up(u , v) ;  
  }
}
struct mat{
  int a[4][4] , n , m ; 
  mat(){
    rep(i , 0 ,2)rep(j , 0 , 2)a[i][j] = 0 ; 
  }
};;

mat operator* (mat a , mat b){
  mat r ; 
  r.n = b.n ;
  r.m = a.m ;
  rep(i , 0 , r.n-1){
    rep(j , 0 , r.m-1){
      rep(k , 0 , a.n-1){
        r.a[i][j]=  (r.a[i][j] + a.a[k][j] * b.a[i][k]%mod)%mod ;
      }
    }
  }
  return r ; 
}
mat pw(mat a , int d){
  mat ans ;
  rep(i , 0, 1)ans.a[i][i] = 1 ;
  ans.n = ans.m = 2 ;
  while(d){
    if(d&1) ans = ans * a ;
    a = a*a ;
    d/=2; 
  }
  return ans ; 
}

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);   
  int n , d; cin >> n >> d ;
  rep(i , 1, n-1){
    int v , u ;cin >> v >> u ;
    G[v].pb(u) ;
    G[u].pb(v) ;
  }
  down(1) ;
  up(1) ;
  int t1 = 0  , t2 =0  ,t3 =0 ;  
  rep(i , 1, n){
    if(ans[i] == 0){
      t1 ++ ;
      t3 = (t3 + ted[i])%mod ;
      t2 = (t2 + n-ted[i])%mod ;
   }else{
     t3 = (t3 + n-ted[i])%mod ;
     t2 = (t2 + ted[i])%mod ;
   }
  }
  mat z ;z.n = 2 ; z.m = 2 ;
  z.a[0][1] = n*t1%mod ;
  z.a[0][0] = t2 ;
  //
  z.a[1][1] = n*(n-t1)%mod ;
  z.a[1][0] = t3 ;
  mat a ;a.n = 2 ; a.m = 1 ;
  a.a[0][0] = t1 ;a.a[1][0] = n-t1 ;
  mat x = pw(z , d-1) ;
  a = a*x ;
  int g = ted[1] * a.a[0][0] % mod ;
  if(ans[1] == 1)g = (po(n*n%mod , d) - g + mod)%mod ;
  cout << g << "\n" ;
}
/*

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10892 KB Output is correct
2 Correct 1 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10892 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10892 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10892 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 37 ms 23692 KB Output is correct
13 Correct 39 ms 28652 KB Output is correct
14 Correct 25 ms 19284 KB Output is correct
15 Correct 37 ms 19536 KB Output is correct
16 Correct 31 ms 19536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10892 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 2 ms 10840 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 1 ms 11096 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 1 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 2 ms 10896 KB Output is correct
22 Correct 3 ms 10844 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10840 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10892 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 37 ms 23692 KB Output is correct
13 Correct 39 ms 28652 KB Output is correct
14 Correct 25 ms 19284 KB Output is correct
15 Correct 37 ms 19536 KB Output is correct
16 Correct 31 ms 19536 KB Output is correct
17 Correct 2 ms 10840 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 1 ms 11096 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 1 ms 10844 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10896 KB Output is correct
27 Correct 3 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10844 KB Output is correct
32 Correct 2 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 2 ms 10840 KB Output is correct
35 Correct 2 ms 10844 KB Output is correct
36 Correct 33 ms 23636 KB Output is correct
37 Correct 36 ms 28496 KB Output is correct
38 Correct 26 ms 19436 KB Output is correct
39 Correct 32 ms 19548 KB Output is correct
40 Correct 29 ms 19548 KB Output is correct
41 Correct 35 ms 26204 KB Output is correct
42 Correct 34 ms 27220 KB Output is correct
43 Correct 22 ms 18900 KB Output is correct
44 Correct 49 ms 19560 KB Output is correct
45 Correct 30 ms 19636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10892 KB Output is correct
4 Correct 1 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10840 KB Output is correct
10 Correct 2 ms 10892 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10840 KB Output is correct
19 Correct 37 ms 23692 KB Output is correct
20 Correct 39 ms 28652 KB Output is correct
21 Correct 25 ms 19284 KB Output is correct
22 Correct 37 ms 19536 KB Output is correct
23 Correct 31 ms 19536 KB Output is correct
24 Correct 2 ms 10840 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 1 ms 11096 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 1 ms 10844 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 2 ms 10844 KB Output is correct
32 Correct 2 ms 10844 KB Output is correct
33 Correct 2 ms 10896 KB Output is correct
34 Correct 3 ms 10844 KB Output is correct
35 Correct 2 ms 10844 KB Output is correct
36 Correct 2 ms 10844 KB Output is correct
37 Correct 2 ms 10844 KB Output is correct
38 Correct 2 ms 10844 KB Output is correct
39 Correct 2 ms 10844 KB Output is correct
40 Correct 2 ms 10844 KB Output is correct
41 Correct 2 ms 10840 KB Output is correct
42 Correct 2 ms 10844 KB Output is correct
43 Correct 33 ms 23636 KB Output is correct
44 Correct 36 ms 28496 KB Output is correct
45 Correct 26 ms 19436 KB Output is correct
46 Correct 32 ms 19548 KB Output is correct
47 Correct 29 ms 19548 KB Output is correct
48 Correct 35 ms 26204 KB Output is correct
49 Correct 34 ms 27220 KB Output is correct
50 Correct 22 ms 18900 KB Output is correct
51 Correct 49 ms 19560 KB Output is correct
52 Correct 30 ms 19636 KB Output is correct
53 Correct 35 ms 28504 KB Output is correct
54 Correct 39 ms 26564 KB Output is correct
55 Correct 20 ms 18388 KB Output is correct
56 Correct 31 ms 22184 KB Output is correct
57 Correct 29 ms 19800 KB Output is correct
58 Correct 33 ms 19796 KB Output is correct
59 Correct 30 ms 19536 KB Output is correct
60 Correct 33 ms 19516 KB Output is correct