Submission #464645

# Submission time Handle Problem Language Result Execution time Memory
464645 2021-08-13T14:54:40 Z Sarah_Mokhtar Zamjena (COCI18_zamjena) C++14
70 / 70
270 ms 13424 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define read freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define LESSGO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll N=1e6+10,M=505,OO=1e16,mod=1e9+7;
int n,cur,valid=1,vid;
string s;
vector<int>a,b;
map<string,int>idx;
map<int,int>val,vis;
map<int,vector<int>>adj;
set<int>st;
bool isDigit(char c){
    return c>='0'&&c<='9';
}
void dfs(int src){
    vis[src]=1;
    for(int i:adj[src]){
        if(!vis[i]){
            dfs(i);
        }
    }
    if(val[src]) st.insert(val[src]);
}
int main(){
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>s;
        if(isDigit(s[0])) a.push_back(stoi(s));
        else{
            if(idx[s]) a.push_back(idx[s]);
            else{
                idx[s]=++cur;
                idx[s]*=-1;
                a.push_back(idx[s]);
            }
        }
    }
    for(int i=0;i<n;++i){
        cin>>s;
        if(isDigit(s[0])) b.push_back(stoi(s));
        else{
            if(idx[s]) b.push_back(idx[s]);
            else{
                idx[s]=++cur;
                idx[s]*=-1;
                b.push_back(idx[s]);
            }
        }
    }
    for(int i=0;i<n;++i){
        if(a[i]>0&&b[i]>0){
            if(a[i]!=b[i]){
                valid=0;
                break;
            }
        }
        if(a[i]<0&&b[i]<0){
            adj[a[i]].push_back(b[i]);
            adj[b[i]].push_back(a[i]);
            if(!(val[a[i]]==0||val[b[i]]==0||val[a[i]]==val[b[i]])){
                
                valid=0;
                break;
            }else{
                val[a[i]]=max(val[a[i]],val[b[i]]);
                val[b[i]]=max(val[a[i]],val[b[i]]);
            }
        }
        if(a[i]>0&&b[i]<0){
            if(!(val[b[i]]==0||val[b[i]]==a[i])){
                valid=0;
                break;
            }else val[b[i]]=a[i];
        }
        if(a[i]<0&&b[i]>0){
            if(!(val[a[i]]==0||val[a[i]]==b[i])){
                valid=0;
                break;
            }else val[a[i]]=b[i];
        }
    }
    if(valid==0){
        puts("NE");
        return 0;
    }
    for(int i=0;i<n;++i){
        if(a[i]<0&&!vis[a[i]]){
            st.clear();
            dfs(a[i]);
            if(st.size()>1){
                valid=0;
                break;
            }
        }
    }
    
    for(int i=0;i<n;++i){
        if(b[i]<0&&!vis[b[i]]){
            st.clear();
            dfs(b[i]);
            if(st.size()>1){
                valid=0;
                break;
            }
        }
    }
    puts(valid?"DA":"NE");
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 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 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 9 ms 896 KB Output is correct
4 Correct 11 ms 972 KB Output is correct
5 Correct 9 ms 880 KB Output is correct
6 Correct 6 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2076 KB Output is correct
2 Correct 83 ms 4120 KB Output is correct
3 Correct 152 ms 7376 KB Output is correct
4 Correct 122 ms 6544 KB Output is correct
5 Correct 270 ms 13424 KB Output is correct
6 Correct 188 ms 6720 KB Output is correct