답안 #789391

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789391 2023-07-21T10:13:02 Z JakobZorz Star Trek (CEOI20_startrek) C++14
100 / 100
75 ms 22576 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <stack>
#include <limits.h>
#include <math.h>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <cstring>
#include <sstream>
 
#pragma GCC target("popcnt")
 
typedef long long ll;
typedef long double ld;
using namespace std;
const int MOD=1e9+7;
typedef pair<ll,ll>point;
//#define x first
//#define y second

ll powm(ll base,ll ex){
    base%=MOD;
    ex%=MOD-1;
    ll res=1;
    for(int p=0;p<64;p++){
        if(ex&(1LL<<p))
            res*=base;
        res%=MOD;
        base*=base;
        base%=MOD;
    }
    return res;
}

ll inv(ll n){
    return powm(n,MOD-2);
}


ll n;
ll d;
vector<int> nodes[100000];
int parents[100000];

int to(int a,int b){
    if(a==b)
        return a+200000;
    if(parents[a]==b)
        return a;
    return b+100000;
}

void init_parents(int node,int par){
    parents[node]=par;
    for(int ne:nodes[node]){
        if(ne==par)
            continue;
        init_parents(ne,node);
    }
}

bool dpt1[300000];
bool dp1[300000];

bool winning(int node,int par);

bool dpt3[100000];
int dp3[100000];

int get1(int node){
    if(dpt3[node])
        return dp3[node];
    
    int num=0;
    for(int ne:nodes[node]){
        if(ne==parents[node])
            continue;
        num+=!winning(ne,node);
    }
    dpt3[node]=true;
    dp3[node]=num;
    return num;
}

bool winning(int node,int par){
    int t=to(node,par);
    if(dpt1[t])
        return dp1[t];
    
    int num=get1(node);
    if(par!=parents[node]){
        if(node!=0)
            num+=!winning(parents[node],node);
        if(node!=par)
            num-=!winning(par,node);
    }
    
    bool res=num>0;
    dpt1[t]=true;
    dp1[t]=res;
    return res;
}

bool dpt2[300000];
int dp2[300000];

int count_critical(int node,int par);

bool dpt4[300000];
pair<int,pair<int,int>> dp4[300000];

pair<int,pair<int,int>> get2(int node){
    if(dpt4[node])
        return dp4[node];
    
    int l_count=0;
    int l_ch1=-1;
    int l_ch2=-1;
    
    for(int ne:nodes[node]){
        if(!winning(ne,node)){
            l_count++;
            if(l_ch1==-1)
                l_ch1=ne;
            else
                l_ch2=ne;
        }
    }
    
    dpt4[node]=false;
    dp4[node]={l_count,{l_ch1,l_ch2}};
    return{l_count,{l_ch1,l_ch2}};
}

bool dpt5[300000];
int dp5[300000];

int get3(int node){
    if(dpt5[node])
        return dp5[node];
    int res=1;
    for(int ne:nodes[node]){
        if(ne==parents[node])
            continue;
        res+=count_critical(ne,node);
    }
    dpt5[node]=true;
    dp5[node]=res;
    return res;
}

int count_critical(int node,int par){
    int t=to(node,par);
    if(dpt2[t])
        return dp2[t];
    
    int r=0;
    if(winning(node,par)){
        auto res=get2(node);
        
        int l_count=res.first;
        int l_ch1=res.second.first;
        int l_ch2=res.second.second;
        
        if(l_ch2==par)
            l_count--;
        
        if(l_ch1==par){
            l_count--;
            l_ch1=l_ch2;
        }
        
        if(l_count!=1)
            return 0;
        r=count_critical(l_ch1,node);
    }else{
        r=get3(node);
        if(par!=parents[node]){
            if(node!=0)
                r+=count_critical(parents[node],node);
            if(node!=par)
                r-=count_critical(par,node);
        }
    }
    dpt2[t]=true;
    dp2[t]=r;
    return r;
}

