답안 #541363

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
541363 2022-03-23T09:05:17 Z AlperenT Magenta (COCI21_magenta) C++17
110 / 110
70 ms 8956 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, INF = 1e9 + 5;

int n, a, b, x, y, adist[N], bdist[N];

string str;

char c;

vector<pair<int, char>> tree[N];

bool isainf, isbinf;

void dfsa(int v, int p, int d){
    adist[v] = d;

    for(auto e : tree[v]){
        if(e.first != p && e.second != 'R'){
            dfsa(e.first, v, d + 1);
        }
    }
}

void dfsb(int v, int p, int d){
    bdist[v] = d;

    for(auto e : tree[v]){
        if(e.first != p && e.second != 'B'){
            dfsb(e.first, v, d + 1);
        }
    }
}

void checka(int v, int p, int d){
    for(auto e : tree[v]){
        if(e.first != p && e.second != 'R'){
            if(bdist[e.first] > d - 1 || bdist[e.first] % 2 != (d - 1) % 2){
                checka(e.first, v, d + 1);

                if((((bdist[v] % 2 != ((d + 1) - 1) % 2) || bdist[v] == INF) && ((bdist[e.first] % 2 != (d - 1) % 2) || bdist[e.first] == INF))) isainf = true;
            }
        }
    }
}

void checkb(int v, int p, int d){
    for(auto e : tree[v]){
        if(e.first != p && e.second != 'B'){
            if(adist[e.first] > d || adist[e.first] % 2 != d % 2){
                checkb(e.first, v, d + 1);

                if(((adist[v] % 2 != (d + 1) % 2 || adist[v] == INF) && (adist[e.first] % 2 != d % 2 || adist[e.first] == INF))) isbinf = true;
            }
        }
    }
}

int dist(int v, int p, int d){
    if(v == b) return d;
    else{
        int mx = -1;

        for(auto e : tree[v]){
            if(e.first != p){
                mx = max(mx, dist(e.first, v, d + 1));
            }
        }

        return mx;
    }
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    cin >> n;

    cin >> a >> b;

    for(int i = 1; i <= n - 1; i++){
        cin >> x >> y >> str;

        if(str == "plava") c = 'B';
        else if(str == "crvena") c = 'R';
        else if(str == "magenta") c = 'M';

        tree[x].push_back({y, c});
        tree[y].push_back({x, c});
    }

    fill(adist, adist + N, INF), fill(bdist, bdist + N, INF);

    dfsa(a, 0, 0), dfsb(b, 0, 0);

    checka(a, 0, 1), checkb(b, 0, 1);

    if(isainf && isbinf) cout << "Magenta";
    else if(isainf) cout << "Paula";
    else if(isbinf) cout << "Marin";
    else{
        int ans = dist(a, 0, 0);

        if(adist[b] == INF && bdist[a] == INF) cout << "Marin";
        else if(ans == -1) cout << "Marin";
        else if(ans % 2 == 0) cout << "Paula";
        else cout << "Marin";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3420 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3440 KB Output is correct
13 Correct 2 ms 3444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 7096 KB Output is correct
2 Correct 70 ms 7176 KB Output is correct
3 Correct 57 ms 7200 KB Output is correct
4 Correct 54 ms 7108 KB Output is correct
5 Correct 54 ms 7096 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 3 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3420 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 3 ms 3412 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3440 KB Output is correct
13 Correct 2 ms 3444 KB Output is correct
14 Correct 65 ms 7096 KB Output is correct
15 Correct 70 ms 7176 KB Output is correct
16 Correct 57 ms 7200 KB Output is correct
17 Correct 54 ms 7108 KB Output is correct
18 Correct 54 ms 7096 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 3 ms 3412 KB Output is correct
21 Correct 2 ms 3412 KB Output is correct
22 Correct 2 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3372 KB Output is correct
25 Correct 2 ms 3444 KB Output is correct
26 Correct 2 ms 3448 KB Output is correct
27 Correct 3 ms 3412 KB Output is correct
28 Correct 2 ms 3444 KB Output is correct
29 Correct 2 ms 3412 KB Output is correct
30 Correct 2 ms 3412 KB Output is correct
31 Correct 41 ms 8956 KB Output is correct
32 Correct 39 ms 8956 KB Output is correct
33 Correct 44 ms 8952 KB Output is correct
34 Correct 46 ms 8904 KB Output is correct