Submission #107186

# Submission time Handle Problem Language Result Execution time Memory
107186 2019-04-22T12:09:02 Z WLZ Zamjena (COCI18_zamjena) C++17
70 / 70
154 ms 8700 KB
#include <bits/stdc++.h>
using namespace std;

const long long MOD = 998244353;

class DSU {
  private:
    vector<int> p, rank, sz;
  public:
    DSU(int n) {
      p.assign(n, -1);
      rank.assign(n, 0);
      sz.assign(n, 0);
      for (int i = 0; i < 1000; i++) {
        sz[i] = 1;
      }
    }

    int root(int x) {
      if (p[x] < 0) {
        return x;
      }
      return (p[x] = root(p[x]));
    }

    int sameSet(int x, int y) {
      return (root(x) == root(y));
    }

    void connect(int x, int y) {
      x = root(x);
      y = root(y);
      if (x != y) {
        if (rank[x] > rank[y]) {
          p[y] = x;
          sz[x] += sz[y];
        } else {
          p[x] = y;
          if (rank[x] == rank[y]) {
            rank[y]++;
          }
          sz[y] += sz[x];
        }
      }
    }

    int setSize(int x) {
      return sz[root(x)];
    }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> a(n), b(n);
  map<string, int> mp;
  int cur = 1000;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (!isdigit(a[i][0]) && !mp.count(a[i])) {
      mp[a[i]] = cur++;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (!isdigit(b[i][0]) && !mp.count(b[i])) {
      mp[b[i]] = cur++;
    }
  }
  DSU dsu((int) 1e5);
  for (int i = 0; i < n; i++) {
    int tmpa, tmpb;
    if (isdigit(a[i][0])) {
      tmpa = stoi(a[i]);
    } else {
      tmpa = mp[a[i]];
    }
    if (isdigit(b[i][0])) {
      tmpb = stoi(b[i]);
    } else {
      tmpb = mp[b[i]];
    }
    dsu.connect(tmpa, tmpb);
  }
  for (int i = 0; i < (int) 1e5; i++) {
    if (dsu.setSize(i) > 1) {
      cout << "NE\n";
      return 0;
    }
  }
  cout << "DA\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1536 KB Output is correct
2 Correct 3 ms 1536 KB Output is correct
3 Correct 4 ms 1536 KB Output is correct
4 Correct 3 ms 1536 KB Output is correct
5 Correct 3 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1536 KB Output is correct
2 Correct 1 ms 1408 KB Output is correct
3 Correct 4 ms 1564 KB Output is correct
4 Correct 4 ms 1536 KB Output is correct
5 Correct 4 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1508 KB Output is correct
2 Correct 3 ms 1536 KB Output is correct
3 Correct 4 ms 1536 KB Output is correct
4 Correct 3 ms 1536 KB Output is correct
5 Correct 3 ms 1536 KB Output is correct
6 Correct 3 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1664 KB Output is correct
2 Correct 3 ms 1664 KB Output is correct
3 Correct 7 ms 1920 KB Output is correct
4 Correct 9 ms 1920 KB Output is correct
5 Correct 7 ms 1920 KB Output is correct
6 Correct 7 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2688 KB Output is correct
2 Correct 35 ms 3964 KB Output is correct
3 Correct 72 ms 5468 KB Output is correct
4 Correct 70 ms 6392 KB Output is correct
5 Correct 154 ms 8700 KB Output is correct
6 Correct 96 ms 6676 KB Output is correct