int main(){
    ios::sync_with_stdio(false);
    cout.tie(NULL);
    cin.tie(NULL);
    
    //freopen("speeding.in","r",stdin);
    //freopen("speeding.out","w",stdout);
    
    cin>>n>>d;
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        nodes[a].push_back(b);
        nodes[b].push_back(a);
    }
    init_parents(0,0);
    
    ll num_l=0;
    for(int i=0;i<n;i++)
        num_l+=!winning(i,i);
    
    ll E=0;
    for(int i=0;i<n;i++){
        ll res=count_critical(i,i);
        if(winning(i,i))
            E+=res;
        else
            E+=MOD-res;
        E%=MOD;
    }
    
    ll L=1;
    
    L*=powm(n,2*d)+MOD-powm(E,d);
    L%=MOD;
    
    L*=inv(n*n+MOD-E);
    L%=MOD;
    
    L*=num_l;
    L%=MOD;
    
    ll all_combs=powm(n,2*d);
    
    ll res=L*count_critical(0,0);
    res%=MOD;
    if(winning(0,0))
        res=all_combs+MOD-res;
    res%=MOD;
    cout<<res<<"\n";
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2680 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2680 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2680 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 58 ms 16592 KB Output is correct
13 Correct 75 ms 22512 KB Output is correct
14 Correct 39 ms 11592 KB Output is correct
15 Correct 56 ms 11624 KB Output is correct
16 Correct 69 ms 11664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2680 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 1 ms 2676 KB Output is correct
13 Correct 2 ms 2816 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2680 KB Output is correct
16 Correct 1 ms 2676 KB Output is correct
17 Correct 1 ms 2680 KB Output is correct
18 Correct 1 ms 2772 KB Output is correct
19 Correct 1 ms 2680 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2900 KB Output is correct
22 Correct 2 ms 2900 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2900 KB Output is correct
28 Correct 2 ms 2812 KB Output is correct
29 Correct 2 ms 2812 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2680 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 2680 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 58 ms 16592 KB Output is correct
13 Correct 75 ms 22512 KB Output is correct
14 Correct 39 ms 11592 KB Output is correct
15 Correct 56 ms 11624 KB Output is correct
16 Correct 69 ms 11664 KB Output is correct
17 Correct 1 ms 2676 KB Output is correct
18 Correct 2 ms 2816 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 1 ms 2676 KB Output is correct
22 Correct 1 ms 2680 KB Output is correct
23 Correct 1 ms 2772 KB Output is correct
24 Correct 1 ms 2680 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 2 ms 2900 KB Output is correct
27 Correct 2 ms 2900 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2772 KB Output is correct
32 Correct 2 ms 2900 KB Output is correct
33 Correct 2 ms 2812 KB Output is correct
34 Correct 2 ms 2812 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 67 ms 16644 KB Output is correct
37 Correct 65 ms 22496 KB Output is correct
38 Correct 38 ms 11656 KB Output is correct
39 Correct 59 ms 11596 KB Output is correct
40 Correct 50 ms 11644 KB Output is correct
41 Correct 60 ms 19692 KB Output is correct
42 Correct 58 ms 20892 KB Output is correct
43 Correct 34 ms 10604 KB Output is correct
44 Correct 54 ms 11628 KB Output is correct
45 Correct 50 ms 11616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2680 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 3 ms 2680 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 58 ms 16592 KB Output is correct
20 Correct 75 ms 22512 KB Output is correct
21 Correct 39 ms 11592 KB Output is correct
22 Correct 56 ms 11624 KB Output is correct
23 Correct 69 ms 11664 KB Output is correct
24 Correct 1 ms 2676 KB Output is correct
25 Correct 2 ms 2816 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 2 ms 2680 KB Output is correct
28 Correct 1 ms 2676 KB Output is correct
29 Correct 1 ms 2680 KB Output is correct
30 Correct 1 ms 2772 KB Output is correct
31 Correct 1 ms 2680 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2900 KB Output is correct
34 Correct 2 ms 2900 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2900 KB Output is correct
40 Correct 2 ms 2812 KB Output is correct
41 Correct 2 ms 2812 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 67 ms 16644 KB Output is correct
44 Correct 65 ms 22496 KB Output is correct
45 Correct 38 ms 11656 KB Output is correct
46 Correct 59 ms 11596 KB Output is correct
47 Correct 50 ms 11644 KB Output is correct
48 Correct 60 ms 19692 KB Output is correct
49 Correct 58 ms 20892 KB Output is correct
50 Correct 34 ms 10604 KB Output is correct
51 Correct 54 ms 11628 KB Output is correct
52 Correct 50 ms 11616 KB Output is correct
53 Correct 75 ms 22576 KB Output is correct
54 Correct 74 ms 20316 KB Output is correct
55 Correct 42 ms 9936 KB Output is correct
56 Correct 55 ms 16608 KB Output is correct
57 Correct 52 ms 11824 KB Output is correct
58 Correct 53 ms 11736 KB Output is correct
59 Correct 58 ms 11716 KB Output is correct
60 Correct 57 ms 11608 KB Output is correct