Submission #89826

#TimeUsernameProblemLanguageResultExecution timeMemory
89826tpoppoZamjena (COCI18_zamjena)C++14
70 / 70
566 ms13664 KiB
#include <bits/stdc++.h>

using namespace std;
using ll= long long;

void NE(){
    cout<<"NE";
    exit(0);
}

void DA(){
    cout<<"DA";
    exit(0);
}

vector<string> a,b;
map<string,string> parent;
map<string,int> value;

string find(string a){
    if(parent[a] == a) return a;
    return parent[a] = find(parent[a]);
}

void merge(string a,string b){
    a = find(a);
    b = find(b);
    
    if(value[a] == -1e9 || value[b] == -1e9 || value[a] == value[b]){
        parent[b] = a;
        value[a] = max(value[a],value[b]);
    }else{
        NE();
    }
}


int main() {
    ll n;
    ll sn = 0;
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    cin>>n;
    a.resize(n);
    b.resize(n);
    for(auto&el:a){
        cin>>el;
        if(!isdigit(el[0])){
            value[el] = -1e9;
            parent[el] = el;
        }
    }

    for(auto&el:b){
        cin>>el;
        if(!isdigit(el[0])){
            value[el] = -1e9;
            parent[el] = el;        
        }
    }

    for(int i=0;i<n;i++){

        if(isdigit(a[i][0]) && isdigit(b[i][0])){
            if(a[i] != b[i]){
                NE();
            }
        }else if(isdigit(a[i][0]) || isdigit(b[i][0])){
            if(isdigit(a[i][0])) swap(a[i],b[i]);

            int c = stoi(b[i]);
            a[i] = find(a[i]);

            if(value[a[i]] != c && value[a[i]] != -1e9){
                NE();
            }
            value[a[i]] = c;
        }else{
            merge(a[i],b[i]);
        }
    }

    DA();
}

Compilation message (stderr)

zamjena.cpp: In function 'int main()':
zamjena.cpp:40:8: warning: unused variable 'sn' [-Wunused-variable]
     ll sn = 0;
        ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...