Submission #466279

# Submission time Handle Problem Language Result Execution time Memory
466279 2021-08-18T12:49:43 Z ivan_tudor Star Trek (CEOI20_startrek) C++14
100 / 100
785 ms 23324 KB
#include<bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long MOD = 1e9 + 7;
long long tp[N], tpr[N];
long long 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 dfs(long long nod, long long dad){
  tp[nod] = 0;
  long long 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(long long nod, long long dad){
  long long OLDTP = tp[nod], OLDCRIT = crit[nod];
  long long 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;
    long long 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;
          long long 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 mat[2][2];
long long aux[2][2];
long long rez[2][2];

void multiply(long long A[2][2], long long B[2][2], long long rez[2][2]){
  for(long long i = 0; i < 2; i++)
    for(long long j = 0; j < 2; j++)
      rez[i][j] = 0;
  for(long long i = 0; i <2; i++){
    for(long long j = 0; j < 2; j++){
      for(long long k = 0; k < 2; k++){
        add(rez[i][j], 1LL * A[i][k] * B[k][j]);
      }
    }
  }

}
void cpy(long long A[2][2], long long B[2][2]){
  for(long long i = 0; i < 2; i++){
    for(long long j = 0; j < 2; j++){
      B[i][j] = A[i][j];
    }
  }
}
void mknull(long long A[2][2]){
  for(long long i = 0; i < 2; i++){
    for(long long j = 0; j < 2; j++){
      A[i][j] = 0;
    }
  }
  A[0][0] = A[1][1] = 1;
}
void lgcput(long long pwr){
  mknull(rez);
  while(pwr){
    if(pwr & 1){
      multiply(mat, rez, aux);
      cpy(aux, rez);
    }
    multiply(mat, mat, aux);
    cpy(aux, mat);
    pwr/=2;
  }
}
int main()
{
  //freopen(".in","r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0),cout.tie(0);
  long long n, d;
  cin>>n>>d;
  for(long long i = 1; i < n; i++){
    long long 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(long long 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";
  }
  mat[0][0] = 1LL *(1LL * n * nr0 + sumc1 - sumc0 + MOD)%MOD;
  mat[0][1] = 1LL *(1LL * n * nr1 + sumc0 - sumc1 + MOD)%MOD;
  mat[1][0] = 1LL * n * nr0 % MOD;
  mat[1][1] = 1LL * n * nr1 % MOD;
  lgcput(d - 1);
  // puterea se afla in rez
  mat[0][0] = nr0;
  mat[0][1] = nr1;
  mat[1][0] = mat[1][1] = 0;

  multiply(mat, rez, aux);
  long long ans = 0;
  if(tpr[1] == 0){
    ans = 1LL * critr[1] * aux[0][0] % MOD;
  }
  else{
    ans = 1LL * critr[1] * aux[0][1] % MOD;
    add(ans, 1LL * (n - critr[1] + MOD)%MOD * ((aux[0][0] + aux[0][1])%MOD));
  }
  cout<<ans;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 8 ms 2748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2672 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 12 ms 2820 KB Output is correct
8 Correct 9 ms 2868 KB Output is correct
9 Correct 8 ms 2636 KB Output is correct
10 Correct 9 ms 2636 KB Output is correct
11 Correct 9 ms 2748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 12 ms 2820 KB Output is correct
8 Correct 9 ms 2868 KB Output is correct
9 Correct 8 ms 2636 KB Output is correct
10 Correct 9 ms 2636 KB Output is correct
11 Correct 9 ms 2748 KB Output is correct
12 Correct 723 ms 15280 KB Output is correct
13 Correct 734 ms 22212 KB Output is correct
14 Correct 657 ms 9676 KB Output is correct
15 Correct 751 ms 9300 KB Output is correct
16 Correct 684 ms 9464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 12 ms 2820 KB Output is correct
8 Correct 9 ms 2868 KB Output is correct
9 Correct 8 ms 2636 KB Output is correct
10 Correct 9 ms 2636 KB Output is correct
11 Correct 9 ms 2748 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 8 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 3 ms 2688 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 8 ms 2764 KB Output is correct
22 Correct 8 ms 2764 KB Output is correct
23 Correct 9 ms 2636 KB Output is correct
24 Correct 8 ms 2636 KB Output is correct
25 Correct 8 ms 2636 KB Output is correct
26 Correct 8 ms 2764 KB Output is correct
27 Correct 8 ms 2764 KB Output is correct
28 Correct 7 ms 2636 KB Output is correct
29 Correct 9 ms 2636 KB Output is correct
30 Correct 8 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 3 ms 2636 KB Output is correct
7 Correct 12 ms 2820 KB Output is correct
8 Correct 9 ms 2868 KB Output is correct
9 Correct 8 ms 2636 KB Output is correct
10 Correct 9 ms 2636 KB Output is correct
11 Correct 9 ms 2748 KB Output is correct
12 Correct 723 ms 15280 KB Output is correct
13 Correct 734 ms 22212 KB Output is correct
14 Correct 657 ms 9676 KB Output is correct
15 Correct 751 ms 9300 KB Output is correct
16 Correct 684 ms 9464 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 8 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2636 KB Output is correct
22 Correct 2 ms 2636 KB Output is correct
23 Correct 2 ms 2636 KB Output is correct
24 Correct 3 ms 2688 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 8 ms 2764 KB Output is correct
27 Correct 8 ms 2764 KB Output is correct
28 Correct 9 ms 2636 KB Output is correct
29 Correct 8 ms 2636 KB Output is correct
30 Correct 8 ms 2636 KB Output is correct
31 Correct 8 ms 2764 KB Output is correct
32 Correct 8 ms 2764 KB Output is correct
33 Correct 7 ms 2636 KB Output is correct
34 Correct 9 ms 2636 KB Output is correct
35 Correct 8 ms 2636 KB Output is correct
36 Correct 687 ms 15332 KB Output is correct
37 Correct 694 ms 22228 KB Output is correct
38 Correct 656 ms 9680 KB Output is correct
39 Correct 753 ms 9364 KB Output is correct
40 Correct 725 ms 9476 KB Output is correct
41 Correct 751 ms 18800 KB Output is correct
42 Correct 689 ms 20460 KB Output is correct
43 Correct 655 ms 8860 KB Output is correct
44 Correct 686 ms 9400 KB Output is correct
45 Correct 687 ms 9536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 8 ms 2748 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 3 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 3 ms 2636 KB Output is correct
14 Correct 12 ms 2820 KB Output is correct
15 Correct 9 ms 2868 KB Output is correct
16 Correct 8 ms 2636 KB Output is correct
17 Correct 9 ms 2636 KB Output is correct
18 Correct 9 ms 2748 KB Output is correct
19 Correct 723 ms 15280 KB Output is correct
20 Correct 734 ms 22212 KB Output is correct
21 Correct 657 ms 9676 KB Output is correct
22 Correct 751 ms 9300 KB Output is correct
23 Correct 684 ms 9464 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 8 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2636 KB Output is correct
29 Correct 2 ms 2636 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 3 ms 2688 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 8 ms 2764 KB Output is correct
34 Correct 8 ms 2764 KB Output is correct
35 Correct 9 ms 2636 KB Output is correct
36 Correct 8 ms 2636 KB Output is correct
37 Correct 8 ms 2636 KB Output is correct
38 Correct 8 ms 2764 KB Output is correct
39 Correct 8 ms 2764 KB Output is correct
40 Correct 7 ms 2636 KB Output is correct
41 Correct 9 ms 2636 KB Output is correct
42 Correct 8 ms 2636 KB Output is correct
43 Correct 687 ms 15332 KB Output is correct
44 Correct 694 ms 22228 KB Output is correct
45 Correct 656 ms 9680 KB Output is correct
46 Correct 753 ms 9364 KB Output is correct
47 Correct 725 ms 9476 KB Output is correct
48 Correct 751 ms 18800 KB Output is correct
49 Correct 689 ms 20460 KB Output is correct
50 Correct 655 ms 8860 KB Output is correct
51 Correct 686 ms 9400 KB Output is correct
52 Correct 687 ms 9536 KB Output is correct
53 Correct 731 ms 23324 KB Output is correct
54 Correct 714 ms 20748 KB Output is correct
55 Correct 603 ms 9096 KB Output is correct
56 Correct 718 ms 16224 KB Output is correct
57 Correct 746 ms 10676 KB Output is correct
58 Correct 689 ms 10608 KB Output is correct
59 Correct 695 ms 10524 KB Output is correct
60 Correct 785 ms 10664 KB Output is correct