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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define num(a) a-'0'
#define charr(a) (a - 'A' + 1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define maxs(a,b) if(b>a)a=b
#define mins(a,b) if(b<a)a=b
#define bin1(a) __builtin_popcount(a)
#define debug(x) cerr<<"["<<#x<<":"<<x<<"] "
#define debug2(a,b) debug(a);debug(b)
#define debug3(a,b,c) debug2(a,b);debug(c)
#define de cerr
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (((a)/(__gcd(a,b))) * b)
#define print(arr) for(auto it = arr.begin();it < arr.end();it ++){cout << *it << " ";}cout << ln;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define v vector
#define p pair
#define pii p<int,int>
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define ln "\n"
typedef long double ld;
typedef double d;
using namespace std;
using namespace __gnu_pbds;
ll modF=1e9+7;
/*OUTPUT
*/
bool isNum(char ch){
if(ch >= '0' and ch <= '9'){
return true;
}
return false;
}
struct DSU{
public:
map<string,string> pr;
map<string,int> size;
DSU(set<string> s){
for(auto e:s){
pr[e] = e;
size[e] = 1;
}
}
string root(string x){
if(pr[x] == x){
return x;
}
return pr[x] = root(pr[x]);
}
void unite(string a,string b){
string ra = root(a),rb = root(b);
if(ra == rb)return;
if(size[ra] > size[rb]){
pr[rb] = ra;
size[ra] += size[rb];
}
else{
pr[ra] = rb;
size[rb] += size[ra];
}
}
};
void solve(){
int n;
cin >> n;
v<string> a(n);
v<string> b(n);
set<string> build;
rep(i,0,n){
cin >> a[i];
if(!isNum(a[i][0])){
build.insert(a[i]);
}
}
rep(i,0,n){
cin >> b[i];
if(!isNum(b[i][0])){
build.insert(b[i]);
}
}
DSU d(build);
map<string,string> mp;
// map<string,string> rp;
// for(auto e:d.pr){
// cout << e.f << " " << e.s << ln;
// }
for(int i = 0;i < n;i ++){
// if(rp.find(a[i]) != rp.end()){
// a[i] = rp[a[i]];
// }
// if(rp.find(b[i]) != rp.end()){
// b[i] = rp[b[i]];
// }
// debug(i);
if(!isNum(a[i][0])){
a[i] = d.root(a[i]);
}
if(!isNum(b[i][0])){
b[i] = d.root(b[i]);
}
// cout << a[i] << " " << b[i] << " ";
// if(mp.find(a[i]) != mp.end()){
// debug(mp[a[i]]);
// }
// if(mp.find(b[i]) != mp.end()){
// debug(mp[b[i]]);
// }
// cout << ln;
if(isNum(a[i][0]) and isNum(b[i][0])){
if(a[i] != b[i]){
cout << "NE\n";
return;
}
}
else if(isNum(a[i][0]) and !isNum(b[i][0])){
if(mp.find(b[i]) == mp.end()){
mp[b[i]] = a[i];
}
else{
if(mp[b[i]] != a[i]){
cout << "NE\n";
return;
}
}
}
else if(!isNum(a[i][0]) and isNum(b[i][0])){
if(mp.find(a[i]) == mp.end()){
mp[a[i]] = b[i];
}
else{
if(mp[a[i]] != b[i]){
cout << "NE\n";
return;
}
}
}
else{
if(mp.find(a[i]) != mp.end() and mp.find(b[i]) != mp.end()){
if(mp[a[i]] != mp[b[i]]){
cout << "NE\n";
return;
}
}
else if(mp.find(b[i]) != mp.end()){
mp[a[i]] = mp[b[i]];
}
else if(mp.find(a[i]) != mp.end()){
mp[b[i]] = mp[a[i]];
}
else{
d.unite(a[i],b[i]);
}
}
}
cout << "DA\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int t = 1;
// cin >> t;
while(t --){
solve();
}
}
# | 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... |