#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
588 KB |
Output is correct |
5 |
Correct |
3 ms |
448 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
860 KB |
Output is correct |
2 |
Correct |
19 ms |
1728 KB |
Output is correct |
3 |
Correct |
34 ms |
2812 KB |
Output is correct |
4 |
Correct |
41 ms |
3128 KB |
Output is correct |
5 |
Correct |
65 ms |
4896 KB |
Output is correct |
6 |
Correct |
47 ms |
2784 KB |
Output is correct |