Submission #497673

#TimeUsernameProblemLanguageResultExecution timeMemory
497673hgmhcKutije (COCI21_kutije)C++17
70 / 70
157 ms9520 KiB
#include <bits/stdc++.h>
#define REP(i,a,b) for (auto i = (a); i <= (b); ++i)
#define debug(x) cerr << #x << " is " << x << endl
#define el '\n'
using namespace std; using ll = long long;

const int N = 1003, Q = 500003;

struct DSU {
    vector<int> par, sz;
    DSU(int n): par(n+1), sz(n+1,1) {iota(par.begin(),par.end(),0);}
    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }
    void merge(int a, int b) {
        a = find(a), b = find(b);
        if (a == b) return;
        if (sz[a] > sz[b]) swap(a,b);
        sz[b] += sz[a];
        par[a] = b;
    }
    int size(int x) {return sz[find(x)];}
    bool same(int a, int b) {return find(a) == find(b);}
};

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m, q; cin >> n >> m >> q;
    // n: the number of boxes
    // m: the number of friends
    // q: the number of questions
    
    DSU toys(n);
    
    int p[N] = {0,};
    REP(k,1,m) {
        REP(i,1,n) {
            cin >> p[i];
            // k번째 친구가 사용할
            // 장난감 도로 넣을 박스들
            toys.merge(i,p[i]);
        }
    }
    REP(i,1,q) {
        int a, b; cin >> a >> b;
        cout << (toys.same(a,b) ? "DA" : "NE") << el;
    }
    // if it's possible: DA
    // else: NE
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...