답안 #798378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
798378 2023-07-30T16:20:31 Z AndiR Zamjena (COCI18_zamjena) C++14
70 / 70
32 ms 8420 KB
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
const int Nmax=50000;

int n, ind, sol[Nmax*2];
string v[Nmax];
unordered_map <long long, int> m;
vector <int> ad[Nmax*2];
bool ok=1;

void dfs (int nod, int val){
    if (sol[nod]==0){
        sol[nod]=val;
        for (int i=0; i<ad[nod].size(); i++)
            dfs(ad[nod][i], val);
    }
    else if (sol[nod]!=val)
        ok=0;
}
void eq (string &s, int val){
    long long h=0;
    for (int i=0; i<s.size(); i++)
        h=h*26+s[i]-'a'+1;
    if (m.count(h)==0)
        m[h]=ind++;
    dfs(m[h], val+1);
}
void check (string &s1, string &s2){
    long long h1=0, h2=0;
    for (int i=0; i<s1.size(); i++)
        h1=h1*26+s1[i]-'a'+1;
    for (int i=0; i<s2.size(); i++)
        h2=h2*26+s2[i]-'a'+1;
    if (m.count(h1)==0)
        m[h1]=ind++;
    if (m.count(h2)==0)
        m[h2]=ind++;
    if (sol[m[h1]]!=0 && sol[m[h2]]!=0 && sol[m[h1]]!=sol[m[h2]])
        ok=0;
    else if (sol[m[h1]]==0 && sol[m[h2]]!=0)
        dfs(m[h1], sol[m[h2]]);
    else if (sol[m[h2]]==0 && sol[m[h1]]!=0)
        dfs(m[h2], sol[m[h1]]);
    ad[m[h1]].push_back(m[h2]);
    ad[m[h2]].push_back(m[h1]);
}
int main()
{
    cin>>n;
    for (int i=0; i<n; i++)
        cin>>v[i];
    string s;
    for (int i=0; i<n && ok; i++){
        cin>>s;
        int v1=-1, v2=-1;
        if (v[i][0]<='9'){
            v1=0;
            for (int j=0; j<v[i].size(); j++)
                v1=v1*10+v[i][j]-'0';
        }
        if (s[0]<='9'){
            v2=0;
            for (int j=0; j<s.size(); j++)
                v2=v2*10+s[j]-'0';
        }
        if (v1!=-1 && v2!=-1 && v1!=v2)
            ok=0;
        else if (v1!=v2){
            if (v1==-1)
                eq(v[i], v2);
            else eq(s, v1);
        }
        else if (v1==-1)
            check(v[i], s);
    }
    if (ok)
        cout<<"DA";
    else cout<<"NE";
    return 0;
}

Compilation message

zamjena.cpp: In function 'void dfs(int, int)':
zamjena.cpp:17:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |         for (int i=0; i<ad[nod].size(); i++)
      |                       ~^~~~~~~~~~~~~~~
zamjena.cpp: In function 'void eq(std::string&, int)':
zamjena.cpp:25:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for (int i=0; i<s.size(); i++)
      |                   ~^~~~~~~~~
zamjena.cpp: In function 'void check(std::string&, std::string&)':
zamjena.cpp:33:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i=0; i<s1.size(); i++)
      |                   ~^~~~~~~~~~
zamjena.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i=0; i<s2.size(); i++)
      |                   ~^~~~~~~~~~
zamjena.cpp: In function 'int main()':
zamjena.cpp:61:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |             for (int j=0; j<v[i].size(); j++)
      |                           ~^~~~~~~~~~~~
zamjena.cpp:66:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |             for (int j=0; j<s.size(); j++)
      |                           ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4212 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4180 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 2 ms 4224 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 2 ms 4180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4180 KB Output is correct
2 Correct 2 ms 4224 KB Output is correct
3 Correct 3 ms 4364 KB Output is correct
4 Correct 3 ms 4436 KB Output is correct
5 Correct 3 ms 4308 KB Output is correct
6 Correct 4 ms 4384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4692 KB Output is correct
2 Correct 11 ms 5332 KB Output is correct
3 Correct 17 ms 6356 KB Output is correct
4 Correct 15 ms 6284 KB Output is correct
5 Correct 32 ms 8420 KB Output is correct
6 Correct 26 ms 6364 KB Output is correct