1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp>
#define R register #define ll long long #define pair std::pair<int, int> #define mp(i, j) std::make_pair(i, j) #define sum(a, b, mod) (((a) + (b)) % mod) #define meow(cat...) fprintf(stderr, cat)
const int MaxN = 2e5 + 10;
int n, m, cnt, vis[MaxN]; std::vector<int> v, b, a[MaxN]; std::vector<pair> vec[MaxN];
void init() { v.clear(), b.clear(); memset(vis, 0, 4 * (m + 10)); for (int i = 1; i <= n; i++) a[i].resize(1); }
inline int read() { int x = 0; char ch = getchar(); while (ch > '9' || ch < '0') ch = getchar(); while (ch <= '9' && ch >= '0') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x; }
signed main() { int T = read(); while (T--) { int flag = 0; n = read(), init(); for (int i = 1; i <= n; i++) { a[i][0] = read(), m += a[i][0], a[i].resize(a[i][0] + 1); for (int j = 1; j <= a[i][0]; j++) a[i][j] = read(), b.push_back(a[i][j]); std::sort(++a[i].begin(), a[i].end()); } m = sqrt(m) / 2, std::sort(b.begin(), b.end()); b.erase(std::unique(b.begin(), b.end()), b.end()); for (int i = 1; i <= n; i++) for (int j = 1; j <= a[i][0]; j++) a[i][j] = std::lower_bound(b.begin(), b.end(), a[i][j]) - b.begin() + 1; for (int i = 1; i <= n; i++) { if (a[i][0] > m) { cnt = 0; memset(vis, 0, 4 * (b.size() + 10)); for (int j = 1; j <= a[i][0]; j++) vis[a[i][j]] = 1; for (int j = 1; j <= n; j++) if (i ^ j) { cnt = 0; for (int k = 1; k <= a[j][0]; k++) { vis[a[j][k]]++, cnt += (vis[a[j][k]] == 2); if (cnt == 2 && !flag) { flag = 1, printf("%d %d\n", i, j); break; } } for (int k = 1; k <= a[j][0]; k++) vis[a[j][k]]--; if (flag) break; } } else v.push_back(i); } memset(vis, 0, 4 * (b.size() + 10)); for(int i = 1; i <= b.size(); i++) vec[i].clear(); for(auto i : v) { for(int j = 1; j <= a[i][0]; j++) for(int k = j + 1; k <= a[i][0]; k++) vec[a[i][j]].push_back(mp(a[i][k], i)); } for(int i = 1; i <= b.size(); i++) { for(int j = 0; j < vec[i].size(); j++) { if(vis[vec[i][j].first] && !flag) { printf("%d %d\n", vis[vec[i][j].first], vec[i][j].second); flag = 1; break; } vis[vec[i][j].first] = vec[i][j].second; } if(flag) break; for (int j = 0; j < vec[i].size(); j++) vis[vec[i][j].first] = 0; } if(!flag) puts("-1"); } return 0; }
|