답안 #600055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
600055 2022-07-20T12:16:45 Z jasmin Star Trek (CEOI20_startrek) C++14
23 / 100
1000 ms 14824 KB
#include<bits/stdc++.h>
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);
    ans[0]=a.a*b[0]+a.b*b[1];
    ans[1]=a.c*b[0]+a.d*b[1];
    return ans;
}
matrix multiply(matrix a, matrix b){
    matrix ans;
    ans.a=a.a*b.a + a.b*b.c;
    ans.b=a.a*b.b + a.b*b.d;
    ans.c=a.c*b.a + a.d*b.c;
    ans.c=a.c*b.b + a.d+b.d;
    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;
}

vector<int> win_down;
vector<int> redchild;
bool dfs_win(int v, int p, vector<vector<int> >& adi){
    for(auto u: adi[v]){
        if(u==p) continue;

        if(!dfs_win(u, v, adi)){
            redchild[v]=u;
            win_down[v]++;
        }
    }
    return win_down[v]>0;
}
vector<bool> win_up;
int violet=0; //=n-c
void dfs_win2(int v, int p, vector<vector<int> >& adi){
    if(p==-1){
        win_up[v]=false;
    }
    else if(win_down[v]){
        if(win_down[p]==0 && !win_up[p]){
            win_up[v]=true;
        }
        else{
            win_up[v]=false;
        }
    }
    else if(win_down[v]==0){
        if(win_down[p]==1 && !win_up[p]){
            win_up[v]=true;
        }
        else{
            win_up[v]=false;
        }
    }
    if(!win_up[v] && !win_down[v]){
        violet++;
    } 

    for(auto u: adi[v]){
        if(u!=p) dfs_win2(u, v, adi);
    }
}

int redforce=0;
void dfs(int v, int p, vector<vector<int> >& adi){

    if(win_down[v]==0){
        redforce++;
        for(auto u: adi[v]){
            if(u!=p) dfs(u, v, adi);
        }
    }
    else if(win_down[v]==1){
        dfs(redchild[v], v, adi);
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    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=-1; int r1=n-1;
    for(int i=n-1; i>=0; i--){
        violet=0;
        redforce=0;
        win_up.assign(n, 0);
        redchild.assign(n, -1);
        win_down.assign(n, 0);

        bool we_win=dfs_win(i, -1, adi);
        dfs_win2(i, -1, adi);
        dfs(i, -1, adi);

        if(we_win){
            g1=n;
            r1=n-redforce;
        }
        else{
            g1=0;
            r1=redforce;
        }
        G+=g1;
        R+=r1;
    }

    /*cout << g1 << " " << r1 << "\n";
    cout << G << " " << R << " " << "\n" << n*n-G << " " << n*n-R << "\n";
    cout << n-violet << " " << violet << "\n";*/

    matrix m1={g1, r1, -1, -1};
    matrix m2={G, R, n*n-G, n*n-R};
    vector<int> ans=mul(m1, mul(power(m2, d-1), {n-violet, violet}));
    cout << ans[0] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 27 ms 324 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 35 ms 428 KB Output is correct
8 Correct 43 ms 468 KB Output is correct
9 Correct 28 ms 340 KB Output is correct
10 Correct 29 ms 324 KB Output is correct
11 Correct 35 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 35 ms 428 KB Output is correct
8 Correct 43 ms 468 KB Output is correct
9 Correct 28 ms 340 KB Output is correct
10 Correct 29 ms 324 KB Output is correct
11 Correct 35 ms 340 KB Output is correct
12 Execution timed out 1083 ms 14824 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 35 ms 428 KB Output is correct
8 Correct 43 ms 468 KB Output is correct
9 Correct 28 ms 340 KB Output is correct
10 Correct 29 ms 324 KB Output is correct
11 Correct 35 ms 340 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Incorrect 31 ms 384 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 35 ms 428 KB Output is correct
8 Correct 43 ms 468 KB Output is correct
9 Correct 28 ms 340 KB Output is correct
10 Correct 29 ms 324 KB Output is correct
11 Correct 35 ms 340 KB Output is correct
12 Execution timed out 1083 ms 14824 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 27 ms 324 KB Output isn't correct
3 Halted 0 ms 0 KB -