# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
472621 | MamdouhN | Zamjena (COCI18_zamjena) | C++17 | 103 ms | 12796 KiB |
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 namespace std;
#define int long long
vector<int> a;
vector<int> b;
bool isNumeric(string s){
for(int i=0;i<s.size();i++){
if(s[i]<'0'||s[i]>'9')return false;
}
return true;
}
int dsu[1000000];
int findIt(int x){
if(x==dsu[x])return x;
return dsu[x] = findIt(dsu[x]);
}
void unionIt(int x,int y){
dsu[findIt(x)] = findIt(y);
}
map<string,int> variableMapper;
main()
{
for(int i=0;i<1000000;i++)dsu[i]=i;
int n;
cin>>n;
int cnt = 1;
for(int i=0;i<n;i++){
string s;
cin>>s;
if(isNumeric(s))a.push_back(stoi(s));
else{
if(variableMapper[s]==0){
variableMapper[s] = 1000+cnt;
cnt++;
}
a.push_back(variableMapper[s]);
}
}
for(int i=0;i<n;i++){
string s;
cin>>s;
if(isNumeric(s))b.push_back(stoi(s));
else{
if(variableMapper[s]==0){
variableMapper[s] = 1000+cnt;
cnt++;
}
b.push_back(variableMapper[s]);
}
}
bool failure = false;
for(int i=0;i<n;i++){
if(failure)break;
if(a[i]<=1000&&b[i]<=1000){
if(a[i]!=b[i])failure = true;
continue;
}
if(a[i]>1000&&b[i]<=1000){
if(findIt(a[i])<=1000 && findIt(a[i])!= b[i]) failure = true;
unionIt(a[i],b[i]);
continue;
}
if(a[i]<=1000&&b[i]>1000){
if(findIt(b[i])<=1000 && findIt(b[i])!= a[i]) failure = true;
unionIt(b[i],a[i]);
continue;
}
if(findIt(a[i])<=1000 && findIt(b[i])<=1000 && findIt(a[i])!=findIt(b[i]) ){
failure = true;
continue;
}
if(findIt(b[i])<=1000){
unionIt(a[i],b[i]);
continue;
}
unionIt(b[i],a[i]);
}
if(failure)cout<<"NE"<<endl;
else cout<<"DA"<<endl;
}
Compilation message (stderr)
# | 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... |