Submission #107186

#TimeUsernameProblemLanguageResultExecution timeMemory
107186WLZZamjena (COCI18_zamjena)C++17
70 / 70
154 ms8700 KiB
#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 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...