답안 #466257

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
466257 2021-08-18T12:17:12 Z ivan_tudor Star Trek (CEOI20_startrek) C++14
38 / 100
722 ms 19304 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
int tp[N], tpr[N];
int crit[N], critr[N];
vector<int> gr[N];
void add(long long &x, long long y){
  y%=MOD;
  x+=y;
  if(x>=MOD)
    x-=MOD;
  if(x<0)
    x+=MOD;
}
void add(int &x, long long y){
  y%=MOD;
  x+=y;
  if(x>=MOD)
    x-=MOD;
  if(x<0)
    x+=MOD;
}
void dfs(int nod, int dad){
  tp[nod] = 0;
  int nr0 = 0, sum = 0, unq = 0;
  for(auto x:gr[nod]){
    if(x != dad){
      dfs(x, nod);
      if(tp[x] == 0)
        nr0++, unq = crit[x];
      sum += crit[x];
    }
  }
  if(nr0 == 0){
    tp[nod] = 0;
    crit[nod] = sum + 1;
  }
  else if(nr0 == 1){
    tp[nod] = 1;
    crit[nod] = unq;
  }
  else{
    tp[nod] = 1;
    crit[nod] = 0;
  }
}
void changeroot(int nod, int dad){
  int OLDTP = tp[nod], OLDCRIT = crit[nod];
  int nr0 = 0, sum = 0, unq = 0;
  for(auto x:gr[nod]){
    if(tp[x] == 0 )
      nr0++, unq = crit[x];
    sum += crit[x];
  }
  if(nr0 == 0)
    tp[nod] = 0;
  else
    tp[nod] = 1;
  if(tp[nod] == 0){
    crit[nod] = sum + 1;
  }
  else if(tp[nod] == 1){
    crit[nod] = 0;
    if(nr0 == 1)
      crit[nod] = unq;
  }
  critr[nod] = crit[nod];
  tpr[nod] = tp[nod];

  for(auto x:gr[nod]){
    if(x == dad)
      continue;
    int oldtp = tp[nod], oldcrit = crit[nod];
    if(tp[nod] == 0){
      tp[nod] = 0;
      crit[nod] = sum + 1 - crit[x];
    }
    else{
      if(tp[x] == 1){
        tp[nod] = 1;
        crit[nod] = oldcrit;
      }
      else{
        if(nr0 == 1){
          tp[nod] = 0;
          crit[nod] = sum + 1 - crit[x];
        }
        else if(nr0 == 2){
          tp[nod] = 1;
          int othunq = 0;
          for(auto vc:gr[nod]){
            if(vc == x)
              continue;
            if(tp[vc] == 0)
              othunq = crit[vc];
          }
          crit[nod] = othunq;
        }
        else if(nr0 >= 3){
          tp[nod] = 1;
          crit[nod] = 0;
        }
      }

    }

    changeroot(x, nod);

    tp[nod] = oldtp;
    crit[nod] = oldcrit;
  }

  tp[nod] = OLDTP;
  crit[nod] = OLDCRIT;
}
long long dp[N][2];
int main()
{
  //freopen(".in","r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0),cout.tie(0);
  int n, d;
  cin>>n>>d;
  for(int i = 1; i < n; i++){
    int u, v;
    cin>>u>>v;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  dfs(1, 0);
  changeroot(1, 0);
  long long nr0 = 0, sumc0= 0, nr1 = 0, sumc1= 0;
  for(int i = 1; i<=n; i++){
    if(tpr[i] == 0){
      nr0++;
      add(sumc0, critr[i]);
    }
    else{
      nr1++;
      add(sumc1, critr[i]);
    }
    cerr<<tpr[i]<<" "<<critr[i]<<"\n";
  }
  dp[0][0] = nr0;
  dp[0][1] = nr1;
  for(int i = 1; i <d; i++){
    dp[i][0] = 0;
    //vine din 0
    add(dp[i][0], 1LL * sumc0 * dp[i-1][1]);
    add(dp[i][0], 1LL * (1LL*n*n%MOD - sumc0 + MOD)%MOD * ((dp[i-1][0] + dp[i-1][1])%MOD));

    //vine din 1
    add(dp[i][0], 1LL * sumc1 * dp[i-1][0]);

    dp[i][1] = 0;
    //vine din 0
    add(dp[i][1], 1LL * sumc0 * dp[i-1][0]);

    //vine din 1
    add(dp[i][1], 1LL * sumc1 * dp[i - 1][1]);
    add(dp[i][1], 1LL * (1LL*n*n%MOD - sumc1 + MOD)%MOD * ((dp[i-1][0] + dp[i-1][1])%MOD));
  }
  long long ans = 0;
  if(tpr[1] == 0){
    ans = 1LL * critr[1] * dp[d-1][0] % MOD;
  }
  else{
    ans = 1LL * critr[1] * dp[d - 1][1] % MOD;
    add(ans, 1LL * (n - critr[1] + MOD)%MOD * ((dp[d-1][0] + dp[d-1][1])%MOD));
  }
  cout<<ans;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Incorrect 8 ms 2764 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2676 KB Output is correct
3 Correct 3 ms 2672 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2676 KB Output is correct
3 Correct 3 ms 2672 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 9 ms 2800 KB Output is correct
8 Correct 10 ms 2848 KB Output is correct
9 Correct 10 ms 2748 KB Output is correct
10 Correct 9 ms 2680 KB Output is correct
11 Correct 8 ms 2748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2676 KB Output is correct
3 Correct 3 ms 2672 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 9 ms 2800 KB Output is correct
8 Correct 10 ms 2848 KB Output is correct
9 Correct 10 ms 2748 KB Output is correct
10 Correct 9 ms 2680 KB Output is correct
11 Correct 8 ms 2748 KB Output is correct
12 Correct 713 ms 13340 KB Output is correct
13 Correct 692 ms 19304 KB Output is correct
14 Correct 660 ms 8460 KB Output is correct
15 Correct 681 ms 8156 KB Output is correct
16 Correct 722 ms 8224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2676 KB Output is correct
3 Correct 3 ms 2672 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 9 ms 2800 KB Output is correct
8 Correct 10 ms 2848 KB Output is correct
9 Correct 10 ms 2748 KB Output is correct
10 Correct 9 ms 2680 KB Output is correct
11 Correct 8 ms 2748 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Incorrect 8 ms 2636 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 3 ms 2676 KB Output is correct
3 Correct 3 ms 2672 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 9 ms 2800 KB Output is correct
8 Correct 10 ms 2848 KB Output is correct
9 Correct 10 ms 2748 KB Output is correct
10 Correct 9 ms 2680 KB Output is correct
11 Correct 8 ms 2748 KB Output is correct
12 Correct 713 ms 13340 KB Output is correct
13 Correct 692 ms 19304 KB Output is correct
14 Correct 660 ms 8460 KB Output is correct
15 Correct 681 ms 8156 KB Output is correct
16 Correct 722 ms 8224 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Incorrect 8 ms 2636 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2636 KB Output is correct
2 Incorrect 8 ms 2764 KB Output isn't correct