This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
struct union_find {
vector<int> par;
union_find(const int n) : par(n, -1) {}
int find(const int u) {
return par[u] < 0 ? u : par[u] = find(par[u]);
}
void merge(int x, int y) {
x = find(x), y = find(y);
if (x != y) par[x] = y;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
array<vector<int>, 2> arr;
arr[0] = arr[1] = vector<int>(n);
int idx = 0;
std::map<std::string, int> var;
for (auto& v : arr) {
for (auto& x : v) {
std::string s;
std::cin >> s;
if (std::isdigit(s[0])) {
x = -std::stoi(s);
} else {
if (var.find(s) == var.end()) var[s] = idx++;
x = var[s];
}
}
}
const int m = var.size();
union_find dsu(m);
for (int i = 0; i < n; ++i) {
if (arr[0][i] >= 0 and arr[1][i] >= 0) {
dsu.merge(arr[0][i], arr[1][i]);
}
}
vector<int> val(m);
for (int i = 0; i < n; ++i) {
int x = arr[0][i], y = arr[1][i];
if (x < 0 and y < 0 and x != y) {
std::cout << "NE\n";
return 0;
}
if (x < 0) std::swap(x, y);
if (x >= 0 and y < 0) {
const int u = dsu.find(x);
if (val[u] != 0 and val[u] != y) {
std::cout << "NE\n";
return 0;
}
val[u] = y;
}
}
std::cout << "DA\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |