답안 #601843

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601843 2022-07-22T10:52:45 Z jasmin Star Trek (CEOI20_startrek) C++14
100 / 100
199 ms 54516 KB
#include<bits/stdc++.h>
//#include<iostream>
//#include<vector>
using namespace std;
#define int long long
 
const int mod=1e9+7;
struct matrix{
    int a=1;
    int b=0;
    int c=0;
    int d=1;
};
matrix zero={1, 0, 0, 1};
vector<int> mul(matrix a, vector<int> b){
    vector<int> ans(2, -1);
    ans[0]=(a.a*b[0])%mod +(a.b*b[1])%mod;
    ans[0]%=mod;
    ans[1]=(a.c*b[0])%mod +(a.d*b[1])%mod;
    ans[1]%=mod;
    return ans;
}
matrix multiply(matrix a, matrix b){
    matrix ans;
    ans.a=(a.a*b.a)%mod + (a.b*b.c)%mod;
    ans.a%=mod;
    ans.b=(a.a*b.b)%mod + (a.b*b.d)%mod;
    ans.b%=mod;
    ans.c=(a.c*b.a)%mod + (a.d*b.c)%mod;
    ans.c%=mod;
    ans.d=(a.c*b.b)%mod + (a.d*b.d)%mod;
    ans.d%=mod;
    return ans;
}
matrix power(matrix a, int e){
    if(e==0){
        return zero;
    }
 
    matrix res=power(a, e/2);
    res=multiply(res, res);
    if(e%2==1){
        res=multiply(res, a);
    }
    return res;
}
 
 
// vertex_status ist: 
bool dfs_win(int v, int p, vector<vector<int> >& adi, vector<int>& vertex_status, vector<vector<int>>& red_neighbors){
    if (vertex_status[v] == -2) {
      vertex_status[v] = p;
      for(auto u: adi[v]){
          if(u==p) continue;
   
          if(!dfs_win(u, v, adi, vertex_status, red_neighbors)){
              red_neighbors[v].push_back(u);
          }
      }
    } else {
      if (vertex_status[v] >= 0 && vertex_status[v] != p) {
        auto u = vertex_status[v];
        if(!dfs_win(u, v, adi, vertex_status, red_neighbors)){
            red_neighbors[v].push_back(u);
        }
        vertex_status[v] = -1;
      }
    }
 
    if (vertex_status[v] == p) {
      return red_neighbors[v].size() > 0;
    } else {
      if (red_neighbors[v].size() >= 2) return true;
      if (red_neighbors[v].size() == 0) return false;
      return red_neighbors[v][0] != p;
    }
}
 
int dfs_redforce(int v, int p, vector<vector<int> >& adi, vector<vector<int>>& red_neighbors, vector<int>& vertex_status, vector<unordered_map<int, int> >& redf, vector<int>& summe){
    bool is_green;
    if (red_neighbors[v].size() >= 2) is_green = true;
    else if (red_neighbors[v].size() == 0) is_green = false;
    else  is_green = red_neighbors[v][0] != p;

    if(vertex_status[v]==-2){
        int redforce=0;
 
        for(auto u: adi[v]){
            if(u==p) continue;
            redf[v][u]=dfs_redforce(u, v, adi, red_neighbors, vertex_status, redf, summe);
            summe[v]+=redf[v][u];
        }
 
        vertex_status[v]=p;
    }
    else{
        if(vertex_status[v]>=0 && vertex_status[v]!=p){
            redf[v][vertex_status[v]]=dfs_redforce(vertex_status[v], v, adi, red_neighbors, vertex_status, redf, summe);
            summe[v]+=redf[v][vertex_status[v]];
            vertex_status[v]=-1;
        }
    }

    int redforce = 0;
    if(!is_green){ //red
        redforce++;
        redforce+=summe[v];
        if (vertex_status[v] == -1 && p != -1) {
          redforce-=redf[v][p];
        }
    }
    else { //green with one red child
        bool needed=red_neighbors[v].size()<3;
        int u=red_neighbors[v][0];
        if(red_neighbors[v].size()==2){
            if(red_neighbors[v][0]==p) u=red_neighbors[v][1];
            else if(red_neighbors[v][1]!=p) needed=false;
        }
        if (needed) {
            redforce+=redf[v][u];
        }
    }

    return redforce;

}
 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    //freopen("input1.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
 
    int n, d;
    cin >> n >> d;
    vector<vector<int> > adi(n);
    for(int i=0; i<n-1; i++){
        int a,b;
        cin >> a >> b;
        adi[a-1].push_back(b-1);
        adi[b-1].push_back(a-1);
    }
 
    int G=0; int R=0;
    int g1=0; int r1=0;
    int c=0;
    vector<int> vertex_status(n, -2);
    vector<int> vertex_status2(n, -2);
    vector<vector<int>> red_neighbors(n);
    vector<unordered_map<int,int> > redf(n);
    vector<int> summe(n, 0);
    for(int i=n-1; i>=0; i--){
 
        bool we_win=dfs_win(i, -1, adi, vertex_status, red_neighbors);
        int redforce=dfs_redforce(i, -1, adi, red_neighbors, vertex_status2, redf, summe);
 
        if(we_win){
            c++;
            g1=n;
            r1=n-redforce;
        }
        else{
            g1=0;
            r1=redforce;
        }
        G+=g1;
        R+=r1;
        G%=mod;
        R%=mod;
    }
 
    matrix m1={g1, r1, 0, 0};
    matrix m2={G, R, ((n*n)-G)%mod, ((n*n)-R)%mod};
    matrix p=power(m2, d-1);
    vector<int> v=mul(p, {c, n-c});
    vector<int> ans=mul(m1, v);
    cout << ans[0] << "\n";
}

