답안 #588414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
588414 2022-07-03T09:13:04 Z 79brue Star Trek (CEOI20_startrek) C++17
50 / 100
33 ms 8284 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll MOD = 1000000007;

struct Matrix{
    int n, m;
    ll mat[2][2];
    Matrix(){}
    Matrix(int n, int m): n(n), m(m){
        for(int i=0; i<n; i++) for(int j=0; j<m; j++) mat[i][j] = 0;
    }

    Matrix operator*(const Matrix &r)const{
        assert(m == r.n);
        Matrix ret (n, r.m);
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                for(int k=0; k<r.m; k++){
                    ret.mat[i][k] += mat[i][j] * r.mat[j][k];
                    ret.mat[i][k] %= MOD;
                }
            }
        }
        return ret;
    }
};

ll mpow(ll x, ll y){
    if(!y) return 1;
    if(y%2) return mpow(x, y-1) * x % MOD;
    ll tmp = mpow(x, y/2);
    return tmp*tmp%MOD;
}

Matrix BasicMatrix(int n, int m){
    Matrix mat (n, m);
    assert(n==m);
    for(int i=0; i<n; i++) mat.mat[i][i] = 1;
    return mat;
}

Matrix mpow(Matrix x, ll y){
    if(!y) return BasicMatrix(x.n, x.m);
    if(y&1) return mpow(x, y-1) * x;
    Matrix tmp = mpow(x, y/2);
    return tmp * tmp;
}

ll n;
ll k;
vector<int> link[1002];
ll mat[1002][1002];
ll depth[1002];
ll group[1002], groupCnt[2];
ll ans;

ll DP[2][1002];
ll find1[1002];

void groupDfs(int x, int p=-1){
    groupCnt[group[x]]++;
    for(auto y: link[x]){
        if(y==p) continue;
        group[y] = !group[x];
        groupDfs(y, x);
    }
}

void dfs(int x, int p, int r){
    for(auto y: link[x]){
        if(y==p) continue;
        depth[y] = depth[x] + 1;
        dfs(y, x, r);
        if(!mat[r][y]) mat[r][x] = 1;
    }
}

int dfsFind(int x, int p, int dp, int r){ /// 무조건 지나야 하는 상대 차례 점 개수 찾기
    if(dp % 2 == 0){ /// my turn
        int cnt = 0;
        for(auto y: link[x]){
            if(y!=p && !mat[x][y]) cnt++;
        }
        if(cnt > 1) return 0;
        assert(cnt == 1);
        for(auto y: link[x]){
            if(y!=p && !mat[x][y]) return dfsFind(y, x, dp+1, r);
        }
        exit(1);
    }
    else{ /// your turn
        int ret = 1;
        for(auto y: link[x]){
            if(y==p) continue;
            ret += dfsFind(y, x, dp+1, r);
        }
        return ret;
    }
}

int main(){
    scanf("%lld %lld", &n, &k);
    for(int i=1; i<n; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        link[x].push_back(y);
        link[y].push_back(x);
    }

    groupDfs(1);
    for(int i=1; i<=n; i++){
        dfs(i, -1, i);
    }
    for(int i=1; i<=n; i++){
        if(mat[i][i]) find1[i] = groupCnt[!group[i]] - dfsFind(i, -1, 0, i);
        else find1[i] = dfsFind(i, -1, 1, i);
        DP[0][i] = mat[i][i];
    }

    Matrix first (1, 2);
    first.mat[0][0] = first.mat[0][1] = 0;
    for(int i=1; i<=n; i++){
        if(mat[i][i]) first.mat[0][0]++;
        else first.mat[0][1]++;
    }

    Matrix multiplier (2, 2); /// 0: WIN, 1: LOSE
    for(int i=1; i<=n; i++){
        if(mat[i][i]){
            multiplier.mat[0][0] = (multiplier.mat[0][0] + n) % MOD;
            multiplier.mat[1][0] = (multiplier.mat[1][0] + groupCnt[group[i]]) % MOD;
            multiplier.mat[1][0] = (multiplier.mat[1][0] + find1[i]) % MOD;

            multiplier.mat[1][1] = (multiplier.mat[1][1] + n - groupCnt[group[i]] - find1[i] + MOD)%MOD;
        }
        else{
            multiplier.mat[1][0] = (multiplier.mat[1][0] + find1[i]) % MOD;

            multiplier.mat[0][1] = (multiplier.mat[0][1] + n) % MOD;
            multiplier.mat[1][1] = (multiplier.mat[1][1] + n - find1[i]) % MOD;
        }
    }
    multiplier = mpow(multiplier, k-1);
    first = first * multiplier;
    ll Wsum = first.mat[0][0], Lsum = first.mat[0][1];
    ll ans = 0;
    if(mat[1][1]){
        ans = (Wsum * n + groupCnt[group[1]] * Lsum + find1[1] * Lsum) % MOD;
    }
    else ans = find1[1] * Lsum % MOD;

    printf("%lld", ans);
}

Compilation message

startrek.cpp: In function 'int main()':
startrek.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |     scanf("%lld %lld", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
startrek.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 17 ms 8112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 23 ms 8276 KB Output is correct
8 Correct 31 ms 8268 KB Output is correct
9 Correct 18 ms 8184 KB Output is correct
10 Correct 18 ms 8272 KB Output is correct
11 Correct 25 ms 8200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 23 ms 8276 KB Output is correct
8 Correct 31 ms 8268 KB Output is correct
9 Correct 18 ms 8184 KB Output is correct
10 Correct 18 ms 8272 KB Output is correct
11 Correct 25 ms 8200 KB Output is correct
12 Runtime error 1 ms 412 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 23 ms 8276 KB Output is correct
8 Correct 31 ms 8268 KB Output is correct
9 Correct 18 ms 8184 KB Output is correct
10 Correct 18 ms 8272 KB Output is correct
11 Correct 25 ms 8200 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 16 ms 8196 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 23 ms 8272 KB Output is correct
22 Correct 33 ms 8284 KB Output is correct
23 Correct 21 ms 8148 KB Output is correct
24 Correct 21 ms 8128 KB Output is correct
25 Correct 23 ms 8156 KB Output is correct
26 Correct 19 ms 8236 KB Output is correct
27 Correct 29 ms 8204 KB Output is correct
28 Correct 12 ms 6612 KB Output is correct
29 Correct 19 ms 8164 KB Output is correct
30 Correct 22 ms 8240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 23 ms 8276 KB Output is correct
8 Correct 31 ms 8268 KB Output is correct
9 Correct 18 ms 8184 KB Output is correct
10 Correct 18 ms 8272 KB Output is correct
11 Correct 25 ms 8200 KB Output is correct
12 Runtime error 1 ms 412 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 17 ms 8112 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 23 ms 8276 KB Output is correct
15 Correct 31 ms 8268 KB Output is correct
16 Correct 18 ms 8184 KB Output is correct
17 Correct 18 ms 8272 KB Output is correct
18 Correct 25 ms 8200 KB Output is correct
19 Runtime error 1 ms 412 KB Execution killed with signal 11
20 Halted 0 ms 0 KB -