1468M - Similar Sets - CodeForces Solution


data structures graphs implementation *2300

Please click on ads to support us..

C++ Code:

//region template
#include <bits/stdc++.h>

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...)        GET_MACRO(__VA_ARGS__, repn, rep0)(__VA_ARGS__)
#define rep0(i, b)      for(int i = 0; i < (b); ++i)
#define repn(i, a, b)   for(int i = (a); i < (b); ++i)
#define trav(a, b)      for(auto& a : b)
#define all(a)          (a).begin(),(a).end()
#define endl            '\n'
#define sz(x)           ((int)(x).size())
#define tests           int ___;cin>>___;while(___--)

using namespace std;
using ll = int64_t;
using ld = long double;
using ul = uint64_t;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vpi = vector<pi>;

static constexpr int inf = (int) 1e9 + 5;
static constexpr ll infl = (ll) 1e18 + 5;
static mt19937 rng((uint32_t) chrono::duration_cast<chrono::nanoseconds>(
        chrono::high_resolution_clock::now().time_since_epoch()).count());
//endregion

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);

    tests {
        int n;
        cin >> n;
        vvi a(n);
        map<int, int> c;
        rep(i, n) {
            int k;cin >> k;
            rep(j, k) {
                int x;cin >> x;
                if (!c.count(x))
                    c.insert({x, sz(c)});
                a[i].push_back(c[x]);
            }
            sort(all(a[i]));
        }
        const int b = 250;

        vector p(sz(c), vpi());

        rep(i,n) {
            if (sz(a[i]) < b) continue;
            vi occ(sz(c), 0);
            trav(k, a[i]) occ[k] = 1;
            rep(j,n) {
                if (i == j) continue;
                int cnt = 0;
                trav(k, a[j]) {
                    cnt += occ[k];
                }
                if (cnt > 1) {
                    cout << i+1 << ' ' << j+1 << endl;
                    goto next;
                }
            }
        }

        rep(i,n) {
            if (sz(a[i]) >= b) continue;
            rep(j, sz(a[i])) {
                rep(k, j+1, sz(a[i])) {
                    p[a[i][j]].emplace_back(a[i][k], i);
                }
            }
        }
        rep(i,sz(c)) {
            sort(all(p[i]));
            rep(j, sz(p[i])-1) {
                if (p[i][j].first == p[i][j+1].first) {
                    cout << p[i][j].second + 1 << ' ' << p[i][j+1].second + 1 << endl;
                    goto next;
                }
            }
        }


        cout << -1 << endl;
        next:;
    };

    return 0;
}


Comments

Submit
0 Comments
More Questions

470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number