text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
bool inrange(int y) { return ((y >= 1) && (y <= 9)); }
int main() {
int r1, r2, c1, c2, d1, d2;
bool taken[9] = {false};
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int x = (r1 + r2);
int y = (c1 + c2);
int z = (d1 + d2);
int w;
if (x == y && y == z) {
if ((r1 + c2 - d1) % 2) {
cout << "-1\n";
return 0;
}
y = (r1 + c2 - d1) / 2;
if (!inrange(y)) {
cout << "-1\n";
return 0;
}
if (taken[y - 1]) {
cout << "-1\n";
return 0;
} else
taken[y - 1] = true;
x = r1 - y;
if (!inrange(x)) {
cout << "-1\n";
return 0;
}
if (taken[x - 1]) {
cout << "-1\n";
return 0;
} else {
taken[x - 1] = true;
}
w = d2 - y;
if (!inrange(w)) {
cout << "-1\n";
return 0;
}
if (taken[w - 1]) {
cout << "-1\n";
return 0;
} else {
taken[w - 1] = true;
}
z = c2 - y;
if (!inrange(z)) {
cout << "-1\n";
return 0;
}
if (taken[z - 1]) {
cout << "-1\n";
return 0;
} else {
taken[z - 1] = true;
}
cout << x << " " << y << "\n";
cout << w << " " << z << "\n";
} else
cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2, a = 1, b, c, d;
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
while (a <= 9) {
b = 1;
while (b <= 9) {
if (a == b) {
b++;
continue;
}
c = 1;
while (c <= 9) {
if (c == b || c == a) {
c++;
continue;
}
d = 1;
while (d <= 9) {
if (c == d || d == b || d == a) {
d++;
continue;
}
if (a + b == r1 && c + d == r2 && a + c == c1 && b + d == c2 &&
a + d == d1 && b + c == d2) {
return cout << a << " " << b << endl << c << " " << d << endl, 0;
}
d++;
}
c++;
}
b++;
}
a++;
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2, i;
cin >> r1 >> r2;
cin >> c1 >> c2;
cin >> d1 >> d2;
int x1, x2, y1, y2, y3, y4;
bool flag = false;
for (i = 1; i <= 9; i++) {
y1 = i, y2 = r1 - i, y3 = c1 - y1, y4 = c2 - y2, x1 = y1 + y4, x2 = y2 + y3;
if (y1 + y2 == r1 and y1 != y2 and y3 + y4 == r2 and y3 != y4 and
y1 != y3 and y1 + y3 == c1 and y2 != y4 and y2 + y4 == c2 and
y1 != y4 and y1 + y4 == d1 and y2 != y3 and y2 + y3 == d2) {
if (y1 >= 1 and y1 <= 9 and y2 >= 1 and y2 <= 9 and y3 >= 1 and
y3 <= 9 and y4 >= 1 and y4 <= 9) {
flag = true;
break;
}
}
}
if (flag) {
cout << y1 << " " << y2 << "\n";
cout << y3 << " " << y4;
} else {
cout << -1;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
bool cd = false;
scanf("%d %d %d %d %d %d", &r1, &r2, &c1, &c2, &d1, &d2);
for (int i = 1; i <= 9; ++i) {
for (int j = 1; j <= 9; ++j) {
for (int k = 1; k <= 9; ++k) {
for (int l = 1; l <= 9; ++l) {
if (i + j == r1 && k + l == r2 && i + k == c1 && j + l == c2 &&
i + l == d1 && j + k == d2 && i != j && i != k && i != l &&
j != k && j != l && k != l) {
printf("%d %d\n%d %d", i, j, k, l);
cd = true;
}
}
}
}
}
if (!cd) printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
scanf("%d %d", &r1, &r2);
scanf("%d %d", &c1, &c2);
scanf("%d %d", &d1, &d2);
int a1 = -1;
int a2 = -1;
int a3 = -1;
int a4 = -1;
for (int i = 1; i <= 9; i++) {
for (int y = 1; y <= 9; y++) {
for (int l = 1; l <= 9; l++) {
for (int p = 1; p <= 9; p++) {
if (i != y && i != l && i != p && y != l && y != p && l != p) {
if (i + y == r1) {
if (l + p == r2) {
if (i + l == c1) {
if (y + p == c2) {
if (i + p == d1) {
if (y + l == d2) {
a1 = i;
a2 = y;
a3 = l;
a4 = p;
}
}
}
}
}
}
}
}
}
}
}
if (a1 != -1) {
printf("%d %d\n", a1, a2);
printf("%d %d\n", a3, a4);
} else {
printf("-1\n");
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int a[maxn];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int k2 = (d2 + r2 - c2);
if (k2 % 2 == 1) {
cout << -1 << endl;
return 0;
}
int c = k2 / 2;
int a = c1 - c;
int b = r1 - a;
int d = r2 - c;
if (a < 1 || b < 1 || c < 1 || d < 1 || a > 9 || b > 9 || c > 9 || d > 9) {
cout << -1 << endl;
return 0;
}
if (a == b || a == c || a == d || b == c || b == d || c == d) {
cout << -1 << endl;
return 0;
}
if (a + b != r1 || a + c != c1 || a + d != d1 || b + c != d2 || b + d != c2 ||
c + d != r2) {
cout << -1 << endl;
return 0;
}
cout << a << " " << b << endl;
cout << c << " " << d << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
vector<int> sums{r1, c1, d1, d2, c2, r2};
for (int c = 1; c < 10; c++)
for (int d = 1; d < 10; d++)
for (int e = 1; e < 10; e++)
for (int f = 1; f < 10; f++) {
vector<int> vec{c, d, e, f};
set<int> s(vec.begin(), vec.end());
if (s.size() == 4) {
int i = 0;
for (int g = 0; g < 4; g++)
for (int h = g + 1; h < 4; h++)
if (vec[g] + vec[h] != sums[i++]) goto fail;
cout << c << ' ' << d << endl << e << ' ' << f << endl;
return 0;
fail:;
}
}
cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int r1, r2, d1, d2, c1, c2, a, b, c, d;
cin >> r1 >> r2;
cin >> d1 >> d2;
cin >> c1 >> c2;
a = (c1 + d1 - r2) / 2;
b = c1 - a;
c = r1 - a;
d = r2 - b;
if (a != b && a != c && a != d && b != c && b != d && c != d && a && b && c &&
d && a <= 9 && b <= 9 && c <= 9 && d <= 9) {
cout << a << " " << c << endl;
cout << d << " " << b;
} else
cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
int main() {
float r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
float a = c1 / 2 + (d1 + r1 - d2 - r2) / 4;
float c = c1 / 2 - (d1 + r1 - d2 - r2) / 4;
float b = c2 / 2 + (r1 + d2 - r2 - d1) / 4;
float d = c2 / 2 - (r1 + d2 - r2 - d1) / 4;
if (a > 9 || a < 1) {
cout << -1;
return 0;
}
if (b > 9 || b < 1) {
cout << -1;
return 0;
}
if (c > 9 || c < 1) {
cout << -1;
return 0;
}
if (d > 9 || d < 1) {
cout << -1;
return 0;
}
if (a + b != r1) {
cout << -1;
return 0;
}
if (d + c != r2) {
cout << -1;
return 0;
}
if (a + c != c1) {
cout << -1;
return 0;
}
if (d + b != c2) {
cout << -1;
return 0;
}
if (a + d != d1) {
cout << -1;
return 0;
}
if (c + b != d2) {
cout << -1;
return 0;
}
if (a == b || a == c || a == d || b == c || b == d || c == d) {
cout << -1;
return 0;
}
cout << (int)a << " " << (int)b << endl << (int)c << " " << (int)d;
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int r1, r2, c1, c2, d1, d2, x1, x2, x3, x4;
scanf("%d%d%d%d%d%d", &r1, &r2, &c1, &c2, &d1, &d2);
x1 = (d1 + r1 - c2) / 2;
x2 = (c2 + r1 - d1) / 2;
x3 = (2 * r2 + r1 - d1 - c2) / 2;
x4 = (d1 + c2 - r1) / 2;
bool ind = 1;
if (x1 == x2 || x1 == x3 || x1 == x4 || x2 == x3 || x2 == x4 || x3 == x4) {
ind = 0;
}
if (x1 > 9 || x1 < 1 || x2 > 9 || x2 < 1 || x3 > 9 || x3 < 1 || x4 > 9 ||
x4 < 1) {
ind = 0;
}
if (c2 + c1 != r1 + r2 || d1 + d2 != r1 + r2) {
ind = 0;
}
if (ind) {
printf("%d %d\n%d %d\n", x1, x2, x3, x4);
} else {
printf("%d\n", -1);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int num1, num2, num3, num4, flag = 0;
for (int i = 1; i < r1; i++) {
num1 = i;
num2 = r1 - i;
if (c1 - num1 > 0 && num1 <= 9) {
flag++;
num3 = c1 - num1;
} else {
flag = 0;
continue;
}
if (c2 - num2 > 0 && num2 <= 9) {
flag++;
num4 = c2 - num2;
} else {
flag = 0;
continue;
}
if (num3 + num4 == r2 && num3 <= 9)
flag++;
else {
flag = 0;
continue;
}
if (num1 + num4 == d1 && num4 <= 9)
flag++;
else {
flag = 0;
continue;
}
if (num2 + num3 == d2)
flag++;
else {
flag = 0;
continue;
}
if (flag == 5 && num1 != num2 && num1 != num3 && num1 != num4 &&
num2 != num3 && num2 != num4 && num3 != num4) {
cout << num1 << " " << num2 << "\n" << num3 << " " << num4;
return 0;
}
}
flag = 0;
if (flag == 0) cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int notIdentical(int a, int b, int c, int d) {
if (a != b && b != c && c != d && d != a && a != c && b != d) return 1;
return 0;
}
int main() {
int r1, r2, c1, c2, d1, d2, i;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (i = 1; i <= 9; ++i) {
if (r1 - i >= 1 && r1 - i <= 9) {
if (c1 - i >= 1 && c1 - i <= 9) {
if (r2 - c1 + i >= 1 && r2 - c1 + i <= 9) {
if (notIdentical(i, r1 - i, c1 - i, r2 - c1 + i)) {
if (2 * i + r2 - c1 == d1 && r1 + c1 - i * 2 == d2) {
cout << i << " " << r1 - i << endl
<< c1 - i << " " << r2 - c1 + i;
return 0;
}
}
}
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2, a, b, c, d;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (a = 1; a <= 9; a++)
for (b = 1; b <= 9; b++)
for (c = 1; c <= 9; c++)
for (d = 1; d <= 9; d++) {
if (a != b && a != c && a != d && b != c && b != d && c != d) {
if (a + b == r1 && c + d == r2 && a + c == c1 && b + d == c2 &&
a + d == d1 && b + c == d2) {
cout << a << " " << b << endl;
cout << c << " " << d << endl;
return 0;
}
}
}
cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (int i = 1; i <= 9; ++i) {
for (int j = 1; j <= 9; ++j) {
if (i == j) continue;
for (int k = 1; k <= 9; ++k) {
if (k == i || k == j) continue;
for (int l = 1; l <= 9; ++l) {
if (l == i || l == j || l == k) continue;
if (i + j == r1 && k + l == r2 && i + k == c1 && j + l == c2 &&
i + l == d1 && j + k == d2) {
cout << i << " " << j << endl << k << " " << l << endl;
return 0;
}
}
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2, x1, x2, y1, y2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
x1 = (r1 + d1 - c2) / 2;
x2 = (r1 + d2 - c1) / 2;
y1 = (r2 + d2 - c2) / 2;
y2 = (r2 + d1 - c1) / 2;
if (x1 + x2 == r1 && y1 + y2 == r2 && c1 == x1 + y1 && c2 == x2 + y2 &&
d1 == x1 + y2 && d2 == x2 + y1 && x1 != x2 && x1 != y1 && x1 != y2 &&
x2 != y1 && x2 != y2 && y1 != y2 && x1 < 10 && x2 < 10 && y1 < 10 &&
y2 < 10 && x1 > 0 && x2 > 0 && y1 > 0 && y2 > 0) {
cout << x1 << " " << x2 << endl;
cout << y1 << " " << y2 << endl;
} else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2;
int a[2][2];
bool solve() {
if (a[0][0] + a[0][1] != r1) return false;
if (a[1][0] + a[1][1] != r2) return false;
if (a[0][0] + a[1][0] != c1) return false;
if (a[0][1] + a[1][1] != c2) return false;
if (a[0][0] + a[1][1] != d1) return false;
if (a[0][1] + a[1][0] != d2) return false;
return true;
}
int main() {
bool flag = false;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (i == j) continue;
for (int k = 1; k <= 9; k++) {
if (k == i || k == j) continue;
for (int l = 1; l <= 9; l++) {
if (l == i || l == j || l == k) continue;
a[0][0] = i;
a[0][1] = j;
a[1][0] = k;
a[1][1] = l;
if (solve()) {
flag = true;
break;
}
}
if (flag) break;
}
if (flag) break;
}
if (flag) break;
}
if (flag) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
} else {
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2;
bool dfs(vector<int>& a) {
bool ans = false;
if (a.size() == 4) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (i == j) continue;
if (a[i] == a[j]) return false;
}
}
int i = a[0];
int j = a[1];
int s = a[2];
int t = a[3];
if (i + j == r1 && s + t == r2 && i + s == c1 && j + t == c2 &&
i + t == d1 && j + s == d2) {
cout << i << " " << j << endl;
cout << s << " " << t << endl;
return true;
}
} else {
for (int i = 1; i <= 9; ++i) {
a.push_back(i);
ans = dfs(a);
if (ans) return true;
a.pop_back();
}
}
return ans;
}
int main() {
while (cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2) {
vector<int> a;
bool find = dfs(a);
if (!find) {
cout << -1 << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long c1, c2, r1, r2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
set<long> st;
long t = 0;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int l = 1; l <= 9; l++) {
if (i + k == c1 and j + l == c2 and i + j == r1 and k + l == r2 and
i + l == d1 and j + k == d2 and (i != j and i != l and i != k)) {
t = 1;
st.insert(i);
st.insert(j);
st.insert(k);
st.insert(l);
if (st.size() == 4) {
cout << i << " " << j << endl;
cout << k << " " << l << endl;
return 0;
} else {
cout << -1 << endl;
return 0;
}
}
}
}
}
}
if (t == 0) cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
char s[N];
int t[N];
int main() {
int r1, r2, c1, c2, d1, d2;
scanf("%d%d%d%d%d%d", &r1, &r2, &c1, &c2, &d1, &d2);
for (int a = 1; a <= 9; a++)
for (int b = 1; b <= 9; b++)
for (int c = 1; c <= 9; c++)
for (int d = 1; d <= 9; d++) {
if (a + b != r1) continue;
if (c + d != r2) continue;
if (a + c != c1) continue;
if (b + d != c2) continue;
if (a + d != d1) continue;
if (b + c != d2) continue;
if (a == b || b == c || c == d) continue;
if (a == c || b == d) continue;
if (a == d) continue;
printf("%d %d\n%d %d\n", a, b, c, d);
scanf(" ");
exit(0);
}
puts("-1");
scanf(" ");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int a11 = (r1 + c1 - d2) / 2;
int a12 = r1 - a11;
int a21 = c1 - a11;
int a22 = d1 - a11;
if (a11 != a12 && a11 != a21 && a11 != a22 && a12 != a21 && a12 != a22 &&
a21 != a22 && a11 >= 1 && a11 <= 9 && a12 >= 1 && a12 <= 9 && a21 >= 1 &&
a21 <= 9 && a22 >= 1 && a22 <= 9 && a21 + a22 == r2 && a12 + a22 == c2 &&
a12 + a21 == d2) {
cout << a11 << " " << a12 << endl;
cout << a21 << " " << a22 << endl;
} else {
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
bool flag = true;
int A, B, C, D;
if ((r1 - c2 + d1) % 2 == 0) {
A = (r1 - c2 + d1) / 2;
D = (d1 - r1 + c2) / 2;
} else {
flag = false;
}
if ((c2 - r2 + d2) % 2 == 0) {
B = (c2 - r2 + d2) / 2;
C = (d2 - c2 + r2) / 2;
} else {
flag = false;
}
if (flag &&
(0 < A && A < 10 && 0 < B && B < 10 && 0 < C && C < 10 && 0 < D &&
D < 10) &&
!(A == B || B == C || C == D || A == D || A == C || B == D) &&
((A + C) == c1 && (B + D) == c2 && (A + B) == r1 && (C + D) == r2 &&
(A + D) == d1 && (B + C) == d2)) {
cout << A << " " << B << endl;
cout << C << " " << D << endl;
} else {
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long fx[] = {1, -1, 0, 0};
long long fy[] = {0, 0, 1, -1};
bool isprime(int n) {
int i;
if (n == 1)
return false;
else if (n == 2)
return true;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
void SieveOE(int n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (int p = 2; p <= n; p++)
if (prime[p]) cout << p << " ";
}
void dtb(int n) {
if (n <= 1) {
printf("%d", n);
} else {
dtb(n / 2);
printf("%d", n % 2);
}
}
int btd(int n) {
int num = n, dec_value = 0, base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
void printDivisors(int n) {
for (int i = 1; i <= n; i++)
if (n % i == 0) cout << i << " ";
}
int smallestDivisor(long long n) {
if (n % 2 == 0) return 2;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
;
long long r1, r2, c1, c2, d1, d2, a, b, c, d;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (a = 1; a <= 9; a++) {
for (b = 1; b <= 9; b++) {
for (c = 1; c <= 9; c++) {
for (d = 1; d <= 9; d++) {
if (a != b and a != c and a != d and b != c and b != d and c != d) {
if (a + b == r1 and c + d == r2 and a + d == d1 and b + c == d2 and
a + c == c1 and b + d == c2) {
cout << a << " " << b << endl;
cout << c << " " << d << endl;
return 0;
}
}
}
}
}
}
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int a11, a12, a21, a22;
a11 = (r1 + c1 - d2) / 2;
a21 = c1 - a11;
a12 = r1 - a11;
a22 = r2 - a21;
if (a11 != a22 && a11 != a12 && a11 != a21 && a12 != a21 && a12 != a22 &&
a21 != a22 && d2 == a21 + a12 && c2 == a12 + a22 && r2 == a21 + a22 &&
a11 > 0 && a11 <= 9 && a12 > 0 && a12 <= 9 && a21 > 0 && a21 <= 9 &&
a22 > 0 && a22 <= 9) {
cout << a11 << " " << a12 << endl;
cout << a21 << " " << a22 << endl;
} else {
cout << "-1" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2;
int A[4];
int main() {
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (int i = 1; i <= 9; i++)
for (int j = 1; j <= 9; j++)
for (int k = 1; k <= 9; k++)
for (int l = 1; l <= 9; l++) {
if (i != j && i != k && i != l && j != k && j != l && k != l) {
if (i + j == r1 && i + k == c1 && i + l == d1 && j + k == d2 &&
j + l == c2 && k + l == r2) {
cout << i << " " << j << "\n" << k << " " << l;
return 0;
}
}
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (int a = 1; a <= 9; a++)
for (int b = 1; b <= 9; b++)
for (int c = 1; c <= 9; c++)
for (int d = 1; d <= 9; d++) {
if (a + b == r1 && c + d == r2 && a + c == c1 && b + d == c2 &&
a + d == d1 && b + c == d2 && a != b && a != c && a != d &&
b != c && b != d && c != d) {
cout << a << " " << b << endl << c << " " << d << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2;
int main() {
scanf("%d%d%d%d%d%d", &r1, &r2, &c1, &c2, &d1, &d2);
for (int i = 1; i < 10; i++)
for (int j = 1; j < 10; j++)
for (int k = 1; k < 10; k++)
for (int z = 1; z < 10; z++) {
if (i == j || i == k || i == z || j == k || j == z || k == z)
continue;
if (i + j == r1 && k + z == r2 && i + k == c1 && j + z == c2 &&
i + z == d1 && j + k == d2) {
cout << i << " " << j << endl << k << " " << z;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s;
int r1, r2, c1, c2, d1, d2, a1, a2, a3, a4;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= 20; j++) {
a1 = i;
a2 = j;
if (a1 + a2 == r1) {
a3 = c1 - a1;
a4 = c2 - a2;
if (a1 + a4 == d1 && a2 + a3 == d2 && a3 + a4 == r2) {
if (a1 > 9 || a2 > 9 || a3 > 9 || a4 > 9 || a1 < 1 || a2 < 1 ||
a3 < 1 || a4 < 1)
continue;
s.insert(a1);
s.insert(a2);
s.insert(a3);
s.insert(a4);
if (s.size() == 4) {
cout << a1 << " " << a2 << endl << a3 << " " << a4;
return 0;
}
}
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c1, c2, d1, d2, r1, r2, i, j, k, l;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (i = 1; i < 10; i++) {
j = r1 - i;
k = c1 - i;
l = d1 - i;
if (i != j && i != k && i != l && j != k && j != l && k != l && j < 10 &&
k < 10 && l < 10 && j > 0 && k > 0 && l > 0) {
if (c2 == j + l && r2 == k + l && d2 == k + j) {
cout << i << " " << j << "\n" << k << " " << l;
return 0;
}
}
}
cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
int freq[10];
memset(freq, 0, sizeof(freq));
int k, r1, r2, c1, c2, d1, d2, x1, x2, y1, y2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
x1 = (c1 + r1 - d2) / 2;
v.push_back(x1);
x2 = c1 - x1;
v.push_back(x2);
y1 = (d2 + c2 - r2) / 2;
v.push_back(y1);
y2 = c2 - y1;
v.push_back(y2);
for (int i = 0; i < v.size(); i++) {
k = v[i];
if (k < 10 && k > 0) freq[k]++;
}
for (int i = 0; i < 10; i++) {
if (freq[i] > 1) {
cout << -1;
return 0;
}
}
if (x1 > 0 && x2 > 0 && y1 > 0 && y2 > 0) {
if (x1 < 10 && x2 < 10 && y1 < 10 && y2 < 10) {
cout << x1 << " " << y1 << "\n";
cout << x2 << " " << y2;
} else {
cout << -1;
return 0;
}
} else
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int a1, a2, a3, a4;
for (int i = 1; i <= 9; i++) {
a1 = i;
a2 = r1 - a1;
a3 = c1 - a1;
a4 = c2 - a2;
if (1 > a2 or 1 > a3 or 1 > a4 or a2 > 9 or a3 > 9 or a4 > 9) continue;
if (a1 == a2 or a1 == a3 or a1 == a4 or a2 == a3 or a2 == a4 or a3 == a4)
continue;
if (a3 + a4 != r2 or a1 + a4 != d1 or a2 + a3 != d2) continue;
cout << a1 << " " << a2 << endl;
cout << a3 << " " << a4 << endl;
return 0;
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
return 0;
}
void solve() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int a;
bool ans = false;
for (a = 1; a <= 9; ++a) {
int b = r1 - a;
int c = c1 - a;
int d = d1 - a;
if (a > 0 && a < 10 && b > 0 && b < 10 && c > 0 && c < 10 && d > 0 &&
d < 10) {
if (a != b && b != c && c != d && b != d && a != d && a != c) {
if (a + b == r1 && c + d == r2 && a + c == c1 && b + d == c2 &&
a + d == d1 && b + c == d2) {
cout << a << " " << b << "\n";
cout << c << " " << d << "\n";
ans = true;
}
}
}
}
if (!ans) cout << "-1";
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (long long int i = 1; i < 10; ++i) {
for (long long int j = 1; j < 10; ++j) {
for (long long int k = 1; k < 10; ++k) {
for (long long int l = 1; l < 10; ++l) {
if (i + k == c1 && i + j == r1 && i + l == d1 && j + k == d2 &&
k + l == r2 && j + l == c2 && i != j && i != k && i != l &&
j != k && j != l && k != l) {
cout << i << " " << j << " " << '\n' << k << " " << l;
return 0;
}
}
}
}
}
cout << -1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
bool ff = false;
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int l = 1; l <= 9; l++) {
if (i + j == a && k + l == b && i + k == c && j + l == d &&
i + l == e && j + k == f && i != j && i != k && i != l &&
j != k && j != l && k != l) {
ff = true;
cout << i << " " << j << endl << k << " " << l << endl;
break;
}
}
if (ff == true) break;
}
if (ff == true) break;
}
if (ff == true) break;
}
if (ff == false) cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2;
int main() {
scanf("%d%d%d%d%d%d", &r1, &r2, &c1, &c2, &d1, &d2);
int tag = 0;
for (int i = 1; i <= 9; ++i) {
for (int j = 1; j <= 9; ++j) {
for (int k = 1; k <= 9; ++k) {
for (int l = 1; l <= 9; ++l) {
if (i == j || i == k) continue;
if (i == l || j == k) continue;
if (j == l || k == l) continue;
if (i + j == r1 && i + k == c1 && k + l == r2 && j + l == c2 &&
i + l == d1 && j + k == d2) {
printf("%d %d\n", i, j);
printf("%d %d\n", k, l);
tag = 1;
break;
}
}
if (tag) break;
}
if (tag) break;
}
if (tag) break;
}
if (!tag) printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool comp(int n) { return n > 0 && n < 10; }
bool noteq(int a, int b, int c, int d) {
return a != b && a != c && a != d && b != c && b != d && c != d;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int k = r1 - c2 + d1;
if (k & 1) {
cout << "-1";
} else {
int a, b, c, d;
a = k / 2;
b = r1 - a;
c = c1 - a;
d = r2 - c;
if ((comp(a) && comp(b) && comp(c) && comp(d) && noteq(a, b, c, d)) &&
((a + b == r1) && (c + d == r2) && (a + c == c1) && (b + d == c2) &&
(a + d == d1) && (b + c == d2))) {
cout << a << " " << b << "\n" << c << " " << d;
} else {
cout << "-1";
}
}
return 0;
}
|
#include <bits/stdc++.h>
const int Inf = 2e9;
long long LINF = (long long)4e18;
using namespace std;
int r1, r2, c1, c2, d1, d2;
bool ok(int i, int j, int k, int l) {
if (i + j != r1) return false;
if (k + l != r2) return false;
if (i + k != c1) return false;
if (j + l != c2) return false;
if (i + l != d1) return false;
if (j + k != d2) return false;
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int l = 1; l <= 9; l++) {
if (i == j) continue;
if (i == k) continue;
if (i == l) continue;
if (j == k) continue;
if (j == l) continue;
if (k == l) continue;
if (!ok(i, j, k, l)) continue;
cout << i << ' ' << j << '\n';
cout << k << ' ' << l << '\n';
return 0;
}
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void SR(int &x) { scanf("%d", &x); }
void SR(long long &x) { scanf("%lld", &x); }
void SR(double &x) { scanf("%lf", &x); }
void SR(char *s) { scanf("%s", s); }
void RI() {}
template <typename I, typename... T>
void RI(I &x, T &...tail) {
SR(x);
RI(tail...);
}
int main() {
int r[2], c[2], d[2];
for (int i = 0; i < int(2); i++) RI(r[i]);
for (int i = 0; i < int(2); i++) RI(c[i]);
for (int i = 0; i < int(2); i++) RI(d[i]);
for (int x = (1); x <= int(9); x++)
for (int y = (1); y <= int(9); y++)
for (int z = (1); z <= int(9); z++)
for (int w = (1); w <= int(9); w++) {
if (x + y != r[0]) continue;
if (z + w != r[1]) continue;
if (x + z != c[0]) continue;
if (y + w != c[1]) continue;
if (x + w != d[0]) continue;
if (y + z != d[1]) continue;
set<int> s;
s.insert(x);
s.insert(y);
s.insert(z);
s.insert(w);
if (((int)(s).size()) != 4) continue;
printf("%d %d\n%d %d\n", x, y, z, w);
return 0;
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int c[10];
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int x1, x2, x3, x4;
x2 = (c2 - r2 + d2) / 2;
x3 = (r2 - c2 + d2) / 2;
x1 = r1 - x2;
x4 = c2 - x2;
int flag = 0;
if (x1 + x2 == r1 && x3 + x4 == r2 && x1 + x3 == c1 && x2 + x4 == c2 &&
x1 + x4 == d1 && x2 + x3 == d2)
flag = 1;
c[x1]++;
c[x2]++;
c[x3]++;
c[x4]++;
int sum = 0;
for (int i = 1; i < 10; i++) {
sum += c[i];
if (c[i] > 1) flag = 0;
}
if (sum != 4) flag = 0;
if (flag) {
cout << x1 << " " << x2 << endl;
cout << x3 << " " << x4 << endl;
} else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int r1, r2, c1, c2, d1, d2;
scanf("%d%d%d%d%d%d", &r1, &r2, &c1, &c2, &d1, &d2);
for (int i = 1; i < 10; i++)
for (int j = 1; j < 10; j++)
if (i != j)
for (int k = 1; k < 10; k++)
if (k != i && k != j)
for (int l = 1; l < 10; l++)
if (l != i && l != j && l != k)
if (i + j == r1 && k + l == r2 && i + k == c1 && j + l == c2 &&
i + l == d1 && j + k == d2) {
printf("%d %d\n%d %d\n", i, j, k, l);
return 0;
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MAXN = 100000 + 10;
const int INF = 0x7fffffff;
bool vis[10];
int main() {
int r1, r2, c1, c2, d1, d2, a, b, c, d;
while (scanf("%d%d%d%d%d%d", &r1, &r2, &c1, &c2, &d1, &d2) == 6) {
bool f = false;
memset(vis, -1, sizeof(vis));
for (a = 1; a < 10; a++) {
vis[a] = false;
for (b = 1; b < 10; b++) {
if (vis[b]) {
vis[b] = false;
for (c = 1; c < 10; c++) {
if (vis[c]) {
vis[c] = false;
for (d = 1; d < 10; d++) {
if (vis[d] && a + b == r1 && c + d == r2 && a + c == c1 &&
b + d == c2 && a + d == d1 && b + c == d2) {
f = true;
break;
}
}
if (f) break;
vis[c] = true;
}
}
if (f) break;
vis[b] = true;
}
}
if (f) break;
vis[a] = true;
}
if (f)
printf("%d %d\n%d %d\n", a, b, c, d);
else
printf("-1\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int c1, c2, r1, r2, d1, d2, x[5], i, j;
cin >> r1 >> r2;
cin >> c1 >> c2;
cin >> d1 >> d2;
x[3] = ((r1 - d1) + c2) / 2;
x[2] = d2 - x[3];
x[4] = c2 - x[3];
x[1] = d1 - x[4];
if (x[1] + x[2] == c1 && x[3] + x[4] == c2 && x[1] + x[3] == r1 &&
x[2] + x[4] == r2 && x[1] + x[4] == d1 && x[2] + x[3] == d2) {
for (i = 1; i < 4; i++) {
for (j = i + 1; j <= 4; j++)
if (x[i] == x[j] || x[i] > 9 || x[j] > 9 || x[i] < 1 || x[j] < 1) {
cout << -1;
return 0;
}
}
cout << x[1] << " " << x[3] << endl << x[2] << " " << x[4];
} else
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, r2, c1, c2, d1, d2;
int board[2][2];
bool taken[10];
bool ans;
void check() {
int a = board[0][0] + board[0][1];
int b = board[1][0] + board[1][1];
int c = board[0][0] + board[1][1];
int d = board[0][1] + board[1][0];
int e = board[0][0] + board[1][0];
int f = board[0][1] + board[1][1];
if (a == r1 and b == r2 and c == d1 and d == d2 and e == c1 and f == c2) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
ans = true;
return;
} else {
return;
}
}
void solve(int i, int j) {
if (j == 2) {
i++;
j = 0;
}
if (i == 2) {
check();
return;
}
for (int ii = 1; ii <= 9; ii++) {
if (!taken[ii]) {
taken[ii] = true;
board[i][j] = ii;
solve(i, j + 1);
board[i][j] = 0;
taken[ii] = false;
}
}
}
int main() {
memset(taken, false, sizeof(taken));
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
board[i][j] = 0;
}
}
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
solve(0, 0);
if (!ans) {
cout << -1 << endl;
}
}
|
#include <bits/stdc++.h>
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
#define pb push_back
#define ALL(s) s.begin(),s.end()
#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define SZ(s) int(s.size())
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int MAXN=5e5+10,MOD=1e9+7;
int add(int a, int b){a+=b;if(a>=MOD)a-=MOD;return a;}
int sub(int a, int b){a-=b;if(a<0)a+=MOD;return a;}
int mul(ll a, ll b){return a*b%MOD;}
int fpow(int a, ll b){
int r=1;
while(b){if(b&1)r=mul(r,a); b>>=1; a=mul(a,a);}
return r;
}
int p[MAXN],am[MAXN],did[MAXN];
int find(int x){return p[x]=p[x]==x?x:find(p[x]);}
bool join(int x, int y){
x=find(x); y=find(y);
if(x==y)return 0;
if(x!=y)p[x]=y,am[y]|=am[x];
return 1;
}
int main(){FIN;
int n,m,tot=0; cin>>n>>m;
fore(i,0,m) p[i]=i;
fore(i,0,n){
int k; cin>>k;
if(k==1){
int x; cin>>x; x--;
if(!am[find(x)]) am[find(x)]=1,did[i]=1,tot++;
}
if(k==2){
int x,y; cin>>x>>y; x--; y--;
int has=am[find(x)]&&am[find(y)];
if(join(x,y)&&!has)did[i]=1,tot++;
}
}
cout<<fpow(2,tot)<<" "<<tot<<"\n";
fore(i,0,n)if(did[i])cout<<i+1<<" ";cout<<"\n";
}
|
/**
* author: akifpathan
* created: Thursday 31.12.2020 11:51:21 AM
**/
/*
#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
#ifdef LOCAL
#include "debug.h"
#else
#include<bits/stdc++.h>
using namespace std;
#define debug(x...)
#endif
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set= tree<T, null_type,
less<T>,
rb_tree_tag, tree_order_statistics_node_update> ;
template<class T>
using ordered_mset= tree<T, null_type,
less_equal<T>,
rb_tree_tag, tree_order_statistics_node_update> ;
*/
/*
PBDS
-------------------------------------------------
0 based indexing
-------------------------------------------------
1) insert(value)
2) erase(value)
3) order_of_key(value) // Number of items strictly smaller than value
4) *find_by_order(k) : K-th element in a set (counting from zero)
*/
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T,typename U>
T power(T b,U n)
{
assert(n>=0);
T ans=1;
for(;n>0;n/=2,b*=b) if(n&1) ans*=b;
return ans;
}
ll power(ll b,ll n,ll mod)
{
assert(n>=0);
ll ans=1;
for(;n>0;n/=2,b=(b*b)%mod) if(n&1) ans=(ans*b)%mod;
return ans;
}
template<ll mod>
struct Modular
{
ll val;
Modular(){ val=0; }
Modular(ll _val) { val=_val%mod; if(val<0) val+=mod; }
Modular normalize(const ll x) { val=x%mod; if(val<0) val+=mod; return val; }
bool operator == (const Modular &a) const { return val==a.val; }
bool operator != (const Modular &a) const { return val!=a.val; }
bool operator < (const Modular &a) const { return val<a.val; }
Modular operator = (ll x) { return normalize(x); }
Modular operator = (const Modular &x){ val=x.val;return *this;}
Modular operator += (const Modular &a) { return normalize(val+a.val); }
Modular operator -= (const Modular &a) { return normalize(val-a.val); }
Modular operator *= (const Modular &a) { return normalize(val*a.val);}
Modular operator /= (const Modular &b) { return normalize(val*inverse(b).val); }
//friend Modular power(Modular b,ll n) {Modular ans=1; for(;n>0;n/=2,b*=b) if(n&1) ans*=b;return ans; }
friend Modular operator ^ (Modular b,ll n) { return power(b,n); }
friend Modular inverse(Modular b) { return b^(mod-2); }
friend Modular operator + (Modular a,const Modular &b) { return a+=b; }
friend Modular operator - (Modular a,const Modular &b) { return a-=b; }
friend Modular operator * (Modular a,const Modular &b) { return a*=b; }
friend Modular operator / (Modular a,const Modular &b) { return a/=b; }
friend Modular operator + (Modular a,ll b) { return a+=b; }
friend Modular operator - (Modular a,ll b) { return a-=b; }
friend Modular operator * (Modular a,ll b) { return a*=b; }
friend Modular operator / (Modular a,ll b) { return a/=b; }
friend Modular operator + (ll b,Modular a) { return a+=b; };
friend Modular operator - (ll b,const Modular &a) { Modular c(b); return c-=a; }
friend Modular operator * (ll b,Modular a) { return a*=b; };
friend Modular operator / (ll b,const Modular &a) { Modular c(b); return c/=a; }
friend istream& operator >> (istream& in,Modular &a) {ll x; in>>x; a.normalize(x); return in; }
friend ostream& operator << (ostream& out,const Modular& a) { return out<<a.val; }
};
//const ll mod=998244353;
const ll mod=1e9+7;
typedef Modular<mod> Mint;
struct DSU
{
vector<int>par;
vector<int>sz;
DSU(int n)
{
par.resize(n+1);
iota(par.begin(),par.end(),0);
sz.assign(n+1,1);
}
int Find(int a)
{
return par[a]==a?a:par[a]=Find(par[a]);
}
bool same(int a,int b)
{
a=Find(a);
b=Find(b);
return a==b;
}
bool Union(int a,int b)
{
a=Find(a);
b=Find(b);
if(a==b) return false;
if(sz[a]<sz[b]) swap(a,b);
sz[a]+=sz[b];
par[b]=a;
return true;
}
};
void solve()
{
int n,m;
cin>>n>>m;
DSU dsu(m+1);
vector<int>ans;
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
int u,v;
cin>>u;
if(k==2) cin>>v;
else v=m+1;
if(dsu.Union(u,v)) ans.push_back(i);
}
cout<<(Mint(2)^ans.size())<<" "<<ans.size()<<"\n";
for(int x: ans) cout<<x<<" ";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int testcase=1;
//cin>>testcase;
for(int i=1;i<=testcase;i++)
{
//cout<<"Case "<<i<<": ";
solve();
}
#ifdef LOCAL
cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
#endif
return 0;
}
|
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
// #include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int maxn = 5e5+5;
const int mod = 1e9+7;
int a[maxn],b[maxn][2];
int map[maxn],s[maxn];
int find(int x){
if(map[x] == x) return x;
return map[x] = find(map[x]);
}
void insert(int x,int y){
int xx = find(x);
int yy = find(y);
map[xx] = map[yy];
}
int main(){
std::ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
// freopen("../in.txt","r",stdin); freopen("../out.txt","w",stdout);
int n,m;
int S = 0,ans = 1;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
for(int j=0;j<a[i];j++) cin>>b[i][j];
}
for(int i=1;i<=m;i++) map[i] = i;
int last = 0;
for(int i=1;i<=n;i++){
if(a[i] == 1){
if(last && find(last) == find(b[i][0])) continue;
else if(last) insert(last,b[i][0]);
last = b[i][0];
ans *= 2; ans %= mod;
s[S++] = i;
}else if(find(b[i][0]) == find(b[i][1])){
continue;
}else{
insert(b[i][0],b[i][1]);
ans *= 2; ans %= mod;
s[S++] = i;
}
}
cout<<ans<<" "<<S<<"\n";
sort(s,s+S);
for(int i=0;i<S;i++){
cout<<s[i]<<" ";
}
cout<<"\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#ifdef LOCAL
#include "/Users/lbjlc/Desktop/coding/debug_utils.h"
#else
#define print(...) ;
#define printn(...) ;
#define printg(...) ;
#define fprint(...) ;
#define fprintn(...) ;
#endif
#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
// #define mp make_pair
#define fi first
#define se second
#define maxi(x, y) x = max(x, y)
#define mini(x, y) x = min(x, y)
// long long fact(long long n) { if(!n) return 1; return n*fact(n-1); }
// #define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
static uniform_int_distribution<int> dist(0, 1e9 + 6);
return dist(rng);
}
#define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);}
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;
typedef vector<long long> vll;
#define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val)
template<class T>
void make_unique(T & v) {
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() { int x; cin >> x; return x; }
long long getll() { long long x; cin >> x; return x; }
double getd() { double x; cin >> x; return x; }
// pair<int, int> a(geti(), geti()) does not work
// pair<int, int> a({geti(), geti()}) works, since it uses initializer.
const int MAXN = 5e5 + 100;
struct disjoint_set_union{
int root[MAXN], size[MAXN];
int l, r;
void init(int _l, int _r) {
l = _l; r = _r;
for(int i = l; i <= r; i++) {
root[i] = i;
size[i] = 1;
}
}
int find(int i) {
if(root[i] != i)
return root[i] = find(root[i]);
else
return i;
}
int same(int i, int j) {
return find(i) == find(j);
}
void join(int i, int j) {
if(same(i, j))
return;
// print("join",i,j);
int ri = find(i);
int rj = find(j);
if(size[ri] >= size[rj]) {
root[rj] = ri;
size[ri] += size[rj];
}
else {
root[ri] = rj;
size[rj] += size[ri];
}
}
};
disjoint_set_union dsu;
int vis[MAXN]={};
ll mod=1e9+7;
ll mypow(ll x, ll n) {
if(!n) return 1;
ll res=mypow(x,n/2);
if(n%2) return res*res%mod*x%mod;
else return res*res%mod;
}
void solve(int tt) {
// cout<<"Case #"<<tt<<": ";
}
int main(int argc, char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// solve_testcase;
int n,m,k,x,y;
cin>>n>>m;
dsu.init(1,m);
vi res;
rep(i,0,n) {
cin>>k;
if(k==1) {
cin>>x;
int & tmp = vis[dsu.find(x)];
if(!tmp) {
tmp=1;
res.pb(i+1);
}
else if(tmp==2) {
tmp=1;
res.pb(i+1);
}
}
else {
cin>>x>>y;
if(dsu.same(x,y)) continue;
int t1 = vis[dsu.find(x)];
int t2 = vis[dsu.find(y)];
if(!t1&&!t2) {
dsu.join(x,y);
vis[dsu.find(x)]=2;
res.pb(i+1);
}
else if(t1&&!t2) {
int t=vis[dsu.find(x)];
dsu.join(x,y);
vis[dsu.find(x)]=t;
res.pb(i+1);
}
else if(!t1&&t2) {
int t=vis[dsu.find(y)];
dsu.join(x,y);
vis[dsu.find(y)]=t;
res.pb(i+1);
}
else if(t1==1&&t2==2) {
dsu.join(x,y);
vis[dsu.find(y)]=1;
res.pb(i+1);
}
else if(t1==2&&t2==1) {
dsu.join(x,y);
vis[dsu.find(x)]=1;
res.pb(i+1);
}
else if(t1==2&&t2==2) {
dsu.join(x,y);
// vis[dsu.find(x)]=1;
res.pb(i+1);
}
}
}
ll ans=1;
rep(i,1,m+1) {
if(dsu.find(i)==i && vis[i]) {
print(i,vis[i],dsu.size[i]);
if(vis[i]==2)
ans *= mypow(2,dsu.size[i]-1);
else
ans *= mypow(2,dsu.size[i]);
ans %= mod;
}
}
cout<<ans<<' '<<res.size()<<endl;
for(auto x:res)cout<<x<<' ';
cout<<endl;
return 0;
}
|
/*
`-:://:::-
`//:-------:/:`
.+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.o:--...```..---+/`
`/y+o/---....---:+o.
`...````-os+/:---:/+o/--.`
`-/+++++/:. `...` :h+d+oooo+/+-` ...
`/++//:::://++-`....` -.`//````````:` `..`
`o+/::------://o/` `-` -. -` `..`
`---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..``
`....o+:-++/:--.```..-://s. `-` .- -` `-o: .-//::::/:-`
`:s+/:--....-::/+s-` .- `- -` -///:--------:/:`
./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ `+:--........--:/`
.:ooo+++++osso-` `.:-...`/` ./::-------:/:` -` :+--..``````.--:+:...-+:-`
`.-/+++++/+-.-` -. ``:so:/:--.......--:+` `-```````o+/+--..`````..--:o/-..:s+:.
```````:``.. `-` -` `+:--..`````..--/+-.../.`````..-o:--.......---/o. `
`: `:- -. .o:--..`` ``..--:o` `-` `:o+:--------:+o-`
`-`-... .. .o/--...```..--:+/` `-` `oy/so/////++o/.`
-/` `-` `- ``+s/o/:---...---:++. `-` .-../d://///:-.`
`.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-. ````
`:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `-
-++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `-
`` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.-
-----ss+:++/:--.```..-://s. /. `:: `-:. ./`
`````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-`
`-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-`
`:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-`
`./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:.
``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `- `.:--h.``..```
-.-`.- .- `+:--..`````..--//` `- /s-//::::::::.
-` `/- .. .o:--..`` ``..--:o.```.- `//:--------://`
-` .-`.-` -.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
..`-. `-. ``:os:o/:---...---:++. `- ``///+:-..``````.--:+-````-.`
`.:///////.-` .:-..` -``-+o+/:::::/+o/. `- `:+:-..`````..--:o/:--/ys+-
`-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o. `.`
`++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-`
`````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.`
-----syo/o+/:--.```..-://s. .-` `- .- `... ``-:////:-``
.` `/s//:--....-::/+s. -. `-` .- `..`
.+o+/:::--:://+s/-..` .::+y ``` .- `..`
./oo++////+oso-` `.... :y-+:::::::/` ...
`.:+oooooo/-` `....-. .//:-------:/:-.`
``...`` /+:+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.+/--...```..--:+/`
`-o/:---...---:++.
`-+o+/:---:/+o/.
`.:+oooo+/-.`
``````
*/
#ifdef aimbot
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif
#define hur(f, g) template<class c> int f(c a) {if (sizeof(c) == 8) return g##ll(a); else return g(a);}
hur(popc, __builtin_popcount) hur(ctz, __builtin_ctz) hur(clz, __builtin_clz)
/*
- place bitset modifications here
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <ostream>
#include <istream>
#include <typeinfo>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <limits>
#include <fstream>
#include <array>
#include <list>
#include <bitset>
#include <functional>
#include <random>
#include <cstring>
#include <chrono>
#define random escape__from__random__aetuhoetnuhshe
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define le(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n) - 1); i >= 0; i--)
#define apply(v, act) for (auto &x : v) { act; }
#define log(args...) {string s = #args;deque<string> deq;\
string buf = "";int bal = 0;for (char c : s) {\
if (c == '(' || c == '[' || c == '{') {bal++;\
} else if (c == ')' || c == ']' || c == '}') {\
bal--;} else {if (bal == 0) {if (c == ',') {\
deq.pb(buf);buf = "";} else {if (c != ' ') {\
buf += c;}}}}}if (!buf.empty()) {deq.pb(buf);}\
smart_io::precall_print();smart_io::_print(deq, args);}
inline int min(const int &x, const int &y) { return (((y-x)>>(32-1))&(x^y))^x; }
inline int max(const int &x, const int &y) { return (((y-x)>>(32-1))&(x^y))^y; }
inline long long min(const long long &x, const long long &y) { return (((y-x)>>(64-1))&(x^y))^x; }
inline long long max(const long long &x, const long long &y) { return (((y-x)>>(64-1))&(x^y))^y; }
#define print \
smart_io::precall_print(); \
cout,
#define scan cin,
#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void* operator new(size_t _x) { _ptr += _x; assert(_ptr < MAXMEM); return _memory + _ptr - _x; }
void operator delete (void*) noexcept {}
#endif
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
scanf("%c", &x);
if (x == '\n') {
fast_scan(x);
}
}
void fast_scan(string &x) {
scanf("%s", string_in_buffer);
x = string(string_in_buffer);
}
template<class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T>
void fast_scan(vector<T> &v) {
for (auto &x : v) fast_scan(x);
}
void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const char &x) { printf("%c", x); };
// void fast_print(__int128 x) {
// if (x == 0) { fast_print('0'); return; }
// if (x < 0) {
// fast_print('-');
// x = -x;
// }
// __int128 p = 1;
// while (x / (p * 10)) p *= 10;
// while (p) {
// __int128 symb = x / p;
// fast_print((int)symb);
// x -= p * symb;
// p /= 10;
// }
// };
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const string &x) { printf("%s", x.c_str());}
void fast_print(const char v[]) { fast_print((string)v); }
template<class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T>
void fast_print(const vector<T> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const vector<vector<T>> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const T &v) {
for (const auto &x : v) {
fast_print(x);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template<class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} //namespace smart_io
template <class T>
ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T>
istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace random {
using namespace std::chrono;
mt19937 rng(duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
).count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
};
namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
}
namespace numbers_operation {
template<class T>
inline T floor_mod(T a, const T &b) {
a %= b;
if (a < 0) a += b;
return a;
}
}
using namespace numbers_operation;
using namespace typedefs;
using namespace random;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template<ll MOD>
struct Ring {
ll value = 0;
Ring() {}
Ring(int _value) {
value = _value;
value = floor_mod(value, MOD);
}
Ring(ll _value) {
value = _value;
value = floor_mod(value, MOD);
}
Ring pow(ll p) const {
Ring r = 1;
Ring x; x.value = value;
while (p) {
if (p & 1) r *= x;
x *= x;
p /= 2;
}
return r;
}
Ring inv() const {
return pow(MOD - 2);
}
void operator*=(const Ring<MOD> &b) {
value *= b.value;
value = floor_mod(value, MOD);
}
friend Ring operator*(Ring<MOD> a, const Ring<MOD> &b) {
a *= b;
return a;
}
void operator+=(const Ring<MOD> &b) {
value += b.value;
value -= (value >= MOD) * MOD;
}
friend Ring operator+(Ring a, const Ring &b) {
a += b;
return a;
}
void operator-=(const Ring<MOD> &b) {
value -= b.value;
value += (value < 0) ? MOD : 0;
}
friend Ring operator-(Ring a, const Ring &b) {
a -= b;
return a;
}
void operator/=(const Ring<MOD> &b) {
(*this) *= b.inv();
}
friend Ring operator/(Ring a, const Ring &b) {
a /= b;
return a;
}
bool operator==(const Ring<MOD> &b) {
return value == b.value;
}
bool operator!=(const Ring<MOD> &b) {
return value != b.value;
}
friend void fast_print(const Ring<MOD> &b) {
fast_print(b.value);
}
};
typedef Ring<MOD> num;
int n, m;
vector<int> up;
vector<bool> one;
int get_root(int v) {
if (up[v] == v) return v;
return up[v] = get_root(up[v]);
}
signed main(signed argc, char *argv[]) {
scan n, m;
up.resize(m);
one.resize(m);
iota(up.begin(), up.end(), 0);
vector<int> pick;
f(i, n) {
int k; scan k;
vector<int> cords(k);
scan cords;
apply(cords, x--)
if (k == 1) {
int top = get_root(cords[0]);
if (!one[top]) {
one[top] = true;
pick.pb(i + 1);
}
} else if (k == 2) {
int a = get_root(cords[0]);
int b = get_root(cords[1]);
if (a != b && !(one[a] && one[b])) {
if (one[b]) {
one[a] = true;
}
up[b] = a;
pick.pb(i + 1);
}
}
}
print num(2).pow(le(pick)), le(pick);
print pick;
}
|
#include <bits/stdc++.h>
using namespace std;
#define debbuging false
#define _ if(!debbuging) ios_base::sync_with_stdio(0);cin.tie(0);
#define debug if(debbuging) cout
#define endl '\n'
typedef long long ll;
typedef tuple<int,int,int> t3;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const int MOD = 1e9 + 7;
struct dsu {
vector<int> id, sz;
dsu(int sz_) : id(sz_), sz(sz_,1) { iota(id.begin(), id.end(),0); }
int find(int a) { return id[a] = a == id[a] ? a : find(id[a]); }
void unite(int a, int b) {
a = find(a), b = find(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
id[b] = a;
}
};
int main(){ _
int n, m; cin >> n >> m;
vector<vector<int>> g(m+1);
vector<t3> edg;
for(int i = 0; i < n; i++) {
int k; cin >> k;
if(k == 1) {
int a; cin >> a; a--;
g[m].push_back(a);
g[a].push_back(m);
edg.push_back({i,a,m});
}
else {
int a, b; cin >> a >> b; a--, b--;
g[a].push_back(b);
g[b].push_back(a);
edg.push_back({i,a,b});
}
}
vector<int> vans;
sort(edg.begin(), edg.end());
dsu dsu(m+1);
for(auto [id,a,b] : edg) if(dsu.find(a) != dsu.find(b)) {
vans.push_back(id);
dsu.unite(a,b);
}
vector<ll> p2(m+1);
ll p = 1;
for(int i = 0; i <= m; i++) p2[i] = p, p = (p * 2) % MOD;
cout << p2[vans.size()] << ' ' << vans.size() << endl;
sort(vans.begin(), vans.end());
for(auto u : vans) cout << u + 1 << ' ';
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
using dbl = double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }
void debug_out()
{
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B)
{
cerr << ' ' << A;
debug_out(B...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 1337
#endif
const int maxN = 500105;
const int MOD = 1e9 + 7;
int add(int a, int b)
{
a += b;
if (a >= MOD)
a -= MOD;
return a;
}
void vadd(int &a, int b)
{
a += b;
if (a >= MOD)
a -= MOD;
}
int mult(int a, int b)
{
return a * (ll)b % MOD;
}
int p2[maxN];
int q[maxN];
int badc[maxN];
int gt(int x)
{
return q[x] < 0 ? x : q[x] = gt(q[x]);
}
bool un(int a, int b)
{
a = gt(a);
b = gt(b);
if (a == b)
return false;
if (-q[a] > -q[b])
swap(a, b);
q[b] += q[a];
badc[b] += badc[a];
q[a] = b;
return true;
}
signed main()
{
#ifdef DEBUG
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
memset(q, 255, sizeof q);
p2[0] = 1;
for (int i = 1; i < maxN; ++i)
p2[i] = mult(2, p2[i - 1]);
int n, m;
cin >> n >> m;
vector<int> ans;
for (int i = 1; i <= n; ++i)
{
int k;
cin >> k;
if (k == 1)
{
int a;
cin >> a;
a = gt(a);
if (badc[a] == 0)
{
ans.push_back(i);
++badc[a];
}
}
else
{
int a, b;
cin >> a >> b;
a = gt(a);
b = gt(b);
if (a == b || badc[a] + badc[b] > 1)
continue;
if (-q[a] > -q[b])
swap(a, b);
q[b] += q[a];
badc[b] += badc[a];
q[a] = b;
ans.push_back(i);
}
}
cout << p2[ans.size()] << ' ' << ans.size() << '\n';
for (auto i : ans)
cout << i << ' ';
cout << '\n';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> G;
int get_group(int x) {
if(G[x] == x)
return x;
return G[x] = get_group(G[x]);
}
void merge_group(int x, int y) {
int gx = get_group(x);
int gy = get_group(y);
if(gx == gy) return;
if(gx > gy)
swap(gx, gy);
G[gx] = G[gy];
}
constexpr long long MOD = 1e9 + 7;
void solve(int TestCase) {
int n, m;
cin >> n >> m;
int k = max(n, m);
vector<vector<int>> A(n, vector<int>(4));
for(auto i = 0; i < n; ++i) {
int cnt;
cin >> cnt;
for(auto j = 0; j < cnt; ++j)
cin >> A[i][j];
if(cnt == 1) A[i][1] = k+1;
A[i][2] = i + 1;
if(cnt == 2 && A[i][0] > A[i][1])
swap(A[i][0], A[i][1]);
}
G.resize(k+3);
iota(G.begin(), G.end(), 0);
for(auto& a : A) {
auto g0 = get_group(a[0]);
auto g1 = get_group(a[1]);
if(g0 == g1) a[3] = 1;
else merge_group(a[0], a[1]);
}
int cnt = 0;
for(auto& a : A)
if(!a[3]) cnt++;
ll ret = 1;
for(auto i = 0; i < cnt; ++i)
ret = ret * 2 % MOD;
cout << ret << " " << cnt << endl;
for(auto& a : A)
if(!a[3]) cout << a[2] << " ";
cout << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
//cin >> t;
for(auto i = 1; i <= t; ++i) {
//cout << "Case #"<< i << ": ";
solve(i);
}
}
|
#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;
int freex[500005];
int parent[500005];
vector<int> adj[500005];
int finder(int x)
{if(x==parent[x]){return x;}
int z=finder(parent[x]);
parent[x]=z;
return z;
}
void Union(int a,int b)
{if(finder(a)==finder(b)){return;}
int x=finder(a);
int y=finder(b);
parent[x]=y;
}
int32_t main()
{ios::sync_with_stdio(0);
cin.tie(0);
memset(freex,0,sizeof(freex));
int n,mi;
cin>>n>>mi;
int k;
vector<int> v;
for(int i=0;i<500005;i++){parent[i]=i;}
//Remember to go from 1 to m
int x,y;
for(int i=0;i<n;i++)
{cin>>k;
if(k==1)
{cin>>x;y=500004;
int z=finder(x);
}
else
{//int x,y;
cin>>x>>y;}
//cout<<finder(x)<<" "<<finder(y)<<"\n";
if(finder(x)==finder(y)){continue;}
v.push_back(i+1);
freex[x]++;freex[y]++;
adj[x].push_back(y);
adj[y].push_back(x);
Union(x,y);
}
int ans=1;
map<int,int> m;
for(int i=0;i<500005;i++)
{if(freex[i]==0){continue;}
m[finder(i)]++;
}
for(auto x:m)
{//cout<<x.second<<"\n";
for(int y=0;y<x.second-1;y++){ans=ans*2;ans%=mod;}
}
cout<<ans<<" ";
cout<<v.size()<<"\n";
for(auto x:v){cout<<x<<" ";}
}
|
#include "bits/stdc++.h"
#define requires(...) typename std::enable_if<__VA_ARGS__::value, int>::type = 0
using namespace std;
template<class U, class V>
istream &operator>>(istream &is, pair<U, V> &p) { return is >> p.first >> p.second; }
template<class U, class V>
ostream &operator<<(ostream &os, const pair<U, V> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template<class Istream, class Container, requires(is_same<Istream, istream>)>
Istream &operator>>(Istream &is, Container &container) {
for (auto &value : container) is >> value;
return is;
}
template<class Ostream, class Container, requires(is_same<Ostream, ostream>)>
Ostream &operator<<(Ostream &os, const Container &container) {
auto _begin = begin(container), _end = end(container);
for (auto it = _begin; it != _end;)
os << "{ "[it != _begin] << *it << ",}"[++it == _end];
return os;
}
namespace io {
template<class ...As>
struct last {
};
template<class ...As> using last_t = typename last<As...>::type;
template<class A>
struct last<A> {
using type = A;
};
template<class A, class ...As>
struct last<A, As...> {
using type = typename last<As...>::type;
};
template<class Z>
Z read(Z &) {
Z z;
cin >> z;
return z;
}
template<class A, class ...As>
last_t<As...> read(A &a, As &...as) { return cin >> a, read(as...); }
void log_rest() {}
template<class A, class ...As>
void log_rest(const A &a, const As &...as) {
cerr << ", " << a;
log_rest(as...);
}
template<class A, class ...As>
void log(const string &pref, const A &a, const As &...as) { cerr << pref << a, log_rest(as...); }
} // namespace io
#define A(xs) begin(xs), end(xs)
#define B(...) [&](auto &&lhs, auto &&rhs) { \
return __VA_ARGS__; \
}
#define U(...) [&](auto &&lhs, auto &&rhs) { \
auto predicate = [&](auto &&x) { \
return __VA_ARGS__; \
}; \
return predicate(lhs) < predicate(rhs); \
}
#define X first
#define Y second
#define PB push_back
#define EB emplace_back
#define R(...) __VA_ARGS__ = io::read(__VA_ARGS__)
#define RC(name, ...) name(__VA_ARGS__); cin >> name
#define G3(_1, _2, _3, FUNC, ...) FUNC
#define F1(i, n) for (decltype(n) i = {}; i != n; ++i)
#define F2(i, a, b) for (typename common_type<decltype(a), decltype(b)>::type \
down = a > b, i = a - down; i + down != b; \
down ? --i : ++i)
#define F(...) G3(__VA_ARGS__, F2, F1)(__VA_ARGS__)
#ifdef DEBUG
int recursion_depth = 0;
# define D for (bool _flag = true; _flag; _flag = !_flag)
# define L(...) (++recursion_depth, \
io::log(string(recursion_depth - 1, '\t') + \
string(__func__) + ":" + to_string(__LINE__) + \
" \t( "#__VA_ARGS__" ) := ", \
__VA_ARGS__), \
--recursion_depth, cerr << "\n")
# define dbg(...) [&](const string &func) -> auto && { \
++recursion_depth; \
auto&& value = __VA_ARGS__; \
--recursion_depth; \
cerr << string(recursion_depth, '\t') \
<< func << ":" << __LINE__ \
<< " \t"#__VA_ARGS__" = " << value << endl; \
return forward<decltype(value)>(value); \
}(__func__)
#else
# define L(...) while (false) cerr
# define D while (false)
# define dbg(...) (__VA_ARGS__)
#endif
template<class T>
T make_vec(T default_value) { return default_value; }
template<class T, class Arg, class ...Args>
auto make_vec(T default_value, Arg size, Args ...rest)
-> vector<decltype(make_vec(default_value, rest...))> {
auto level = make_vec(default_value, rest...);
return vector<decltype(level)>(size, level);
}
template<class Xs>
int sz(const Xs &xs) { return static_cast<int>(xs.size()); }
using i64 = int64_t;
using f80 = long double;
using Str = string;
template<class T = int> using Vec = vector<T>;
template<class K = int, class H = hash<K>> using US = unordered_set<K, H>;
template<class K, class V, class H = hash<K>> using UM = unordered_map<K, V, H>;
template<class U = int, class V = U> using P = pair<U, V>;
using G = Vec<Vec<int>>;
template<class T, class P>
auto bin_search(T l, T r, P p) -> T {
for (T m; m = (l + r) / 2, m != l && m != r; (p(m) ? l : r) = m);
return l;
}
Vec<int> parents, sze;
Vec<int> bad;
int get_root(int v) {
if (parents[v] == v)
return v;
return parents[v] = get_root(parents[v]);
}
bool connected(int u, int v) {
return get_root(u) == get_root(v);
}
void unite(int u, int v) {
int a = get_root(u);
int b = get_root(v);
if (sze[a] < sze[b]) swap(a, b);
parents[b] = a;
sze[a] += sze[b];
bad[a] += bad[b];
}
int64_t mod = 1000000007;
int main() {
int R(n, m);
parents.assign(m, 0);
iota(A(parents), 0);
sze.assign(m, 1);
bad.assign(m, 0);
Vec<int> ans;
for (int i = 0; i < n; ++i) {
int R(k);
if (!k) continue;
if (k == 1) {
int R(x);
--x;
int root = get_root(x);
if (bad[root])
continue;
bad[root]++;
ans.push_back(i);
} else {
int R(x, y);
--x;--y;
int ar = get_root(x), br = get_root(y);
if (ar == br || (bad[ar] + bad[br] > 1))
continue;
unite(ar, br);
ans.push_back(i);
}
}
i64 answer = 1;
for (int i = 0; i < ans.size(); ++i) {
answer *= 2;
answer %= mod;
}
cout << answer << ' ' << ans.size() << '\n';
for (int x: ans) cout << x+ 1 << ' ';
return 0;
}
namespace {
auto fast_io = [] {
#ifndef DEBUG
# ifndef INTERACTIVE
ios::sync_with_stdio(false);
cin.tie(nullptr);
# endif // INTERACTIVE
# ifdef FILES
freopen(FILES".in", "r", stdin);
freopen(FILES".out", "w", stdout);
# endif // FILES
#endif // DEBUG
cout << setprecision(8) << fixed;
cerr << boolalpha << setprecision(4) << fixed;
return 0;
}();
} // namespace
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define allv(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = INT_MAX;
const ll infll = LONG_LONG_MAX;
const ll mod = 1000000007LL;
int gcd(int x, int y){return y ? gcd(y, x % y) : y;}
ll n, m, f[505050], par[505050];
vector<ll> v[505050], ansv;
void dfs(ll x)
{
f[x] = 1;
for(auto &i : v[x])
{
if(!f[i]) dfs(i);
}
}
ll p(ll x)
{
if(x == par[x]) return x;
return par[x] = p(par[x]);
}
void mer(ll x, ll y)
{
x = p(x);
y = p(y);
if(x != y) par[x] = y;
}
int main()
{
scanf("%lld %lld", &n, &m);
for(int i = 1; i <= m; i++) par[i] = i;
for(int i = 1; i <= n; i++)
{
ll x;
scanf("%lld", &x);
if(x == 1)
{
ll y;
scanf("%lld", &y);
if(f[y]) continue;
f[y] = 1;
ansv.pb(i);
dfs(y);
}
if(x == 2)
{
ll y, z;
scanf("%lld %lld", &y, &z);
if(f[y] && f[z]) continue;
if(p(y) == p(z)) continue;
if(f[y])
{
f[z] = 1;
ansv.pb(i);
dfs(z);
continue;
}
if(f[z])
{
f[y] = 1;
ansv.pb(i);
dfs(y);
continue;
}
mer(y, z);
v[y].pb(z);
v[z].pb(y);
ansv.pb(i);
}
}
ll ans = 1;
for(int i = 0; i < ansv.size(); i++) ans = ans * 2 % mod;
printf("%lld %lld\n", ans, ansv.size());
for(auto &i : ansv) printf("%lld ", i);
return 0;
}
|
#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <iomanip>
#include <bitset>
using namespace std;
#define pi pair<int ,int>
int mod = 1e9+7 , n , m , ok[1000000] , lab[1000000];
vector <int> ruler[1000001] , res;
void unite(int u , int v)
{
if (lab[u] > lab[v])
{
swap(u , v);
}
lab[u] += lab[v];
lab[v] = u;
}
int get(int u)
{
while (lab[u] > 0) u = lab[u];
return u;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> n >> m;
for (int i = 1 ; i <= n ; i++)
{
int k , u ,v;
cin >> k;
for (int e = 1 ; e <= k ; e++)
{
cin >> v;
ruler[i].push_back(v);
}
}
for (int i = 1 ; i <= m ; i++)
{
lab[i] = -1;
}
int pre = 0;
/*for (int i = 1 ; i <= n ; i++)
{
if (ruler[i].size() == 1)
{
if (pre != 0)
{
int u = ruler[i][0];
if (u != get(pre))
{
res.push_back(i);
unite(u , get(pre));
}
}
else
{
res.push_back(i);
}
pre = ruler[i][0];
}
}*/
for (int i = 1 ; i <= n ; i++)
{
if (ruler[i].size() == 1)
{
if (pre != 0)
{
int u = ruler[i][0];
if (get(u) != get(pre))
{
res.push_back(i);
unite(get(u) , get(pre));
}
}
else
{
res.push_back(i);
}
pre = ruler[i][0];
}
else
{
int u = ruler[i][0] , v = ruler[i][1];
int dadu = get(u) , dadv = get(v);
if (dadu != dadv && ok[u] + ok[v] != 2)
{
res.push_back(i);
unite(dadu , dadv);
}
}
}
int got = 1;
for (int i = 1 ; i <= res.size() ; i++)
{
got = 1ll * got * 2 % mod;
}
sort(res.begin() , res.end());
cout <<got<<" "<<res.size()<<'\n';
for (int i = 0 ; i < res.size() ; i++)
{
cout <<res[i]<<'\n';
}
}
|
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define lun(x) (int)x.size()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<60);
const int inf=(1<<30);
const int nmax=5e5+50;
const ll mod=1e9+7;
using namespace std;
int n,m,i,j,c,vz[nmax],sz[nmax],p[nmax],k,x,y;
ll rs=1;
vector<int>g[nmax],v;
int fnd(int x)
{
if(p[x]==x)return x;
return p[x]=fnd(p[x]);
}
int uni(int x,int y)
{
x=fnd(x),y=fnd(y);
if(x==y)return 0;
if(sz[x]<sz[y])swap(x,y);
sz[x]+=sz[y];
p[y]=x;
return 1;
}
void dfs(int x)
{
vz[x]=1;
c++;
for(int i=0;i<lun(g[x]);i++)if(!vz[g[x][i]])dfs(g[x][i]);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
for(i=1;i<=m+1;i++)
{
sz[i]=1;
p[i]=i;
}
for(i=1;i<=n;i++)
{
cin>>k;
if(k==1)
{
cin>>x;
if(uni(x,m+1))
{
v.pb(i);
g[x].pb(m+1);
g[m+1].pb(x);
}
}
else
{
cin>>x>>y;
if(uni(x,y))
{
v.pb(i);
g[x].pb(y);
g[y].pb(x);
}
}
}
for(i=1;i<=m+1;i++)
{
if(vz[i])continue;
c=0;
dfs(i);
for(j=1;j<c;j++)rs=(rs*2LL)%mod;
}
cout<<rs<<" "<<lun(v)<<'\n';
for(i=0;i<lun(v);i++)cout<<v[i]<<" ";
cout<<'\n';
return 0;
}
|
//goto line 42 for some useful code
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.hpp"
#else
#define dbg(...) 47
#endif
// speed up hacks does not work with MSVC compilers
// refer https://codeforces.com/blog/entry/66279
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
// ..........................
typedef long long ll;
typedef unsigned long int ul;
typedef unsigned long long int ull;
typedef unsigned int ui;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;
#define f(i, x, n) for (int i = x; i < n; i++)
#define rf(i, n, x) for(int i=n;i>=x;--i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define mem(a, b) memset(a, b, sizeof(a))
#define fast_io() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
const ll mod = 1e9+7;
const ll oo = INT64_MAX;
const int ran = 2e5+5;
//* for other functions and declarations .......
vector<int>parent, a;
vector<int> ans;
int root(int x){
while(a[x] != x){
a[x] = a[a[x]];
x = a[x];
}
return x;
}
void uni(int x, int y){
int p = root(x);
int q = root(y);
if(p<q) a[q] = p;
else a[p] = q;
}
void solve(){
int n,m;
cin>>n>>m;
a.resize(m+1);
for(int i=0;i<m+1;++i) a[i] = i;
vector<vector<int>> inp(n);
ll cnt = 1;
f(i, 1, n+1){
int k;
cin>>k;
int x, y=0;
cin>>x;
if(k ==2)cin>>y;
int rx = root(x);
int ry = root(y);
if(rx == ry)continue;
ans.pb(i);
uni(x, y);
cnt<<=1;
cnt%=mod;
}
cout<<cnt<<" "<<ans.size()<<endl;
for(auto &aa:ans)cout<<aa<<" ";
cout<<endl;
}
int main()
{
fast_io();
cerr << "...............Console is yours! :)................." << endl;
solve();
cerr<<"......^_^....."<<endl;
return 0;
}
|
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define em emplace
#define eb emplace_back
#define pb pop_back
#define sz(v) (int) v.size()
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 5e5 + 5, MOD = 1e9 + 7;
int mul(int x, int y) {
return 1ll * x * y % MOD;
}
int n, m;
int p[N];
int get(int x) {
return x == p[x] ? x : p[x] = get(p[x]);
}
bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) {
return false;
}
p[y] = x;
return true;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i <= m; ++i) {
p[i] = i;
}
int ans = 1;
vector<int> answ;
for (int i = 1; i <= n; ++i) {
int k, x, y = 0;
cin >> k >> x;
if (k == 2) {
cin >> y;
}
if (unite(x, y)) {
ans = mul(ans, 2);
answ.eb(i);
}
}
cout << ans << " " << sz(answ) << "\n";
sort(all(answ));
for (int x : answ) {
cout << x << " ";
}
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pb emplace_back
#define pf emplace_front
#define mp make_pair
#define ld long double
#define all(x) x.begin(), x.end()
#define uniq(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
const int maxn = 5e5 + 9;
int p[maxn];
int sz[maxn];
bool kek[maxn];
int n, m;
int mod = 1e9 + 7;
int binpow(int x, int pw) {
if (pw == 0)
return 1;
if (pw == 1)
return x % mod;
int y = binpow(x, pw / 2);
y = (y * y) % mod;
if (pw % 2)
y = (y * x) % mod;
return y;
}
int find_p(int i) {
if (p[i] != i)
p[i] = find_p(p[i]);
return p[i];
}
void merg(int i, int e) {
i = find_p(i);
e = find_p(e);
if (i == e)
return;
if (sz[i] < sz[e])
swap(i, e);
sz[i] += sz[e];
p[e] = i;
kek[i] |= kek[e];
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
p[i] = i;
sz[i]= 1;
}
vector<int> ans;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 1) {
int x;
cin >> x;
x--;
int v = find_p(x);
if (!kek[v]) {
kek[v] = 1;
ans.pb(i);
}
} else {
int x, y;
cin >> x >> y;
x--, y--;
int px = find_p(x);
int py = find_p(y);
if (px != py && (!kek[px] || !kek[py])) {
merg(x, y);
ans.pb(i);
}
}
}
cout << binpow(2, ans.size()) << " ";
cout << ans.size() << "\n";
for (int i : ans)
cout << i + 1 << " ";
}
|
#include <bits/stdc++.h>
using namespace std;
class union_find{
public:
vector<int> arr;
union_find(int n){
arr.resize(n);
fill(arr.begin(),arr.end(),-1);
}
int find(int x){
return arr[x]<0?x:arr[x]=find(arr[x]);
}
void unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return;
if(arr[x]>arr[y]) swap(x,y);
arr[x]+=arr[y];
arr[y]=x;
}
bool connected(int x,int y){
return find(x) == find(y);
}
int size(int x){
return -arr[find(x)];
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin >> n >> m;
vector<bool> ta(m);
int kos=0;
union_find uf(m);
vector<vector<int>> ad(m);
function<void(int,int)> dfs=[&](int cur,int par){
ta[cur]=true;
for(int ch:ad[cur]){
if(ch==par) continue;
if(ta[ch]) continue;
ta[ch]=true;
dfs(ch,cur);
}
};
vector<int> res;
for(int i=0;i<n;i++){
int k;
cin >> k;
if(k==1){
int a;
cin >> a;
a--;
if(ta[a]) continue;
ta[a]=true;
dfs(a,-1);
res.push_back(i);
}else{
int a,b;
cin >> a >> b;
a--,b--;
if(uf.connected(a,b)) continue;
uf.unite(a,b);
ad[a].push_back(b);
ad[b].push_back(a);
if(ta[a]){
if(ta[b]) continue;
res.push_back(i);
dfs(b,-1);
}else{
res.push_back(i);
if(ta[b]) dfs(a,-1);
}
}
}
long long c=1;
long long mod=1000000007;
for(int i=0;i<res.size();i++) c=c*2%mod;
cout << c << " " << res.size() << "\n";
for(int i=0;i<res.size();i++){
cout << res[i]+1 << " ";
}
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
const ll MOD = 1e9+7;
template<class T> T powmod(T x, ll n, ll m)
{
T r = 1;
T a = x % m;
while (n>0)
{
if (n & 1)
r = (r*a)%m;
a = (a*a)%m;
n = n >> 1;
}
return r;
}
int get(int x, vector<int>& mp){
if (x == mp[x])
return x;
return mp[x] = get(mp[x], mp);
}
int main(int argc, char** argv) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
int n, m;
cin >> n >> m;
// n = m = 1e5;
// vector<list<int>> L(m+1);
set<pii> S;
vector<pii> v(n+1);
vector<bool> skip(n+1);
FOR1(i, n){
int k;
#if 0
if (i < n) {
v[i].first = i;
v[i].second = i + 1;
}
else{
v[i].first = 1;
v[i].second = i;
}
#else
cin >> k;
if(k==1) {
cin >> v[i].first;
v[i].second = m+1;
}
else
cin >> v[i].first >> v[i].second;
if (v[i].first > v[i].second)
swap(v[i].first, v[i].second);
#endif
if (!S.count(v[i])){
S.insert(v[i]);
// L[v[i].first].push_back(i);
// if (v[i].second != m+1)
// L[v[i].second].push_back(i);
}
else
skip[i] = 1;
}
vector<int> sol;
vector<int> mp(m+2);
vector<bool> done(m+2); done[m+1] = 1;
iota(mp.begin(), mp.end(), 0);
S.clear();
FOR1(i, n){
if (skip[i]) continue;
int x = get(v[i].first, mp);
int y = get(v[i].second, mp);
if (x == y) continue;
if (S.count({x, y})) continue;
if (done[x] && done[y]) continue;
if (x > y) swap(x, y);
if (!done[x] && !done[y]){
mp[x] = y;
done[x] = 1;
}
else if (!done[x] && done[y]){
done[x] = 1;
}
else{
done[y] = 1;
}
S.insert({x, y});
sol.push_back(i);
}
cout << powmod(2ll, sol.size(), MOD) << " " << sol.size() << endl;
for(auto it : sol) cout << it << " "; cout << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
class DSU {
private:
vector <int> par, sz;
int n;
public:
DSU(int _n) : n(_n) {
sz.assign(n + 1, 1);
par.assign(n + 1, 0);
iota(par.begin(), par.end(), 0);
}
int find_parent(int a) {
if(a == par[a]) return a;
return par[a] = find_parent(par[a]);
}
bool union_sets(int a, int b) {
a = find_parent(a);
b = find_parent(b);
if(a != b) {
par[b] = a;
sz[a] += sz[b];
return 1;
}
return 0;
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
DSU dsu(m + 1);
vector <int> ans;
int res = 1;
for(int i = 1; i <= n; i++) {
int k;
cin >> k;
int a, b;
cin >> a;
if(k > 1) cin >> b;
else b = m + 1;
if(dsu.union_sets(a, b)) {
res = (2LL * res) % MOD;
ans.push_back(i);
}
}
cout << res << ' ' << ans.size() << '\n';
for(int &x : ans) {
cout << x << ' ';
}
}
|
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef pair<int, int> ii;
int p[500001];
int find(int a) {
if (a == p[a]) return a;
else return p[a] = find(p[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
p[a] = b;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> one(m, 0);
vector<vector<int>> two(m);
vector<int> ans;
for (int i = 0; i <= m; ++i) p[i] = i;
int k, a, b;
for (int i = 0; i < n; ++i) {
cin >> k;
if (k == 1) {
cin >> a;
int aa = find(a);
if (aa != 0) {
ans.push_back(i);
// deleta a
p[aa] = 0;
}
}
else {
cin >> a >> b;
int aa = find(a), bb = find(b);
if (aa == 0 && bb == 0) continue;
else if (aa == 0) {
ans.push_back(i);
// deleta b
p[bb] = 0;
}
else if (bb == 0) {
ans.push_back(i);
// deleta a
p[aa] = 0;
}
else {
if (aa != bb) {
merge(a, b);
ans.push_back(i);
}
}
}
}
long long p = 1;
for (int i = 0; i < ans.size(); ++i) {
(p *= 2) %= MOD;
}
cout << p << ' ' << ans.size() << '\n';
for (int u: ans) cout << u + 1 << ' ';
cout << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define benq queue
#define pbenq priority_queue
#define all(x) x.begin(), x.end()
#define sz(x) (ll)x.size()
#define m1(x) memset(x, 1, sizeof(x))
#define m0(x) memset(x, 0, sizeof(x))
#define inf(x) memset(x, 0x3f, sizeof(x))
#define MOD 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define PI 3.14159265358979323846264338
#define flout cout << fixed << setprecision(12)
ll p[500007], r[500007], n, m;
vector<ll> v;
ll get(ll x) {return p[x] = (p[x] == x ? x : get(p[x]));}
void uni(ll x, ll y) {
x = get(x);
y = get(y);
if(x == y) return;
if(r[x] == r[y]) r[x]++;
if(r[x] > r[y]) swap(x, y);
p[x] = y;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
iota(p + 1, p + m + 1, 1);
for(ll i = 1; i <= n; ++i) {
ll k, a, b = m + 1;
cin >> k >> a;
if(k == 2) cin >> b;
if(get(a) != get(b)) {
uni(a, b);
v.push_back(i);
}
}
ll ans = 1;
for(ll i = 0; i < sz(v); ++i) ans = (ans + ans)%MOD;
cout << ans << ' ' << sz(v) << '\n';
for(ll i : v) cout << i << ' ';
cout << '\n';
}
|
#include <bits/stdc++.h>
//using namespace std;
typedef long long ll;
typedef long double ld;
//std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
//int randi = std::uniform_int_distribution<int>(0, 999)(rng);
const double EPS = 1e-9;
const double PI = acos(-1.0);
const ll OO = 2e18 + 10;
const int oo = 1e9 + 10;
const int MOD = 1e9 + 7;
struct DSU {
std::vector<int> parent, size;
void build(int n) {
parent.resize(n);
size.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
int find(int v) {
if (parent[v] == v)
return v;
return parent[v] = find(parent[v]);
}
bool unite(int v, int u) {
v = find(v);
u = find(u);
if (v == u)
return false;
if (size[u] > size[v])
std::swap(v, u);
parent[u] = v;
size[v] += size[u];
return true;
}
};
void solve() {
int n, m;
std::cin >> n >> m;
DSU dsu;
dsu.build(m + 1);
std::set<int> s;
for (int i = 0; i < n; i++) {
int k;
std::cin >> k;
if (k == 1) {
int x;
std::cin >> x;
if (dsu.find(x) != dsu.find(0)) {
dsu.unite(x, 0);
s.insert(i);
}
} else {
int x1, x2;
std::cin >> x1 >> x2;
if (dsu.find(x1) != dsu.find(x2)) {
dsu.unite(x1, x2);
s.insert(i);
}
}
}
ll ans = 1;
for (int i = 0; i < s.size(); i++) {
ans = (ans * 2) % MOD;
}
std::cout << ans << " " << s.size() << "\n";
for (auto &i : s) {
std::cout << (i + 1) << " ";
}
std::cout << "\n";
}
int main() {
std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int tests = 1;
// std::cin >> tests;
while (tests--) {
solve();
}
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// std::cout << "time in micros: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif
#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
class UnionFind
{ // OOP style
private:
vector<int> p, rank, setSize; // remember: vi is vector<int>
int numSets;
public:
UnionFind(int N)
{
setSize.assign(N, 1);
numSets = N;
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
bool unionSet(int i, int j)
{
if (!isSameSet(i, j))
{
numSets--;
int x = findSet(i), y = findSet(j);
// rank is used to keep the tree short
if (rank[x] > rank[y])
{
p[y] = x;
setSize[x] += setSize[y];
}
else
{
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y])
rank[y]++;
}
return true;
}
return false;
}
int numDisjointSets() { return numSets; }
int sizeOfSet(int i) { return setSize[findSet(i)]; }
};
void solveCase()
{
int n, m;
cin >> n >> m;
UnionFind u(m + 1);
vector<int> res;
int val = 1;
for (size_t i = 0; i < n; i++)
{
int k;
cin >> k;
int a = 0, b = 0;
cin >> a;
if (k > 1)
cin >> b;
if (u.unionSet(a, b))
{
res.pb(i + 1);
val += val;
if (val >= MOD)
val -= MOD;
}
}
cout << val << ' ';
cout << res.size() << '\n';
for (auto &x : res)
cout << x << ' ';
cout << '\n';
}
int32_t main()
{
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i)
solveCase();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int maxn=5e5+5;
int f[maxn],ans[maxn];
int _find(int x){return x!=f[x]?f[x]=_find(f[x]):f[x];}
int main(){
int n,m,id=0,num=1;scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)f[i]=i;
for(int i=1;i<=n;i++){
int k,a,b;scanf("%d",&k);
if(k==1)a=0,scanf("%d",&b);
else scanf("%d%d",&a,&b);
int aa=_find(a),bb=_find(b);
if(aa!=bb){
f[aa]=bb;
ans[id++]=i;
(num*=2)%=mod;
}
}
printf("%d %d\n",num,id);
for(int i=0;i<id;i++)printf("%d ",ans[i]);printf("\n");
return 0;
}
|
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <deque>
#include <set>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <ctime>
#include <queue>
#include <stack>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <fstream>
#include <bitset>
#include <complex>
#include <numeric>
//#include "utils/haha.h"
//#include "utils/max_flow.h"
using namespace std;
typedef pair<int, int> PII;
typedef pair<string, string> PSS;
typedef pair<string, int> PSI;
typedef pair<int, PII> PIP;
typedef long long ll;
typedef pair<int, ll> PIL;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<ll, PII> PLP;
template<typename T>
inline T read_by_char() {
T s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
s = (s << 3) + (s << 1) + ch - 48;
ch = getchar();
}
return s * f;
}
#define ri() read_by_char<int>()
#define rl() read_by_char<ll>()
#define CLS(x, v) (memset((x), (v), sizeof((x))))
template<class TH>
void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',')cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template<class T>
ostream &operator<<(ostream &os, set<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template<class T>
ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template<class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
#ifdef _zzz_
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
template<class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T>
using max_heap = priority_queue<T>;
//const int N = 1e6 + 1e5 + 10;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
struct PairHash {
template<typename T1, typename T2>
std::size_t operator()(const pair<T1, T2> &p) const {
return hash<T1>()(p.first) ^ hash<T2>()(p.second);
}
};
template<unsigned MOD_>
struct ModInt {
static constexpr unsigned MOD = MOD_;
unsigned x;
void undef() { x = (unsigned) -1; }
bool isnan() const { return x == (unsigned) -1; }
inline int geti() const { return (int) x; }
ModInt() { x = 0; }
ModInt(const ModInt &y) { x = y.x; }
ModInt(int y) {
if (y < 0 || (int) MOD <= y) y %= (int) MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned y) { if (MOD <= y) x = y % MOD; else x = y; }
ModInt(long long y) {
if (y < 0 || MOD <= y) y %= MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned long long y) { if (MOD <= y) x = y % MOD; else x = y; }
ModInt &operator+=(const ModInt y) {
if ((x += y.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt y) {
if ((x -= y.x) & (1u << 31)) x += MOD;
return *this;
}
ModInt &operator*=(const ModInt y) {
x = (unsigned long long) x * y.x % MOD;
return *this;
}
ModInt &operator/=(const ModInt y) {
x = (unsigned long long) x * y.inv().x % MOD;
return *this;
}
ModInt operator-() const { return (x ? MOD - x : 0); }
ModInt inv() const { return pow(MOD - 2); }
ModInt pow(long long y) const {
ModInt b = *this, r = 1;
if (y < 0) {
b = b.inv();
y = -y;
}
for (; y; y >>= 1) {
if (y & 1) r *= b;
b *= b;
}
return r;
}
friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
const unsigned int mod = 1e9 + 7;
typedef ModInt<mod> mod_int;
void update(int &v, int nv) {
if (nv == -1) return;
if (v < 0 || v > nv) v = nv;
}
struct Disjoint {
Disjoint(int n) {
p.assign(n + 1, -1);
}
vector<int> p;
bool join(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb) return false;
int sum = p[ra] + p[rb];
if (p[ra] < p[rb]) {
p[rb] = ra;
p[ra] = sum;
} else {
p[ra] = rb;
p[rb] = sum;
}
return true;
}
int root(int x) {
return p[x] < 0 ? x : (p[x] = root(p[x]));
}
};
void solve(int ncase) {
int n = ri(), m = ri();
Disjoint djs(m + 1);
int cnt = 0;
vector<int> a;
for (int i = 0; i < n; i++) {
int k = ri();
int b[2] = {0, 0};
for (int j = 0; j < k; j++) b[j] = ri();
if (djs.join(b[0], b[1])) {
a.push_back(i + 1);
}
}
mod_int ret = mod_int(2).pow(a.size());
printf("%d %d\n", ret.geti(), a.size());
for (int i = 0; i < a.size(); i++) printf("%d%c", a[i], " \n"[i + 1 == a.size()]);
}
void solve_all_cases() {
int T = 1;
//scanf("%d", &T);
//cin >> T;
//T = ri();
int ncase = 0;
// pre_calc();
while (T--) {
solve(++ncase);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << std::fixed;
cout << setprecision(9);
#ifdef _zzz_
//ios_base::sync_with_stdio(true);
freopen("C:\\Users\\grain\\Desktop\\in.txt", "r", stdin);
//auto x = freopen("C:\\Users\\grain\\Desktop\\out.txt", "w", stdout);
//cerr << x << " " << errno << endl;
auto start_time = clock();
#endif
solve_all_cases();
//test();
#ifdef _zzz_
cout << (clock() - start_time) * 1.0 / CLOCKS_PER_SEC << " seconds" << endl;
#endif
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
|
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 500010, p = 1e9 + 7;
int n, m;
vector<vector<int>> vs;
struct dsu {
vector<int> g, sz, mxv;
dsu() {}
dsu(int n) { g.resize(n+1), sz.resize(n+1, 1), mxv.resize(n+1), iota(AI(g), 0), iota(AI(mxv), 0); }
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
bool M(int a, int b) {
a = F(a), b = F(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
return g[b] = a, sz[a] += sz[b], chmax(mxv[a], mxv[b]), true;
}
int operator()(int i) { return F(i); }
};
ll bin_pow(ll v, ll t) {
ll res = 1;
for (;t;t>>=1, v = v * v % p)
if (t&1) res = res * v % p;
return res;
}
int pa[MAX_N];
int getpa(int i) { return i == pa[i] ? i : pa[i] = getpa(pa[i]); }
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
vs.resize(n);
for (auto &v : vs) {
int len;
cin >> len;
v.resize(len);
for (auto &x : v) cin >> x;
sort(AI(v));
}
iota(pa, pa+m+5, 0);
vector<int> res;
int bad = m + 2;
for (int i = 0;i < n;++i) {
auto &vec = vs[i];
for (int &u : vec) u = getpa(u);
sort(AI(vec));
while (vec.size() && vec.back() == bad) vec.pop_back();
if (vec.empty() || (vec.size() == 2 && vec[0] == vec[1])) continue;
if (vec.size() == 1)
pa[vec[0]] = bad;
else
pa[vec[0]] = vec[1];
res.pb(i);
}
// for (int i = 0;i < n;++i) {
// if (vs[i].size() > 1) D.M(vs[i][0], vs[i][1]);
// }
//
// {
// vector<vector<int>> lisan(m + 1);
// for (int i = 0;i < n;++i) {
// auto &vec = lisan[ D(vs[i][0]) ];
// vec.insert(end(vec), AI(vs[i]));
// }
// for (auto &vec : lisan) sort(AI(vec)), vec.resize(unique(AI(vec)) - begin(vec));
//
// for (int i = 0;i < n;++i) {
// auto &vec = lisan[ D(vs[i][0]) ];
// for (auto &u : vs[i]) u = lower_bound(AI(vec), u) - begin(vec);
// }
// }
// // lisan is done
// //
// // then I need to calculate thing
// vector<int> res;
//
// vector<dsu> DS(m+1);
// for (int i = 0;i < n;++i) {
// }
cout << bin_pow(2, res.size()) << ' ' << res.size() << '\n';
for (int u : res) cout << u+1 << " \n"[u==res.back()];
}
|
/// In the name of God
#include <bits/stdc++.h>
#define FILE_NAME "A"
using namespace std;
using ll = long long;
#define f first
#define s second
#define pb push_back
#define pp pop_back
#define SZ(x) ((int) x.size())
#define all(x) x.begin(), x.end()
#define what_is(x) cerr << #x << " is " << x << endl;
void freekick() {
#ifndef ONLINE_JUDGE
freopen(FILE_NAME".in", "r", stdin);
freopen(FILE_NAME".out", "w", stdout);
#endif
}
const int N = 2e5 + 112;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const int DX[] = {+1, 0, -1, 0, +1, +1, -1, -1};
const int DY[] = {0, +1, 0, -1, +1, -1, +1, -1};
void freegoal() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
struct DSU {
vector<int> parent;
DSU(int n) {
parent.resize(n, -1);
}
int find_set(int v) {
if (parent[v] < 0)
return v;
return parent[v] = find_set(parent[v]);
}
int find_set2(int v) {
return 0 > parent[v] ? v : parent[v] = find_set2(parent[v]);
}
bool unite(int a, int b) {
a = find_set2(a);
b = find_set2(b);
if (a == b)
return 0;
if (parent[a] < parent[b])
swap(a, b);
parent[b] += parent[a];
parent[a] = b;
return 1;
}
};
signed main() {
freegoal();
int n, m, x, y;
cin >> n >> m;
vector<int> result;
DSU dsu(m + 1);
ll answer = 1;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 1) {
cin >> x;
y = 0;
} else {
cin >> x >> y;
}
auto count_primes = [&](int n) {
const int S = 10000;
vector<int> primes;
int nsqrt = sqrt(n);
vector<char> is_prime(nsqrt + 1, true);
for (int i = 2; i <= nsqrt; i++) {
if (is_prime[i]) {
primes.push_back(i);
for (int j = i * i; j <= nsqrt; j += i)
is_prime[j] = false;
}
}
int result = 0;
vector<char> block(S);
for (int k = 0; k * S <= n; k++) {
fill(block.begin(), block.end(), true);
int start = k * S;
for (int p : primes) {
int start_idx = (start + p - 1) / p;
int j = max(start_idx, p) * p - start;
for (; j < S; j += p)
block[j] = false;
}
if (k == 0)
block[0] = block[1] = false;
for (int i = 0; i < S && start + i <= n; i++) {
if (block[i])
result++;
}
}
return result;
};
if (dsu.unite(x, y)) {
result.pb(i + 1);
answer = answer * 2;
answer = answer % MOD;
}
}
cout << answer << " " << SZ(result) << "\n";
for (auto &r : result)
cout << r << " ";
cout << "\n";
return false;
}
|
#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define LL long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
const int mod=1e9+7;
int n,m;
vector<int>ans;
int fa[500010];
int find(int a){
return fa[a]==a?a:fa[a]=find(fa[a]);
}
bool unite(int a,int b){
a=find(a),b=find(b);
fa[a]=b;
return a!=b;
}
void init(int n){
for(int i=1;i<=n;i++)fa[i]=i;
}
int main() {
cin>>n>>m;
init(m+1);
for(int i=1,k;i<=n;i++){
scanf("%d",&k);
int fa;
scanf("%d",&fa);
int fb=m+1;
if(k>1)scanf("%d",&fb);
if(unite(fa,fb))ans.pb(i);
}
int res=1;
for(int i=0;i<ans.size();i++)res=res*2%mod;
printf("%d %d\n",res,(int)ans.size());
for(auto v:ans)printf("%d ",v);
puts("");
return 0;
}
|
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 500005;
const ll MOD = 1000000007;
int T,n,m,tot;
int fa[MAXN];
int ans[MAXN];
int getroot(int u)
{
return u == fa[u] ? u : fa[u] = getroot(fa[u]);
}
int main()
{
scanf("%d%d",&n,&m);
for (int i = 0;i <= m;i++)
fa[i] = i;
for (int k,u,v,i = 1;i <= n;i++)
{
scanf("%d",&k);
if (k == 1)
{
scanf("%d",&u);
v = 0;
}
else
scanf("%d%d",&u,&v);
int ru = getroot(u),rv = getroot(v);
if (ru != rv)
{
ans[++tot] = i;
fa[ru] = rv;
}
}
ll pw = 1;
for (int i = 1;i <= tot;i++)
(pw *= 2) %= MOD;
printf("%lld %d\n",pw,tot);
for (int i = 1;i <= tot;i++)
printf("%d ",ans[i]);
printf("\n");
return 0;
}
|
// author: erray
#include<bits/stdc++.h>
using namespace std;
// modular template by tourist
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { Modular result(*this); *this += 1; return result; }
Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (mod())
);
value = m;
#else
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
template <typename U>
friend const Modular<U>& abs(const Modular<U>& v) { return v; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
private:
Type value;
};
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
/*
using ModType = int;
struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/
constexpr int md = (int) 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
class dsu {
public:
vector<int> par, ct;
dsu(int n) {
par.resize(n);
ct.resize(n, 1);
iota(par.begin(), par.end(), 0);
}
inline int get(int v) {
return par[v] = (v == par[v] ? v : get(par[v]));
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) return false;
ct[x] += ct[y];
par[y] = x;
return true;
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n, make_pair(-1, 0));
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
cin >> a[i].first;
if (k == 2) {
cin >> a[i].second;
}
--a[i].first;
--a[i].second;
}
vector<bool> taken(m);
vector<int> ans;
dsu d(m);
for (int i = 0; i < n; ++i) {
if (a[i].second == -1) {
if (taken[d.get(a[i].first)] == false){
ans.push_back(i);
taken[d.get(a[i].first)] = true;
}
} else {
auto[v, u] = a[i];
if (taken[d.get(u)]) {
swap(v, u);
}
if ((taken[d.get(v)] && taken[d.get(u)]) || !d.unite(v, u)) {
//no
} else {
ans.push_back(i);
}
}
}
cout << power(Mint(2), (int) ans.size()) << ' ';
cout << (int) ans.size() << '\n';
for (auto el : ans) {
cout << el + 1 << ' ';
}
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define ll long long
using namespace std;
int n, m;
int f[1234567];
int rrank[1234567];
bool cycle[1234567];
int getfa(int a)
{
if (a != f[a])
f[a] = getfa(f[a]);
return f[a];
}
void connect(int a, int b)
{
int fa = getfa(a), fb = getfa(b);
if (fa == fb)
{
cycle[fa] = true;
return;
}
if (rrank[fa] == rrank[fb])
rrank[fb]++;
if (rrank[fa] > rrank[fb])
swap(fa, fb);
cycle[fb] = cycle[fb] | cycle[fa];
f[fa] = fb;
}
bool connected(int a, int b)
{
int fa = getfa(a), fb = getfa(b);
return (fa == fb) || (cycle[fa] && cycle[fb]);
}
vector<int> v;
ll ans = 1;
ll mod = 1e9 + 7;
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i <= m; i++)
f[i] = i;
for (int i = 0; i < n; i++)
{
int k;
cin >> k;
int a, b;
if (k == 1)
{
cin >> a;
b = a;
if (!cycle[getfa(a)])
{
connect(a, b);
v.push_back(i + 1);
ans *= 2;
if (ans >= mod)
ans -= mod;
}
}
else
{
cin >> a >> b;
if (!connected(a, b))
{
connect(a, b);
v.push_back(i + 1);
ans *= 2;
if (ans >= mod)
ans -= mod;
}
}
}
cout << ans << " " << v.size() << endl;
for (auto x : v)
cout << x << " ";
cout << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define FOR(i,x,y) for (register int i=(x);i<(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define Debug(v) for (auto i:v) cout<<i<<" ";puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fil(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pa;
typedef pair<ll,ll> PA;
typedef vector<int> poly;
inline ll read(){
ll x=0,f=1;char c=getchar();
while ((c<'0'||c>'9')&&(c!='-')) c=getchar();
if (c=='-') f=-1,c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
const int N = 5e5+10, mod = 1e9+7;
int n,m,f[N],vis[N];
vector<int> ans;
int cnt=1;
int fa[N];
inline int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
int main(){
n=read(),m=read();
For(i,1,m) fa[i]=i;
For(i,1,n){
int k=read();
if (k==1){
int x=Find(read());
if (!vis[x]) vis[x]=1,ans.pb(i),cnt=2ll*cnt%mod;
} else {
int x=read(),y=read();
x=Find(x),y=Find(y);
if (vis[x]&&vis[y]) continue;
if (x!=y){
ans.pb(i),cnt=2ll*cnt%mod;
fa[x]=y,vis[y]|=vis[x];
}
}
}
printf("%d %d\n",cnt,siz(ans));
for (auto i:ans) printf("%d ",i);
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m;
#define P pair<int,int>
P a[N];
int ans[N],nans;
const int mod=1e9+7;
P b[N];
int insert(int x,int y){
if(b[x].first==x&&b[x].second==y)return -1;
if(b[x].first==0){
b[x]=P(x,y);
return y;
}else{
int l=y,r=b[x].second;
if(l==0){
int t=insert(r,l);
if(t!=-1)b[x].second=max(b[x].second,t);
return t;
}
if(r==0){
int t=insert(l,r);
if(t!=-1)b[x].second=max(b[x].second,t);
return t;
}
if(l>r)swap(l,r);
int tmp=insert(l,r);
if(tmp!=-1)b[x].second=max(b[x].second,tmp);
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
int cnt;
cin>>cnt;
if(cnt>=1)cin>>a[i].first;
if(cnt>=2){
cin>>a[i].second;
if(a[i].first>a[i].second)swap(a[i].first,a[i].second);
}
}
for(int i=1;i<=n;i++)if(insert(a[i].first,a[i].second)!=-1)ans[++nans]=i;
long long base=1;
for(int i=1;i<=nans;i++)base=base*2%mod;
cout<<base<<" "<<nans<<endl;
for(int i=1;i<=nans;i++)cout<<ans[i]<<" ";cout<<endl;
return 0;
}
|
/*
Coded with Leachim's ACM Template.
No errors. No warnings. ~~
*/
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-const-variable"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wsign-compare"
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-7;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
const int RT=3;
const int MOD=1e9+7;
const int MAXN=500005;
int binpow(int x,int y,int m) {
int r=1%m;
while(y) {
if(y&1) r=1LL*r*x%m;
x=1LL*x*x%m;
y>>=1;
}
return r;
}
struct edge {
int to,next,w;
}e[MAXN<<1];
int tot,head[MAXN];
void add(int x,int y,int w) {
tot++;
e[tot].to=y;
e[tot].next=head[x];
e[tot].w=w;
head[x]=tot;
}
int fa[MAXN];
vector<int> v;
int a[MAXN];
void dfs(int x,int f) {
a[x]=-1;
for(int p=head[x];p;p=e[p].next) {
int u=e[p].to;
if(a[u]==-1) continue;
dfs(u,x);
}
}
int find(int x) {
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void join(int x,int y) {
fa[find(x)]=find(y);
}
int x[MAXN],y[MAXN];
void get(int t,int i) {
if(!a[t]) {
a[t]=-1;
v.push_back(i);
} else if(a[t]!=-1) {
v.push_back(i);
dfs(t,t);
}
}
void solve() {
int n,m;
scanf("%d %d", &n, &m);
memset(a+1,0,m*sizeof(a[0]));
for(int i=1;i<=m;i++) fa[i]=i;
for(int i=1;i<=n;i++) {
int k;
scanf("%d", &k);
if(k==1) {
int x;
scanf("%d", &x);
get(x,i);
} else {
scanf("%d %d", &x[i], &y[i]);
if(!a[x[i]] && !a[y[i]]) {
v.push_back(i);
a[x[i]]=a[y[i]]=i;
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
} else if(a[x[i]]==-1) {
get(y[i],i);
} else if(a[y[i]]==-1) {
get(x[i],i);
} else if(!a[x[i]]) {
v.push_back(i);
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
a[x[i]]=i;
} else if(!a[y[i]]) {
v.push_back(i);
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
a[y[i]]=i;
} else {
if(find(x[i])!=find(y[i])) {
v.push_back(i);
join(x[i],y[i]);
add(x[i],y[i],i);
add(y[i],x[i],i);
}
}
}
}
printf("%d %lu\n", binpow(2,v.size(),MOD), v.size());
for(auto x:v) {
printf("%d ", x);
}
puts("");
}
int main() {
int T=1,cas=1;(void)(cas);
// scanf("%d", &T);
while(T--) {
// printf("Case #%d: ", cas++);
solve();
}
return 0;
}
|
#ifdef LOCAL
//#define _GLIBCXX_DEBUG
#endif
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<char> Vc;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<char>> VVc;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define endl '\n'
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl;
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#else
#define dbg(...)
#define dbgmap(...)
#define dbgset(...)
#define dbgarr(...)
#define dbgdp(...)
#endif
#define out(a) cout<<a<<endl
#define out2(a,b) cout<<a<<" "<<b<<endl
#define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,v.size())i>>v[j];return i;}
// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
int len=v.size();
for(int i=0; i<len; ++i) {
s<<v[i];
if(i<len-1) s<<" ";
}
return s;
}
// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
s<<endl;
int len=vv.size();
for(int i=0; i<len; ++i) {
s<<vv[i]<<endl;
}
return s;
}
//mint
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%MOD+MOD)%MOD){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll n) const {
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
// for prime MOD
mint inv() const { return pow(MOD-2);}
mint& operator/=(const mint& a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
typedef vector<mint> Vmint;
typedef vector<vector<mint>> VVmint;
typedef vector<vector<vector<mint>>> VVVmint;
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
};
//UnionFind
struct UnionFind {
vector<ll> par;
UnionFind(ll n) : par(n, -1) { }
void init(ll n) { par.assign(n, -1); }
ll root(ll x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool issame(ll x, ll y) {
return root(x) == root(y);
}
bool unite(ll x, ll y) {
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
ll size(ll x) {
return -par[root(x)];
}
};
int solve(){
ll n;
cin>>n;
ll m;
cin>>m;
VV g(n);
rep(i,n){
ll k;
cin>>k;
rep(j,k){
ll id;
cin>>id;
id--;
g[i].pb(id);
}
}
Vec single(m);
Vec singleid;
ll singleroot = -1;
UnionFind uf(m);
Vec ans;
Vec hassingle(m);
dbg(g);
rep(i,n){
Vec &v = g[i];
if(v.size() == 1){
ll x = v[0];
if(singleroot == -1){
singleroot = x;
hassingle[uf.root(x)] = 1;
ans.push_back(i);
}else{
dbg(x);
dbg(uf.root(x));
dbg(hassingle[uf.root(x)]);
if(hassingle[uf.root(x)] == 1) continue;
if(hassingle[uf.root(singleroot)]){
hassingle[uf.root(x)] = 1;
}
if(hassingle[uf.root(x)]){
hassingle[uf.root(singleroot)] = 1;
}
uf.unite(singleroot, x);
hassingle[uf.root(x)] = 1;
ans.push_back(i);
}
}else{
ll x = v[0];
ll y = v[1];
if(uf.root(x) == uf.root(y)) continue;
if(hassingle[uf.root(x)]){
hassingle[uf.root(y)] = 1;
}
if(hassingle[uf.root(y)]){
hassingle[uf.root(x)] = 1;
}
uf.unite(x,y);
ans.push_back(i);
}
}
sort(ALL(ans));
cout << mint(2).pow(ans.size()) << " " << ans.size() << endl;
rep(i,ans.size()){
cout<<ans[i]+1<<" ";
}
cout<<endl;
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout<<std::setprecision(10);
// ll T;
// cin>>T;
// while(T--)
solve();
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(x) cout <<#x<<":"<<x<<endl;
#define dbgpo(x, y) cout<<"("<<x<<", "<<y<<")";
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl '\n'
#define mod 1000000007
// #define int long long
#define vi vector<int>
inline int parent(vi &par,int i){
while(i!=par[i]) {
par[i] = par[par[i]];
i = par[i];
}
return i;
}
inline void union1(vi &par, vi &sz,int p1, int p2) {
if(sz[p1]<sz[p2]){
par[p1] = p2;
sz[p2] += sz[p1];
}
else {
par[p2] = p1;
sz[p1] += sz[p2];
}
}
signed main() {
fast;
int n, dim;
cin >> n >> dim;
vector<int> par(dim+1), sz(dim+1,1);
iota(par.begin(),par.end(),0);
vector<int> ans;
for(int i = 1; i <=n; ++i) {
int k; cin >> k;
int a,b;
if(k==1)
cin >> a,b=0;
if(k==2)
cin >> a >> b;
a = parent(par,a);
b = parent(par,b);
if(a!=b) {
ans.pb(i);
union1(par,sz,a,b);
}
}
vector<int> p2(dim+1);
p2[0] = 1;
for(int i=1;i<=dim;++i)
p2[i] = (p2[i-1]<<1)%mod;
ll ansVal = 1;
for(int i=0;i<=dim;++i)
if(par[i] == i) {
ansVal *= ll(p2[sz[i]-1])%mod;
ansVal %= mod;
}
cout << ansVal << ' ' << ans.size() << endl;
for(auto &x: ans)
cout << x << ' ';
}
|
#include<bits/stdc++.h>
#define fi first
#define se second
#define LL long long
#define PI std::pair<int,int>
#define MP std::make_pair
const int N=500005,INF=0x8000000;
int n,m,fa[N];
std::vector<int>ans;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int k,x,y;
scanf("%d%d",&k,&x);
if(k==1)y=INF;
else{
scanf("%d",&y);
if(x==y)y=INF;
else if(x>y)std::swap(x,y);
}
while(1){
if(x==INF)break;
if(!fa[x]){
fa[x]=y==INF?-1:y;
ans.push_back(i);
break;
}
if(fa[x]==-1){
x=y;
y=INF;
continue;
}
if(fa[fa[x]])fa[x]=fa[fa[x]];
if(fa[x]==-1){
x=y;
y=INF;
continue;
}
x=fa[x];
if(x==y)break;
if(x>y)std::swap(x,y);
}
}
int uans=1;
for(int i=0;i<ans.size();i++)uans=uans*2%1000000007;
printf("%d %d\n",uans,(int)ans.size());
for(int x:ans)printf("%d ",x);
puts("");
}
|
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}
struct UF {
vector<int> fa, sz;
vector<bool> single;
UF(int n) : fa(n), sz(n, 1), single(n) {
iota(all(fa), 0);
}
int find(int x) {
return fa[x]==x ? x : fa[x]=find(fa[x]);
}
bool join(int x, int y) {
x=find(x), y=find(y);
if (x==y) return false;
if (single[x] && single[y]) return false;
if (sz[x]>sz[y]) swap(x, y);
fa[x]=y;
sz[y]+=sz[x];
single[y]=single[y] || single[x];
return true;
}
};
constexpr int mod=1e9+7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin>>n>>m;
UF uf(m);
vector<int> s;
for (int i=0; i<n; i++) {
int k;
cin>>k;
if (k==1) {
int x;
cin>>x;
x--;
x=uf.find(x);
if (!uf.single[x]) {
uf.single[x]=true;
s.push_back(i);
}
} else {
int x, y;
cin>>x>>y;
x--, y--;
if (uf.join(x, y)) {
s.push_back(i);
}
}
}
ll ans=1;
for (int i=0; i<(int)s.size(); i++) {
ans=2*ans%mod;
}
cout<<ans<<' '<<s.size()<<'\n';
for (auto x:s) cout<<x+1<<' ';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M; cin >> N >> M;
const int MOD = 1e9 + 7;
int tot = 1;
vector<int> ans;
vector<int> par(M); iota(par.begin(), par.end(), 0);
vector<bool> good(M);
auto get_par = [&](int v) {
while (v != par[v]) {
v = par[v] = par[par[v]];
}
return v;
};
auto merge = [&](int v, int u) {
v = get_par(v);
u = get_par(u);
if (v != u) {
par[u] = v;
if (good[u]) good[v] = true;
return true;
} else return false;
};
for (int i = 0; i < N; ++i) {
int sz; cin >> sz;
vector<int> A(sz);
for (int &a : A) cin >> a, --a;
if (sz == 1) {
int a = A[0];
if (!good[get_par(a)]) {
good[get_par(a)] = true;
tot += tot;
if (tot >= MOD) tot -= MOD;
ans.emplace_back(i);
}
} else {
int a = A[0], b = A[1];
if ((!good[get_par(a)] || !good[get_par(b)]) && merge(a, b)) {
tot += tot;
if (tot >= MOD) tot -= MOD;
ans.emplace_back(i);
}
}
}
cout << tot << ' ' << int(ans.size()) << '\n';
for (int i : ans) cout << i + 1 << ' ';
return 0;
}
|
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <string>
#include <stack>
#include <queue>
#define ll long long
#define pii pair<int, int>
#define maxn 500005
#define mod 1000000007
#define Goodbye2020 ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
struct edge{
int a, b, ind;
edge(int x, int y, int z) {
a = x, b = y, ind = z;
}
};
vector<edge> v;
vector<int> ans;
int par[maxn];
int find(int a) {
return (a == par[a] ? a : par[a] = find(par[a]));
}
void Union(int a, int b) {
par[find(a)] = find(b);
}
ll modpow(int p) {
ll ret = 1, mult = 2;
while (p) {
if (p & 1) ret = (ret * mult) % mod;
mult = (mult * mult) % mod;
p >>= 1;
}
return ret;
}
int main() {
Goodbye2020
int n, m;
cin >> n >> m;
for (int i = 0;i <= m;i++) par[i] = i;
for (int i = 0;i < n;i++) {
int k;
cin >> k;
int ed[2] = {0, 0};
for (int j = 0;j < k;j++) {
cin >> ed[j];
}
v.push_back(edge(ed[0], ed[1], i));
}
int cnt = n;
for (int i = 0;i < n;i++) {
if (find(v[i].a) != find(v[i].b)) {
Union(v[i].a, v[i].b);
ans.push_back(v[i].ind + 1);
} else {
cnt--;
}
}
cout << modpow(cnt) << " " << ans.size() << "\n";
for (int i = 0;i < ans.size();i++) cout << ans[i] << " ";
cout << endl;
}
|
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
int f[maxn];
int Find(int u) {
if (u != f[u]) {
f[u] = Find(f[u]);
}
return f[u];
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
f[i] = i;
}
vector<int> V;
for (int i = 1; i <= n; i++) {
int k;
scanf("%d", &k);
if (k == 1) {
int x;
scanf("%d", &x);
int fx = Find(x);
int f0 = Find(0);
if (fx == f0) {
continue;
}
f[fx] = f0;
V.push_back(i);
} else {
int x1, x2;
scanf("%d %d", &x1, &x2);
int fx1 = Find(x1);
int fx2 = Find(x2);
if (fx1 == fx2) {
continue;
}
f[fx1] = fx2;
V.push_back(i);
}
}
long long ans = 1;
for (int i = 1; i <= V.size(); i++) {
ans = ans * 2 % mod;
}
printf("%lld %d\n", ans, V.size());
for (int i = 0; i < V.size(); i++) {
printf("%d ", V[i]);
}
puts("");
}
|
/*
Author: AquaBlaze
Time: 2020-12-30 23:16:53
Generated by powerful Codeforces Tool
You can download the binary file in here https://github.com/xalanq/cf-tool (Windows, macOS, Linux)
Keqing best girl :)
Nephren will always be in my heart
*/
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())
#define pc(x) putchar(x)
#define MP make_pair
#define MT make_tuple
using namespace std;
typedef long long i64;
typedef tuple<int,int,int> iii;
typedef pair<int,int> pii;
typedef pair<i64,i64> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int N = 500005;
vi ans;
vi Stack;
int dsu[N];
int find(int i){
if(i == dsu[i]) return i;
Stack.eb(i);
while(dsu[Stack.back()] != Stack.back()){
Stack.eb(dsu[Stack.back()]);
}
int p = Stack.back();
Stack.pop_back();
while(!Stack.empty()){
dsu[Stack.back()] = p;
Stack.pop_back();
}
return dsu[i];
}
bool occupied[N];
const int inf = (1<<29);
const int mod = 1000000007;
void solve(){
ans.clear();
int n, m;
cin >> n >> m;
FOR(i, 1, m) dsu[i] = i;
FOR(i, 1, n){
int k, a, b;
cin >> k >> a;
if(occupied[a]){
a = find(a);
}
if(k == 1){
if(occupied[a]) continue;
occupied[a] = true;
ans.eb(i);
}else{
cin >> b;
if(occupied[b]) b = find(b);
if(occupied[b]) b = inf;
if(occupied[a]) a = inf;
if(a > b) swap(a,b);
if(a == inf) continue;
if(a == b) continue;
occupied[a] = true;
if(b != inf) dsu[a] = b;
ans.eb(i);
}
}
int cnt = 1;
FOR(i, 1, SZ(ans)) cnt = (cnt+cnt)%mod;
printf("%d %d\n",cnt,SZ(ans));
for(const int &e : ans) printf("%d ",e);
puts("");
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
/*
*
*
*
*
*
*
*
*
*
*
*/
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+5;
#define pii pair<int,int>
#define ff first
#define ss second
#define pb push_back
struct DisjointSet{
int P[N];
int R[N];
DisjointSet( ){
memset(R,0,sizeof(R));
for( int i = 1 ; i < N ; i ++ ){
P[i] = i;
}
}
int find ( int x ){
if( x == P[x] ){
return x;
}
return P[x] = find( P[x] );
}
void merge ( int x, int y ){
int px = find(x);
int py = find(y);
if( px == py ){
return;
}
if( R[px] > R[py] ){
P[py] = px;
}else{
P[px] = py;
if( R[px] == R[py] ){
R[py]++;
}
}
}
}Ds;
int n, m;
struct edge{
int v, w, ind;
edge( int _v, int _w, int _ind ){
v = _v;
w = _w;
ind = _ind;
}
};
bool cmp( edge a , edge b ){
return a.ind < b.ind;
}
vector <edge> edges;
ll MOD = 1e9+7;
ll fastExpo( ll bas , ll ex ){
if( ex == 0 ) return 1;
ll ret = fastExpo( bas , ex/2ll );
ret = (ret*ret)%MOD;
if( ex%2ll == 1 ) ret = (ret*bas)%MOD;
return ret;
}
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m ;
for( int i = 0 ; i < n ; i ++ ){
int k, x, y;
cin >> k ;
if( k == 1 ){
cin >> x ;
y = m+1;
}else if( k == 2 ){
cin >> x >> y ;
}
edges.push_back( edge( x , y , i+1 ) );
}
//sort( edges.begin() , edges.end() , cmp );
ll tot = 0;
set <int> sub;
for( edge e : edges ){
if( Ds.find( e.v ) != Ds.find( e.w ) ){
tot ++;
sub.insert( e.ind );
Ds.merge( e.v , e.w );
}
}
cout << fastExpo( 2 , tot ) << " " << tot << endl ;
for( int x : sub ){
cout << x << " ";
}
cout << endl ;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define int ll
#define fast(); ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define tests() int t; cin>>t; while(t--)
#define endl '\n'
#define F first
#define S second
#define mp make_pair
#define vi vector <ll>
#define pii pair<ll, ll>
#define pdi pair<double, ll>
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define all(x) x.begin(),x.end()
#define f(i,n) for(ll i=0;i<n;i++)
#define shout() {cout << "I'm Here...!!!" << endl;}
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define dbgv(x) { cout<< #x << ": "; for(auto i : x) cout << i << ' '; cout << '\n'; }
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }
int fpow(int a, int b) { if (b == 0)return 1; int t = (fpow(a, b / 2)); if (b % 2 == 0)return (t * t); else return ((a) * (t * t)); }
int inf = 1e15 + 100;
int mod = 1e9 + 7;
double pi = 3.1415926;
vi parent;
void make_set(int n) {
f(i, n) parent[i] = i;
}
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b)
parent[b] = a;
}
signed main()
{
int n, m;
cin >> n >> m;
parent.resize(m + 2);
int res = 1;
vi ans;
make_set(m + 1);
f(i, n) {
int c;
cin >> c;
if (c == 1) {
int x;
cin >> x;
if (find_set(x) != find_set(m + 1)) {
union_sets(m + 1, x);
res = (res * 2) % mod;
ans.push_back(i + 1);
}
}
else {
int x, y;
cin >> x >> y;
if (find_set(x) != find_set(y)) {
union_sets(y, x);
res = (res * 2) % mod;
ans.push_back(i + 1);
}
}
}
cout << res << ' ' << ans.size() << endl;
for (int i : ans) cout << i << ' ';
cout << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
const int maxn = 5e5 + 10;
typedef long long ll;
const ll mode = 1e9+7;
ll bitcount[65];
ll a[maxn];
int pre[maxn];
int find(int x){
if(x==pre[x]) return x;
else return pre[x] = find(pre[x]);
}
bool unions(int x,int y){
int xx = find(x);
int yy = find(y);
if(xx==yy) return false;
pre[xx]=yy;
return true;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
vector<int>ans;
for(int i=1;i<=m+1;i++) pre[i] = i;
for(int i=1;i<=n;i++){
int a,b=m+1,k;
scanf("%d%d",&k,&a);
if(k>1) scanf("%d",&b);
if(unions(a,b)) ans.push_back(i);
}
int res = 1;
for(int i=0;i<ans.size();i++) res=res*2%mode;
printf("%d %d\n",res,ans.size());
for(auto v:ans){
printf("%d ",v);
}
return 0;
}
// 101
// 110
|
#include <bits/stdc++.h>
using namespace std;
template <int mod> struct Modular {
int value;
Modular(int64_t v = 0) { value = v % mod; if (value < 0) value += mod; }
Modular& operator+=(Modular const& b) { value += b.value; if (value >= mod) value -= mod; return *this; }
Modular& operator-=(Modular const& b) { value -= b.value; if (value < 0) value += mod; return *this; }
Modular& operator*=(Modular const& b) { value = (int64_t) value * b.value % mod; return *this; }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular power(Modular a, int64_t e) {
Modular res = 1; for (; e; e >>= 1, a *= a) if (e & 1) res *= a;
return res;
}
friend Modular inverse(Modular a) { return power(a, mod - 2); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend bool operator==(Modular const& a, Modular const& b) { return a.value == b.value; }
friend bool operator!=(Modular const& a, Modular const& b) { return a.value != b.value; }
friend ostream& operator<<(ostream& os, Modular const& a) { return os << a.value; }
};
using Mint = Modular<int(1e9) + 7>;
struct DSU {
vector<int> p;
DSU(int n) : p(n) {
iota(p.begin(), p.end(), 0);
}
int find(int i) {
if (i == p[i]) return i;
return p[i] = find(p[i]);
}
bool merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
p[x] = y;
return true;
}
};
int main() {
int n, m;
cin >> n >> m;
Mint ans = 1;
vector<int> a;
DSU dsu(m + 2);
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
if (k == 1) {
int x;
cin >> x;
if (dsu.merge(x, m + 1)) {
a.push_back(i);
ans *= 2;
}
} else {
int x, y;
cin >> x >> y;
if (dsu.merge(x, y)) {
a.push_back(i);
ans *= 2;
}
}
}
cout << ans << ' ' << a.size() << '\n';
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " \n"[i == a.size() - 1];
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define taskname "TEST"
typedef long long ll;
typedef long double ld;
const int N = 5e5 + 5;
const int MOD = 1e9 + 7;
int n, m, up[N];
vector <int> res;
void init() {
for (int i = 1; i <= 5e5 + 1; i++) up[i] = i;
}
int findset(int u) {
if (up[u] != u) up[u] = findset(up[u]);
return up[u];
}
bool bunion(int u, int v) {
u = findset(u);
v = findset(v);
up[u] = v;
return (u != v);
}
void inp() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int k, u, v = m + 1; cin >> k;
cin >> u;
if (k > 1) cin >> v;
if (bunion(u, v)) res.push_back(i);
}
}
void out() {
int ans = 1;
for (int i = 0; i < res.size(); i++) ans = ((ll)2*ans)%MOD;
cout << ans << " " << res.size() << "\n";
for (int i = 0; i < res.size(); i++) cout << res[i] << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen(taskname".INP", "r", stdin);
// freopen(taskname".OUT", "w", stdout);
init();
inp();
out();
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#define debug(...) {\
std::cout << "[" << __FUNCTION__ << ":" << __LINE__ << "] " << #__VA_ARGS__ << " " << __VA_ARGS__ << std::endl;\
}
#else
#define debug(...)
#endif
const long long mod = 1000000007;
#define MOD(x) ((x)%mod)
struct UnionFind {
vector<int> par;
vector<int> siz;
vector<int> val;
UnionFind(int N) {
par.resize(N);
siz.resize(N);
val.resize(N);
for (int i = 0; i < N; i++) {
par[i] = i;
siz[i] = 1;
val[i] = 0;
}
}
int& operator[](const int &x) {
return val[getParent(x)];
}
int getParent(int x) {
int t = x;
while (par[x] != x) {
x = par[x];
}
par[t] = x;
return x;
}
int getSize(const int &x) {
return siz[getParent(x)];
}
bool merge(int x, int y) {
x = getParent(x);
y = getParent(y);
if (x == y) {
return false;
}
if (val[x] and val[y]) {
return false;
}
if (siz[x] > siz[y]) {
swap(x, y); // siz[x] is smaller
}
par[x] = y;
siz[y] += siz[x];
val[y] = val[x] or val[y];
return true;
}
bool connected(const int &x, const int &y) {
return getParent(x) == getParent(y);
}
};
long long power(long long x, long long n) {
int i = 0;
long long d = x;
long long ats = 1;
while (n > 0) {
if (n & (1ll << i)) {
n ^= (1ll << i);
ats *= d;
ats %= mod;
}
i++;
d = (d * d) % mod;
}
return ats;
}
/*input
3 2
1 1
1 2
2 2 1
*/
/*input
2 3
2 1 3
2 1 2
*/
/*input
3 5
2 1 2
1 3
1 4
*/
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n, m;
cin >> n >> m;
UnionFind par(m);
set<int> imt;
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
if (k == 1) {
int a;
cin >> a;
a--;
if (!par[a]) {
par[a] = true;
imt.insert(i);
}
}
else if (k == 2) {
int a, b;
cin >> a >> b;
a--, b--;
if (par.merge(a, b)) {
imt.insert(i);
}
}
}
long long vis = 0;
for (int i = 0; i < m; ++i) {
if (par.getParent(i) == i) {
if (par.val[i]) {
vis += par.siz[i];
}
else {
vis += par.siz[i] - 1;
}
}
}
cout << power(2, vis) << " " << imt.size() << "\n";
for (auto &&i : imt) {
cout << i + 1 << " ";
}
}
|
#include <bits/stdc++.h>
//define/typeDef
#define all(a) a.begin(), a.end()
#define double long double
#define int long long
#define NIL 0
#define INF LLONG_MAX
#define loop(n) for(int i=0;i<n; i++)
#define rloop(n) for(int i=n-1; i>=0; i--)
using namespace std;
const int mod = 1e9 + 7;
const int N = 5e5 + 50;
int par[N], sze[N];
int getParent(int node) {
while (par[node] != node) {
par[node] = par[par[node]];
node = par[node];
}
return node;
}
bool makeUnion(pair<int, int> edge) {
int u = edge.first;
int v = edge.second;
u = getParent(u);
v = getParent(v);
if (u == v)
return false;
if (sze[u] < sze[v])
swap(u, v);
sze[u] += sze[v];
par[v] = par[u];
return true;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int m;
cin >> m;
vector<pair<int, int> > edges;
for (int i = 0; i < n; i++) {
int cnt;
cin >> cnt;
int u, v = m + 1;
cin >> u;
if (cnt > 1)
cin >> v;
edges.emplace_back(u, v);
}
int mst = 1;
for (int i = 1; i <= m + 1; i++) {
par[i] = i, sze[i] = 1;
}
vector<int> ans;
int cnt = 0;
for (auto edge: edges) {
if (makeUnion(edge)) {
mst = (mst * 2) % mod;
ans.push_back(++cnt);
} else {
++cnt;
}
}
cout << mst << ' ';
cout<<ans.size()<<'\n';
for(auto x: ans){
cout<<x<<' ';
}
}
|
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
int n, m, cnt = 1, par[N];
vector<int> ans;
int find(int u) {
return (u == par[u]) ? u : par[u] = find(par[u]);
}
bool join(int a, int b) {
a = find(a), b = find(b);
if(a == b)
return 0;
par[b] = a;
return 1;
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= m + 1;i++)
par[i] = i;
for(int i = 1;i <= n;i++) {
int k, a, b = m + 1;
cin >> k >> a;
if(k > 1)
cin >> b;
if(join(a, b))
ans.push_back(i), cnt = (2ll * cnt) % mod;
}
cout << cnt << " " << ans.size() << '\n';
for(auto &i : ans)
cout << i << " ";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
template<class t>
void print(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t,class u>
void print(const pair<t,u>&p,int suc=1){
print(p.a,2);
print(p.b,suc);
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
//VERIFY: yosupo
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
vi p,s;
int c;
unionfind(int n):p(n,-1),s(n,1),c(n){}
int find(int a){
return p[a]==-1?a:(p[a]=find(p[a]));
}
//set b to a child of a
bool unite(int a,int b){
a=find(a);
b=find(b);
if(a==b)return false;
p[b]=a;
s[a]+=s[b];
c--;
return true;
}
bool same(int a,int b){
return find(a)==find(b);
}
int sz(int a){
return s[find(a)];
}
};
//mint107 は verify してねえ
//#define DYNAMIC_MOD
struct modinfo{uint mod,root;
#ifdef DYNAMIC_MOD
constexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}
ull im;
constexpr void set_mod(uint m){
mod=m;
im=ull(-1)/m+1;
}
uint product(uint a,uint b)const{
ull z=ull(a)*b;
uint x=((unsigned __int128)z*im)>>64;
uint v=uint(z)-x*mod;
return v<mod?v:v+mod;
}
#endif
};
template<modinfo const&ref>
struct modular{
static constexpr uint const &mod=ref.mod;
static modular root(){return modular(ref.root);}
uint v;
//modular(initializer_list<uint>ls):v(*ls.bg){}
modular(ll vv=0){s(vv%mod+mod);}
modular& s(uint vv){
v=vv<mod?vv:vv-mod;
return *this;
}
modular operator-()const{return modular()-*this;}
modular& operator+=(const modular&rhs){return s(v+rhs.v);}
modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
modular&operator*=(const modular&rhs){
#ifndef DYNAMIC_MOD
v=ull(v)*rhs.v%mod;
#else
v=ref.product(v,rhs.v);
#endif
return *this;
}
modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
modular pow(int n)const{
modular res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modular inv()const{return pow(mod-2);}
/*modular inv()const{
int x,y;
int g=extgcd<ll>(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return modular(x);
}*/
friend modular operator+(int x,const modular&y){
return modular(x)+y;
}
friend modular operator-(int x,const modular&y){
return modular(x)-y;
}
friend modular operator*(int x,const modular&y){
return modular(x)*y;
}
friend modular operator/(int x,const modular&y){
return modular(x)/y;
}
friend ostream& operator<<(ostream&os,const modular&m){
return os<<m.v;
}
friend istream& operator>>(istream&is,modular&m){
ll x;is>>x;
m=modular(x);
return is;
}
bool operator<(const modular&r)const{return v<r.v;}
bool operator==(const modular&r)const{return v==r.v;}
bool operator!=(const modular&r)const{return v!=r.v;}
explicit operator bool()const{
return v;
}
};
#ifndef DYNAMIC_MOD
//extern constexpr modinfo base{998244353,3};
extern constexpr modinfo base{1000000007,0};
//modinfo base{1,0};
#else
modinfo base(1,0);
extern constexpr modinfo base107(1000000007,0);
using mint107=modular<base107>;
#endif
using mint=modular<base>;
void slv(){
int m,n;cin>>m>>n;
unionfind uf(n);
vi has(n);
vi ans;
rep(idx,m){
int k;cin>>k;
if(k==1){
int x;cin>>x;x--;
x=uf.find(x);
if(!has[x]){
ans.pb(idx);
has[x]=1;
}
}else{
int x,y;cin>>x>>y;
x=uf.find(x-1);
y=uf.find(y-1);
if(x!=y){
if(!has[x]||!has[y]){
has[x]|=has[y];
uf.unite(x,y);
ans.pb(idx);
}
}
}
}
print(mint(2).pow(si(ans)),2);
print(si(ans));
for(auto&v:ans)v++;
print(ans);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
//int t;cin>>t;rep(_,t)
slv();
}
|
#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
#define mkp make_pair
#define fir first
#define sec second
const int N=1000005;
#define P 1000000007
template<class I>
il I Min(I p,I q){return p<q?p:q;}
template<class I>
il void ckMin(I&p,I q){p=(p<q?p:q);}
int T,n,cn[N],u,v,ans,fa[N],m;
bool tag[N];
vector<int> o;
il int fd(ct x){return fa[x]^x?fa[x]=fd(fa[x]):x;}
il void mer(it u,it v,ct i){u=fd(u),v=fd(v),(u^v)&&(tag[u]+tag[v]<=1)?fa[u]=v,tag[v]|=tag[u],o.push_back(i),(ans<<=1,ans>=P?ans-=P:0):0;}
int main(){
scanf("%d%d",&n,&m);it i,op;ans=1;
for(i=1;i<=m;++i) fa[i]=i;
for(i=1;i<=n;++i) scanf("%d%d",&op,&u),op^1?scanf("%d",&v),mer(u,v,i),0:(u=fd(u),!tag[u]?o.push_back(i),ans<<=1,ans>=P?ans-=P:0,tag[u]=1:0);
printf("%d %d\n",ans,o.size());
for(const int &i : o) printf("%d ",i);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
vi p;
int find_p(int x) {
if (x == p[x]) return x;
return p[x] = find_p(p[x]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
srand(time(0));
int n, m;
cin >> n >> m;
vi sol;
p.resize(m + 1);
iota(p.begin(), p.end(), 0);
for (int i = 0; i < n; i++) {
int k, x, y;
cin >> k >> x;
if (k == 2) cin >> y;
else y = 0;
x = find_p(x);
y = find_p(y);
if (x == y) continue;
if (rand() % 2) swap(x, y);
p[x] = y;
sol.push_back(i + 1);
}
const int MOD = 1e9 + 7;
int cnt = sol.size(), sz = 1;
for (int i = 0; i < cnt; i++) {
sz += sz;
if (sz >= MOD) sz -= MOD;
}
cout << sz << ' ' << cnt << '\n';
for (auto it : sol) cout << it << ' ';
cout << '\n';
return 0;
}
|
#include <iostream>
#include <vector>
#define ll long long
#define f first
#define s second
#define NMAX 500000
#define MOD 1000000007
using namespace std;
ll n, m, viz[NMAX+10];
ll nr, ans = 1, ans2, tata[NMAX+10], rang[NMAX+10];
vector <ll> nod[NMAX+10], a;
pair <ll, ll> v[NMAX+10];
ll findDaddy(ll x)
{ ll y = x, r = x;
while(r != tata[r]) r = tata[r];
while(x != tata[x])
{ y = tata[y];
tata[x] = r;
x = y;
}
return r;
}
void unite(ll x, ll y)
{ if(rang[x] < rang[y]) tata[x] = y;
else tata[y] = x;
if(rang[x] == rang[y]) rang[x]++;
}
ll lgput(ll a, ll n)
{ if(!n) return 1;
if(n % 2 == 0) return lgput(a*a % MOD, n/2);
return a * lgput(a*a % MOD, n/2) % MOD;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(ll i=1; i<=n; i++)
{ ll k;
cin >> k;
if(k == 1)
{ cin >> v[i].f;
ll x = v[i].f;
nod[0].push_back(x);
nod[x].push_back(0);
}
else
{ cin >> v[i].f >> v[i].s;
ll x = v[i].f, y = v[i].s;
nod[x].push_back(y);
nod[y].push_back(x);
}
}
for(ll i=0; i<=m; i++) tata[i] = i, rang[i] = 1;
for(ll i=1; i<=n; i++)
{ ll val1, val2;
if(!v[i].s) val1 = findDaddy(v[i].f), val2 = findDaddy(0);
else val1 = findDaddy(v[i].f), val2 = findDaddy(v[i].s);
if(val1 == val2) continue;
ans2++;
a.push_back(i);
unite(val1, val2);
}
cout << lgput(2LL, ans2) << ' ' << ans2 << '\n';
for(ll u : a) cout << u << ' ';
cout << '\n';
return 0;
}
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#pragma region Macros
#define FOR(i, l, r) for(int i = (l) ;i < (r) ;i++)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n+1)
#define RFOR(i, l, r) for(int i = (l) ; i >= (r) ; i--)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n , 1)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
#define ll long long
using ld = long double;
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define VEC(type, name, size) \
V<type> name(size); \
INPUT(name)
#define VVEC(type, name, h, w) \
VV<type> name(h, V<type>(w)); \
INPUT(name)
#define INT(...) \
int __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define DOUBLE(...) \
DOUBLE __VA_ARGS__; \
INPUT(__VA_ARGS__)
#define LD(...) \
LD __VA_ARGS__; \
INPUT(__VA_ARGS__)
void scan(int &a) { cin >> a;}
void scan(long long &a) { cin >> a;}
void scan(char &a) { cin >> a;}
void scan(double &a) { cin >> a;}
void scan(long double &a) { cin >> a;}
void scan(char a[]) { scanf("%s", a);}
void scan(string &a) { cin >> a;}
template <class T>
void scan(V<T> &);
template <class T, class L>
void scan(pair<T, L> &);
template <class T>
void scan(V<T> &a){
for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p){
scan(p.first);
scan(p.second);
}
template <class T>
void scan(T &a) { cin >> a;}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail){
scan(head);
INPUT(tail...);
}
template <class T>
inline void print(T x) { cout << x << '\n'; }
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for(T &in : v) is >> in;
return is;
}
template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
REP(i, SZ(v)) {
if(i) os << " ";
os << v[i];
}
return os;
}
//debug
template <typename T>
void view(const V<T> &v) {
cerr << "{ ";
for(const auto &e : v) {
cerr << e << ", ";
}
cerr << "\b\b }";
}
template <typename T>
void view(const VV<T> &vv) {
cerr << "{\n";
for(const auto &v : vv) {
cerr << "\t";
view(v);
cerr << "\n";
}
cerr << "}";
}
template <typename T, typename U>
void view(const V<pair<T, U>> &v) {
cerr << "{\n";
for(const auto &c : v) cerr << "\t(" << c.first << ", " << c.second << ")\n";
cerr << "}";
}
template <typename T, typename U>
void view(const map<T, U> &m) {
cerr << "{\n";
for(auto &t : m) cerr << "\t[" << t.first << "] : " << t.second << "\n";
cerr << "}";
}
template <typename T, typename U>
void view(const pair<T, U> &p) { cerr << "(" << p.first << ", " << p.second << ")"; }
template <typename T>
void view(const set<T> &s) {
cerr << "{ ";
for(auto &t : s) {
view(t);
cerr << ", ";
}
cerr << "\b\b }";
}
template <typename T>
void view(T e) { cerr << e; }
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
view(H);
cerr << ", ";
debug_out(T...);
}
#define debug(...) \
do { \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
debug_out(__VA_ARGS__); \
cerr << "\b\b]\n"; \
} while(0)
#else
#define debug(...) (void(0))
#endif
template <class T>
V<T> press(V<T> &x) {
V<T> res = x;
sort(all(res));
res.erase(unique(all(res)), res.end());
REP(i, SZ(x)) {
x[i] = lower_bound(all(res), x[i]) - res.begin();
}
return res;
}
template <class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if(b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T>
inline void fin(bool b = true, T e = 0) {
if(b) {
cout << e << '\n';
exit(0);
}
}
template <class T>
T divup(T x, T y) { return (x + (y - 1)) / y; }
template <typename T>
T pow(T a, long long n, T e = 1) {
T ret = e;
while(n) {
if(n & 1) ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
struct iofast {
iofast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} iofast_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion
inline int topbit(unsigned long long x){
return x?63-__builtin_clzll(x):-1;
}
inline int popcount(unsigned long long x){
return __builtin_popcountll(x);
}
inline int parity(unsigned long long x){//popcount%2
return __builtin_parity(x);
}
class DisjointSet{
public:
vector<ll> rank,p,sz;
DisjointSet(){}
DisjointSet(ll size){ //作られうる木の頂点数の最大値を入れる。
rank.resize(size,0);
p.resize(size,0);
sz.resize(size,1);
REP(i,size) makeSet(i);
}
void makeSet(ll x){ //xだけが属する木を作る。
p[x]=x;
rank[x]=0;
}
bool same(ll x,ll y){ //xとyが同じ木に属するかどうか
return findSet(x)==findSet(y);
}
void unite(ll x, ll y){
link(findSet(x),findSet(y));
}
void link(ll x, ll y){ //rankが大きい方の根に小さい方の根をつける。
if(rank[x]>rank[y]){
p[y]=x;
}
else{
p[x]=y;
if(rank[x]==rank[y]){
rank[y]++; //xとyの木のrankが同じであれば、統合するとrankが1増える。
}
}
sz[x]+=sz[y];
sz[y]=sz[x];
}
ll findSet(ll x){ //xが属する木の根の番号を返す
if(x!=p[x]){
p[x]=findSet(p[x]);
}
return p[x];
}
};
const ll mod=1e9+7;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(ll n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(ll n, ll k) {
return fact[n]*ifact[n-k];
}
} c(1000005);
int main(){
INT(N,M);
DisjointSet ds=DisjointSet(M+1);
V<ll> ans;
REP(i,N){
INT(k);
if(k==1){
INT(x);
x--;
if(!ds.same(x,M)){
ds.unite(x,M);
ans.push_back(i);
}
}
if(k==2){
INT(x,y);
x--; y--;
if(!ds.same(x,y)){
ds.unite(x,y);
ans.push_back(i);
}
}
}
mint p=2;
cout << p.pow(SZ(ans)) << " " << SZ(ans) << endl;
REP(i,SZ(ans)){
if(i) cout << " ";
cout << ans[i]+1;
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
class DSU {
public:
vector<int> uf, rank;
DSU(int n): uf(n), rank(n, 1) {iota(uf.begin(), uf.end(), 0);};
int find(int x){
if (uf[x] != x) uf[x] = find(uf[x]);
return uf[x];
}
bool unify(int x, int y){
int px = find(x), py = find(y);
bool ans = false;
if (px != py){
if (rank[px] > rank[py]) swap(px, py);
rank[py] += rank[px];
uf[px] = py;
ans = true;
}
return ans;
}
};
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
DSU dsu(m + 1);
vector<int> ans;
int x;
for (int i = 1; i <= n; i++) {
cin >> x;
int a = m, b = m;
cin >> a;
a--;
if (x == 2) {
cin >> b;
b--;
}
if (dsu.unify(a, b)) ans.push_back(i);
}
int t = 1;
for (int i = 0; i < ans.size(); i++) t = t * 2 % MOD;
cout << t << " " << ans.size() << endl;
for (int a: ans) cout << a << " ";
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef long double LD;
#define L first
#define smax(x, y) (x) = max((x), (y))
#define sz(x) ((int)(x).size())
#define R second
#define smin(x, y) (x) = min((x), (y))
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
const int maxn = 5e5 + 100;
const LL Mod = 1000 * 1000 * 1000 + 7;
int par[maxn];
bool mark[maxn];
int n, m;
vector<int> vec;
int root(int u) {
return par[u] < 0 ? u : par[u] = root(par[u]);
}
bool merge(int u) {
u = root(u);
if (mark[u])
return false;
return mark[u] = true;
}
bool merge(int u, int v) {
if ((u = root(u)) == (v = root(v)))
return false;
if (mark[u])
swap(u, v);
if (mark[u])
return false;
if (mark[v])
return mark[u] = true;
if (par[u] > par[v])
swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
memset(par, -1, sizeof par);
cin >> n >> m;
LL ans = 1;
for (int i = 0; i < n; i++) {
int c;
int fi, se;
cin >> c >> fi;
if (c == 1) {
fi--;
if (merge(fi)) {
ans = ans * 2 % Mod;
vec.PB(i);
}
}
else {
cin >> se;
fi--, se--;
if (merge(fi, se)) {
ans = ans * 2 % Mod;
vec.PB(i);
}
}
}
cout << ans << ' ' << sz(vec) << '\n';
for (auto idx: vec)
cout << idx + 1 << ' ';
cout << '\n';
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#define int long long
const int mod = 1e9 + 7;
struct DSU {
vector<int> par;
DSU(int n) {
par.resize(n + 1);
for (int i = 1; i <= n; i++) {
par[i] = i;
}
}
int find(int a) {
if (a == par[a])
return a;
return par[a] = find(par[a]); //set parent to every node in the path to the root (path compression)
}
bool join(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return false;
par[a] = b;
return true;
}
};
int binexp(int a, int b) {
int ress = 1;
while (b > 0) {
if (b & 1) {
ress = ress * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ress;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
DSU dsu(m + 1);
vector<int>ans;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
int v1;
cin >> v1;
int v2;
if (k == 2) {
cin >> v2;
}
else {
v2 = m + 1;
}
if (dsu.join(v1, v2)) {
ans.push_back(i + 1);
}
}
int t = binexp(2, ans.size());
cout << t << " " << ans.size() << endl;
for (auto it : ans) {
cout << it << " ";
}
cout << endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.