답안 #963128

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963128 2024-04-14T14:40:47 Z anton Star Trek (CEOI20_startrek) C++17
0 / 100
18 ms 2264 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long

const int MAX_N = 1000;
const int mod = 1e9+7;


vector<int> adj[MAX_N];
int dpth[MAX_N];
bool can_win[MAX_N][MAX_N]; 
bool can_avoid[MAX_N][MAX_N];

void find_d(int u, int a, int d){
    dpth[u] =d;
    for(auto v: adj[u]){
        if(v!=a){
            find_d(v, u, d+1);
        }
    }
}
void find_win(int f, int u,int p, int a){
    for(auto v: adj[u]){
        if( v!=a){
            find_win(f, v, (p+1)%2, u);
        }
    }
    
    if(p == 0){
        can_win[f][u] = false;
        for(auto v: adj[u]){
            if( v!=a){
                can_win[f][u] |= can_win[f][v];
            }
        }
    }
    else{
        can_win[f][u] = true;
        for(auto v: adj[u]){
            if(v!=a){
                can_win[f][u] &= can_win[f][v];
            }
        }
    }
}

void avoid(int f, int p, int u, int a){
    for(auto v: adj[u]){
        if( v!=a){
            avoid(f, (p+1)%2,v, u);
        }
    }
    
    if(p == 0){
        can_avoid[f][u] = false;
        for(auto v: adj[u]){
            if(v!=a){
                can_avoid[f][u] |= can_avoid[f][v];
            }
        }
    }
    else{
        can_avoid[f][u] = (u!=f);
        for(auto v: adj[u]){
            if(v!=a){
                can_avoid[f][u] &= can_avoid[f][v];
            }
            if(v==f){
                can_avoid[f][u] = false;
            }
        }
    }
}





signed main(){
    int n, d;
    cin>>n>>d;
    for(int i = 0; i<n-1;i++){
        int a, b;
        cin>>a>>b;

        adj[a-1].push_back(b-1);
        adj[b-1].push_back(a-1);
    }

    find_d(0, -1, 0);

    for(int i = 0; i<n; i++){
        avoid(i, 0, 0, -1);
    }

    for(int i = 0; i<n; i++){
        find_win(i, i, 0, -1);
    }

    int res= 0;

    int go = 0;

    for(int i = 0; i<n; i++){
        //cout<<i<<" "<<can_win[i][i]<<endl;
        if(can_win[i][i]){
            go ++;
        }
        
    }

    for(int i = 0; i<n; i++){
        if(can_avoid[i][0]){
            //cout<<"full "<<i<<endl;
            res = (res+n)%mod;
        }
        else{
            if(dpth[i]%2 == 0){
                //cout<<"we lead "<<i<<" "<<n-go<<endl;
                res= (res + n-go)%mod;
            }
            else{
                if(can_win[0][i]){
                    //cout<<"no choice but "<<i<<" "<<go<<endl;
                    res =(res +go)%mod;
                }
            }
        }
    }
    cout<<res<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 18 ms 2264 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 18 ms 2264 KB Output isn't correct
3 Halted 0 ms 0 KB -