제출 #597099

#제출 시각아이디문제언어결과실행 시간메모리
597099kamelfanger83Star Trek (CEOI20_startrek)C++14
23 / 100
90 ms11188 KiB
#include <iostream>
#include <vector>

using namespace std;

#define int long long

signed main(){
    int n, d; cin >> n >> d;
    vector<vector<int>> cons (n);
    int u, v;
    for (int reader = 0; reader < n - 1; ++reader) {
        cin >> u >> v;
        u--; v--;
        cons[u].push_back(v);
        cons[v].push_back(u);
    }
    vector<int> wins (n), redchild (n);

    auto dfswins = [&](auto&& self, int i, int from) -> int{
        int red = 0, redi;
        for(int c : cons[i]){
            if(c == from) continue;
            if(self(self, c, i) == 0){
                red++;
                redi = c;
            }
        }
        if(red == 1) redchild[i] = redi;
        return wins[i] = red;
    };

    dfswins(dfswins, 0, -1);

    int redforce = 0;

    auto findredforce = [&](auto&& self, int i, int from) -> void {
        if(wins[i] == 0){
            redforce++;
            for(int c : cons[i]){
                if(c == from) continue;
                self(self, c, i);
            }
        }
        if(wins[i] == 1){
            self(self, redchild[i], i);
        }
    };

    findredforce(findredforce, 0, -1);

    vector<int> winup (n, -1);

    int violet = 0;

    auto dfs3 = [&](auto&& self, int i, int from) -> void{
        if(from == -1){
            winup[i] = false;
            if(wins[i] == false) violet++;
        }
        else if(wins[i] == 0){
            if(winup[from] == false && wins[from] == 1) winup[i] = true;
            else{
                winup[i] = false;
                violet++;
            }
        }
        else{
            if(winup[from] == false && wins[from] == 0) winup[i] = true;
            else winup[i] = false;
        }
        for(int c : cons[i]){
            if(c == from) continue;
            self(self, c, i);
        }
    };

    dfs3(dfs3, 0, -1);

    if(wins[0] > 0) cout << n*n - violet*redforce;
    else cout << violet*redforce;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...