답안 #756580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
756580 2023-06-12T00:47:45 Z Pring Zamjena (COCI18_zamjena) C++14
70 / 70
61 ms 13884 KB
#include <bits/stdc++.h>
using namespace std;

const int MXN = 100005;
int n;
string a[MXN], b[MXN];
int na[MXN], nb[MXN];
map<string, int> M;

inline bool isNumber(string &s) {
    return ('0' <= s[0] && s[0] <= '9');
}

struct DSU {
    string s[MXN];
    int p[MXN], sz[MXN];
    void init(int _n) {
        for (int i = 0; i < _n; i++) {
            p[i] = i;
            sz[i] = 1;
        }
    }
    int find(int x) {
        return (x == p[x] ? x : p[x] = find(p[x]));
    }
    bool onion(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return true;
        if (isNumber(s[x]) && isNumber(s[y])) return false;
        // if (sz[x] < sz[y]) swap(x, y);
        if (isNumber(s[y])) swap(x, y);
        // cout << s[x] << ' ' << s[y] << endl;
        sz[x] += sz[y];
        p[y] = x;
        return true;
    }
} dsu;

void CIN(string &s, int &x) {
    cin >> s;
    auto it = M.find(s);
    if (it == M.end()) {
        dsu.s[M.size()] = s;
        x = M.size();
        M[s] = x;
    } else {
        x = it -> second;
    }
}

bool check() {
    for (int i = 0; i < n; i++) {
        if (!dsu.onion(na[i], nb[i])) return false;
    }
    return true;
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n; i++) {
        CIN(a[i], na[i]);
    }
    for (int i = 0; i < n; i++) {
        CIN(b[i], nb[i]);
    }
    // for (int i = 0; i < n; i++) cout << na[i] << ' ' << nb[i] << endl;
    dsu.init(M.size());
    cout << (check() ? "DA" : "NE") << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9704 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 8 ms 9892 KB Output is correct
4 Correct 8 ms 9940 KB Output is correct
5 Correct 8 ms 9996 KB Output is correct
6 Correct 7 ms 9908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10308 KB Output is correct
2 Correct 20 ms 11080 KB Output is correct
3 Correct 35 ms 12028 KB Output is correct
4 Correct 38 ms 12200 KB Output is correct
5 Correct 61 ms 13884 KB Output is correct
6 Correct 42 ms 11768 KB Output is correct