Submission #504140

# Submission time Handle Problem Language Result Execution time Memory
504140 2022-01-09T23:37:45 Z Hacv16 Kutije (COCI21_kutije) C++17
70 / 70
221 ms 9240 KB
#include<bits/stdc++.h>
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#define endl '\n'
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define bn binary_search
#define lb lower_bound
#define up upper_bound
#define np next_permutation
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define mdc(x, y) __gcd(x, y)
#define dbg(x) cout << #x << ": " << "[ " << x << " ]\n"

typedef int ii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef priority_queue<int, vector<int>, greater<int>> heap;

const int MAX = 2e6 + 15;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int VAR[4] = {-1, 1, 0, 0};

struct DSU{
    vector<int> sze, id;

    DSU(int n) : sze(n, 1), id(n){
        iota(id.begin(), id.end(), 0);
    }

    int find(int v){
        if(id[v] == v)
            return v;

        return id[v] = find(id[v]);
    }

    void join(int u, int v){
        u = find(u), v = find(v);
        if(u == v) return;
        if(sze[u] > sze[v]) swap(u, v);
        id[u] = v, sze[v] += sze[u];
    }

    bool same(int u, int v){
        return (find(u) == find(v));
    }
};

int n, m, q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    cin >> n >> m >> q;

    DSU toys(n + 1);

    while(m--){
        for(int i = 1; i <= n; i++){
            int a; cin >> a;
            toys.join(i, a);
        }
    }

    while(q--){
        int u, v; cin >> u >> v;
        cout << ((toys.same(u, v)) ? "DA" : "NE") << '\n';
    }
    
    return 0;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
    * brute force to find pattern?
    * sort?
    * graph?
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
# Verdict Execution time Memory Grader output
1 Correct 91 ms 1732 KB Output is correct
2 Correct 76 ms 4828 KB Output is correct
3 Correct 66 ms 4584 KB Output is correct
4 Correct 87 ms 5544 KB Output is correct
5 Correct 92 ms 5448 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 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 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 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 1732 KB Output is correct
2 Correct 76 ms 4828 KB Output is correct
3 Correct 66 ms 4584 KB Output is correct
4 Correct 87 ms 5544 KB Output is correct
5 Correct 92 ms 5448 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 154 ms 9112 KB Output is correct
27 Correct 161 ms 9132 KB Output is correct
28 Correct 120 ms 7464 KB Output is correct
29 Correct 148 ms 8808 KB Output is correct
30 Correct 145 ms 7804 KB Output is correct
31 Correct 122 ms 7776 KB Output is correct
32 Correct 221 ms 9108 KB Output is correct
33 Correct 164 ms 9128 KB Output is correct
34 Correct 151 ms 9240 KB Output is correct
35 Correct 161 ms 8972 KB Output is correct