Compilation message

startrek.cpp: In function 'long long int dfs_redforce(long long int, long long int, std::vector<std::vector<long long int> >&, std::vector<std::vector<long long int> >&, std::vector<long long int>&, std::vector<std::unordered_map<long long int, long long int> >&, std::vector<long long int>&)':
startrek.cpp:86:13: warning: unused variable 'redforce' [-Wunused-variable]
   86 |         int redforce=0;
      |             ^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 181 ms 48084 KB Output is correct
13 Correct 188 ms 54516 KB Output is correct
14 Correct 171 ms 36736 KB Output is correct
15 Correct 179 ms 37580 KB Output is correct
16 Correct 167 ms 36532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 0 ms 316 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 2 ms 840 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 181 ms 48084 KB Output is correct
13 Correct 188 ms 54516 KB Output is correct
14 Correct 171 ms 36736 KB Output is correct
15 Correct 179 ms 37580 KB Output is correct
16 Correct 167 ms 36532 KB Output is correct
17 Correct 0 ms 316 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 576 KB Output is correct
31 Correct 1 ms 724 KB Output is correct
32 Correct 2 ms 840 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 174 ms 48148 KB Output is correct
37 Correct 199 ms 54396 KB Output is correct
38 Correct 184 ms 36712 KB Output is correct
39 Correct 181 ms 37556 KB Output is correct
40 Correct 170 ms 36504 KB Output is correct
41 Correct 168 ms 44844 KB Output is correct
42 Correct 163 ms 47552 KB Output is correct
43 Correct 141 ms 32588 KB Output is correct
44 Correct 188 ms 37232 KB Output is correct
45 Correct 170 ms 36312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 580 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 181 ms 48084 KB Output is correct
20 Correct 188 ms 54516 KB Output is correct
21 Correct 171 ms 36736 KB Output is correct
22 Correct 179 ms 37580 KB Output is correct
23 Correct 167 ms 36532 KB Output is correct
24 Correct 0 ms 316 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 1 ms 596 KB Output is correct
37 Correct 1 ms 576 KB Output is correct
38 Correct 1 ms 724 KB Output is correct
39 Correct 2 ms 840 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 1 ms 596 KB Output is correct
42 Correct 1 ms 596 KB Output is correct
43 Correct 174 ms 48148 KB Output is correct
44 Correct 199 ms 54396 KB Output is correct
45 Correct 184 ms 36712 KB Output is correct
46 Correct 181 ms 37556 KB Output is correct
47 Correct 170 ms 36504 KB Output is correct
48 Correct 168 ms 44844 KB Output is correct
49 Correct 163 ms 47552 KB Output is correct
50 Correct 141 ms 32588 KB Output is correct
51 Correct 188 ms 37232 KB Output is correct
52 Correct 170 ms 36312 KB Output is correct
53 Correct 191 ms 50380 KB Output is correct
54 Correct 177 ms 48416 KB Output is correct
55 Correct 129 ms 29172 KB Output is correct
56 Correct 162 ms 43340 KB Output is correct
57 Correct 161 ms 36940 KB Output is correct
58 Correct 164 ms 36784 KB Output is correct
59 Correct 179 ms 37584 KB Output is correct
60 Correct 175 ms 36260 KB Output is correct