Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long inf = 1000000;
const int N = 300000;
vector<int> tree(2 * N + 4);
vector<int> ranks(2 * N + 4, 0);
char str[N + 4];
int find(int x) { return x == tree[x] ? x : tree[x] = find(tree[x]); }
void link(int x, int y) {
if (ranks[x] > ranks[y]) {
tree[y] = x;
} else {
tree[x] = y;
if (ranks[x] == ranks[y]) {
ranks[y]++;
}
}
}
long long sum = 0;
vector<vector<int>> graph(N);
vector<int> dists(2 * N);
bool visited[2 * N] = {false};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin.exceptions(cin.failbit);
int n, k;
cin >> n >> k;
cin >> str;
for (int i = 0; i < k; i++) {
tree[i] = i;
tree[i + N] = i + N;
dists[i] = 1;
dists[i + N] = 0;
}
for (int i = 0; i < k; i++) {
int len;
cin >> len;
for (int a = 0; a < len; a++) {
int e;
cin >> e;
e--;
graph[e].push_back(i);
}
}
for (int i = 0; i < n; i++) {
if (graph[i].empty()) {
cout << sum << "\n";
continue;
}
if (graph[i].size() == 1) {
if (str[i] == '0') {
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
sum++;
visited[graph[i][0] + N] = true;
dists[graph[i][0] + N] = inf;
} else {
int x = find(graph[i][0]);
int y = find(graph[i][0] + N);
sum -= min(dists[x], dists[y]);
dists[y] = inf;
sum += dists[x];
}
} else {
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
visited[graph[i][0] + N] = true;
dists[graph[i][0]] = inf;
} else {
int x = find(graph[i][0]);
int y = find(graph[i][0] + N);
sum -= min(dists[x], dists[y]);
dists[x] = inf;
sum += dists[y];
}
}
} else {
if (graph[i].size() > 2) {
cout << "?????\n";
}
int x1 = find(graph[i][0]);
int x2 = find(graph[i][0] + N);
int y1 = find(graph[i][1]);
int y2 = find(graph[i][1] + N);
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
visited[graph[i][0] + N] = true;
}
if (!visited[graph[i][1]]) {
visited[graph[i][1]] = true;
visited[graph[i][1] + N] = true;
}
sum -= min(dists[x1], dists[x2]);
sum -= min(dists[y1], dists[y2]);
if (str[i] == '0') {
if (x1 == y2) {
sum += min(dists[x1], dists[x2]);
} else if (dists[x1] == inf) {
tree[y2] = x1;
y2 = x1;
} else if (dists[y2] == inf) {
tree[x1] = y2;
x1 = y2;
} else {
tree[y2] = x1;
dists[x1] += dists[y2];
y2 = x1;
}
if (dists[x2] == inf) {
tree[y1] = x2;
y1 = x2;
} else if (dists[y1] == inf) {
tree[x2] = y1;
x2 = y1;
} else if (x2 != y1) {
tree[x2] = y1;
dists[y1] += dists[x2];
x2 = y1;
}
sum += min(dists[x1], dists[x2]);
} else {
if (x1 == y1) {
sum += min(dists[x1], dists[x2]);
} else if (dists[x1] == inf) {
tree[y1] = x1;
y1 = x1;
} else if (dists[y1] == inf) {
tree[x1] = y1;
x1 = y1;
} else {
tree[y1] = x1;
dists[x1] += dists[y1];
y1 = x1;
}
if (dists[x2] == inf) {
tree[y2] = x2;
y2 = x2;
} else if (dists[y2] == inf) {
tree[x2] = y2;
x2 = y2;
} else if (x2 != y2) {
tree[x2] = y2;
dists[y2] += dists[x2];
x2 = y2;
}
sum += min(dists[x1], dists[x2]);
}
}
cout << sum << "\n";
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
long long val[600010];
int have[600010][2], fa[600010];
char str[600010];
int n, k;
int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
int other(int u) { return u <= k ? u + k : u - k; }
void update(int u, long long x, long long y) {
ans -= min(val[u], val[other(u)]);
ans += min(x, y);
val[u] = x;
val[other(u)] = y;
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
while (c--) {
int a;
scanf("%d", &a);
if (have[a][0])
have[a][1] = i;
else
have[a][0] = i;
}
val[i] = 0, val[i + k] = 1;
fa[i] = i, fa[i + k] = i + k;
}
for (int i = 1; i <= n; i++) {
if (str[i] == '0') {
int x = find(have[i][0]), y = find(have[i][1]);
if (!y)
update(x, 1e7, val[other(x)]);
else {
long long uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = other(x);
fa[other(y)] = x;
update(x, val[x] + vy, val[other(x)] + uy);
}
} else {
int x = find(have[i][0]), y = find(have[i][1]);
if (!x) {
printf("%lld\n", ans);
continue;
}
if (!y)
update(x, val[x], 1e7);
else {
long long uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = x;
fa[other(y)] = other(x);
update(x, val[x] + uy, val[other(x)] + vy);
}
}
printf("%lld\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int w = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
w = (w << 3) + (w << 1) + ch - 48;
ch = getchar();
}
return w * f;
}
int n, m, f[600010], ans, siz[600010], st[600010], a[300010][2];
inline int find(int x) {
if (f[x] == x)
return f[x];
else
return f[x] = find(f[x]);
}
inline void merge(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx == fy) return;
f[fx] = fy;
siz[fy] += siz[fx];
return;
}
signed main() {
n = read();
m = read();
for (int i = 1; i <= n; i++) {
char ch;
cin >> ch;
st[i] = ch - '0';
}
for (int i = 1; i <= m; i++) {
int tot = read();
for (int j = 1; j <= tot; j++) {
int x = read();
if (!a[x][0])
a[x][0] = i;
else
a[x][1] = i;
}
}
m++;
for (int i = 0; i <= 2 * m; i++) {
f[i] = i;
if (i <= m) siz[i] = 1;
}
siz[0] = 0x3f3f3f3f;
siz[m] = 0;
for (int i = 1; i <= n; i++) {
int x = a[i][0];
int y = a[i][1];
if (!x) {
cout << ans << endl;
continue;
}
if (st[i] == 0 && find(x) != find(y + m)) {
ans -= min(siz[find(x)], siz[find(x + m)]);
ans -= min(siz[find(y)], siz[find(y + m)]);
merge(x, y + m);
merge(x + m, y);
ans += min(siz[find(x)], siz[find(x + m)]);
cout << ans << endl;
continue;
}
if (st[i] == 1 && find(x) != find(y)) {
ans -= min(siz[find(x)], siz[find(x + m)]);
ans -= min(siz[find(y)], siz[find(y + m)]);
merge(x, y);
merge(x + m, y + m);
ans += min(siz[find(x)], siz[find(x + m)]);
cout << ans << endl;
continue;
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, k, s, x, op[N][2];
char ch[N];
int p[N << 1], val[N << 1];
int find(int u) { return p[u] == u ? p[u] : p[u] = find(p[u]); }
void uni(int a, int b) {
if (a == b) return;
val[b] += val[a];
p[a] = b;
}
int main() {
scanf("%d%d%s", &n, &k, ch + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
op[x][bool(op[x][0])] = i;
}
}
for (int i = 1; i <= k; i++) val[i] = 1, p[i] = i, p[i + k] = i + k;
p[k + k + 1] = k + k + 1;
val[k + k + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (op[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (op[i][1]) {
if (find(op[i][0]) == find(op[i][1])) goto qwq;
ans -= min(val[find(op[i][1])], val[find(op[i][1] + k)]);
ans -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
uni(find(op[i][0]), find(op[i][1])),
uni(find(op[i][0] + k), find(op[i][1] + k));
ans += min(val[find(op[i][1])], val[find(op[i][1] + k)]);
} else {
ans -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
uni(find(op[i][0]), find(k + k + 1));
ans += val[find(op[i][0] + k)];
}
} else {
if (op[i][1]) {
if (find(op[i][0]) == find(op[i][1] + k)) goto qwq;
ans -= min(val[find(op[i][1])], val[find(op[i][1] + k)]);
ans -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
uni(find(op[i][0]), find(op[i][1] + k)),
uni(find(op[i][1]), find(op[i][0] + k));
ans += min(val[find(op[i][1] + k)], val[find(op[i][0] + k)]);
} else {
ans -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
uni(find(op[i][0] + k), find(k + k + 1));
ans += val[find(op[i][0])];
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 7;
using ll = long long;
string s;
int n, k;
int fa[maxn];
vector<int> st[maxn];
const int inf = 0x3f3f3f3f;
ll sz[maxn];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void Union(int x, int y) {
x = find(x), y = find(y);
if (x != y) {
fa[y] = x;
sz[x] += sz[y];
}
}
int main() {
for (int i = 0; i < maxn; ++i) fa[i] = i;
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
cin >> s;
for (int i = 0; i < k; ++i) {
sz[i] = 1;
int c;
cin >> c;
for (int j = 0, tmp; j < c; ++j) {
cin >> tmp;
--tmp;
st[tmp].push_back(i);
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (st[i].size() == 0)
assert(s[i] == '1');
else if (st[i].size() == 1) {
int a1 = st[i][0];
ans -= min(sz[find(a1)], sz[find(a1 + k)]);
if (s[i] == '1')
sz[find(a1)] += inf;
else
sz[find(a1 + k)] += inf;
ans += min(sz[find(a1)], sz[find(a1 + k)]);
} else {
int u = st[i][0], v = st[i][1];
ans -= min(sz[find(u)], sz[find(u + k)]);
if (find(v) != find(u) && find(u) != find(v + k)) {
ans -= min(sz[find(v)], sz[find(v + k)]);
}
if (s[i] == '0') {
Union(u + k, v);
Union(u, v + k);
} else {
Union(u, v);
Union(u + k, v + k);
}
ans += min(sz[find(u)], sz[find(u + k)]);
if (find(u) != find(v) && find(u) != find(v + k))
ans += min(sz[find(v)], sz[find(v + k)]);
}
cout << ans << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
int dp[maxn][2], fa[maxn * 2], cnt[maxn * 2], n, k;
char buf[maxn];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
int calc(int x) {
int y = (x <= k ? x + k : x - k);
x = Find(x), y = Find(y);
if (x == 0 || y == 0) return cnt[x + y];
return min(cnt[x], cnt[y]);
}
void Unite(int x, int y) {
x = Find(x), y = Find(y);
if (x == 0) swap(x, y);
fa[x] = y;
if (y) cnt[y] += cnt[x];
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", buf + 1);
for (int i = 1; i <= k; ++i) {
int q;
scanf("%d", &q);
while (q--) {
int num;
scanf("%d", &num);
if (dp[num][0])
dp[num][1] = i;
else
dp[num][0] = i;
}
fa[i] = i, fa[i + k] = i + k;
cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (dp[i][1] == 0) {
int x = dp[i][0];
if (x) {
ans -= calc(x);
if (buf[i] == '0')
fa[Find(x)] = 0;
else
fa[Find(x + k)] = 0;
ans += calc(x);
}
} else {
int x = dp[i][0], y = dp[i][1];
if (buf[i] == '1') {
if (Find(x) != Find(y)) {
ans -= calc(x), ans -= calc(y);
Unite(x, y);
Unite(x + k, y + k);
ans += calc(x);
}
} else {
if (Find(x) != Find(y + k)) {
ans -= calc(x);
ans -= calc(y + k);
Unite(x, y + k);
Unite(y, x + k);
ans += calc(x);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk;
fa[kk + kk + 1] = kk + kk + 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
} else {
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(k[i][0], k[i][1] + kk), uni(k[i][1], k[i][0] + kk);
ans += min(val[find(k[i][1] + kk)], val[find(k[i][0] + kk)]);
} else {
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(k[i][0] + kk, kk + kk + 1);
ans += val[find(k[i][0])];
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9 + 5;
const int N = 600010;
class DSU {
public:
int p[N], cost[N];
int n;
int ans = 0;
DSU(int _n) : n(_n) {
for (int i = 0; i < n; i++) {
cost[2 * i + 1] = 1;
cost[2 * i] = 0;
}
cost[2 * n] = inf;
cost[2 * n + 1] = 0;
for (int i = 0; i < 2 * n + 2; i++) {
p[i] = i;
}
}
int find(int x) { return (x == p[x] ? x : (p[x] = find(p[x]))); }
void unite(int x, int y, bool same) {
assert(0 <= 2 * x && 2 * x + 1 < 2 * n + 2);
assert(0 <= 2 * y && 2 * y + 1 < 2 * n + 2);
int x_true = find(2 * x + 1), y_true = find(2 * y + 1);
int x_false = find(2 * x), y_false = find(2 * y);
if (same) {
if (x_true != y_true) {
assert(x_false != y_false);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
cost[y_true] += cost[x_true];
cost[y_false] += cost[x_false];
p[x_true] = y_true;
p[x_false] = y_false;
ans += min(cost[y_true], cost[y_false]);
} else {
return;
}
} else {
if (x_true != y_false) {
assert(x_false != y_true);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
cost[y_true] += cost[x_false];
cost[y_false] += cost[x_true];
p[x_true] = y_false;
p[x_false] = y_true;
ans += min(cost[y_true], cost[y_false]);
} else {
return;
}
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<vector<int>> has(n);
for (int i = 0; i < k; i++) {
int size;
cin >> size;
while (size--) {
int x;
cin >> x;
--x;
has[x].push_back(i);
}
}
DSU dsu(k);
int dummy = k;
for (int i = 0; i < n; i++) {
if ((int)has[i].size() == 2) {
dsu.unite(has[i][0], has[i][1], s[i] == '1');
} else if ((int)has[i].size() == 1) {
dsu.unite(has[i].back(), dummy, s[i] == '0');
} else {
assert(s[i] == '1');
}
cout << dsu.ans << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 10;
int f[maxn], n, k, w[maxn];
vector<int> v[maxn];
int Find(int x) { return f[x] == x ? x : f[x] = Find(f[x]); }
int ask(int x) {
int y = (x <= k) ? x + k : x - k;
x = Find(x);
y = Find(y);
if (x == 0)
return w[y];
else if (y == 0)
return w[x];
else
return min(w[x], w[y]);
}
void Merge(int x, int y) {
x = Find(x), y = Find(y);
if (y == 0) swap(x, y);
f[y] = x;
if (x != 0) w[x] += w[y];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
string s;
cin >> s;
for (long long i = 1; i <= (long long)k; ++i) {
int x;
cin >> x;
for (long long j = 1; j <= (long long)x; ++j) {
int tem;
cin >> tem;
v[tem].push_back(i);
}
f[i] = i;
f[i + k] = i + k;
w[i + k] = 1;
}
int ans = 0;
for (long long i = 1; i <= (long long)n; ++i) {
if (v[i].size() == 1) {
int x = v[i][0];
if (x) {
ans -= ask(x);
if (s[i - 1] == '1')
f[Find(x + k)] = 0;
else
f[Find(x)] = 0;
ans += ask(x);
}
} else {
int x = 0, y = 0;
if (v[i].size() == 2) {
x = v[i][0], y = v[i][1];
}
if (s[i - 1] == '1') {
if (Find(x) != Find(y)) {
ans -= (ask(x) + ask(y));
Merge(x, y);
Merge(x + k, y + k);
ans += ask(x);
}
} else {
if (Find(x + k) != Find(y)) {
ans -= (ask(x) + ask(y));
Merge(x + k, y);
Merge(x, y + k);
ans += ask(x);
}
}
}
cout << ans << "\n";
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct DS {
int rank, cost, parent;
bool forced;
} ds[4 * 300100];
int ans;
char status[300100];
vector<int> subs[300100];
int neg(int n) { return n + 300100; }
int find(int n) {
if (n == ds[n].parent) return n;
return ds[n].parent = find(ds[n].parent);
}
void join(int n1, int n2) {
int s1 = find(n1), s2 = find(n2);
if (s1 != s2) {
if (ds[s1].rank < ds[s2].rank) swap(s1, s2);
ds[s2].parent = s1;
ds[s1].cost += ds[s2].cost;
ds[s1].rank += ds[s2].rank;
ds[s1].forced |= ds[s2].forced;
}
}
int main() {
int n, k, qtd, v, s1, s2, ns1, ns2, mini;
scanf("%d %d", &n, &k);
getchar();
scanf("%s", status);
for (int i = 0; i < k; i++) {
scanf("%d", &qtd);
for (int j = 0; j < qtd; j++) {
scanf("%d", &v);
subs[v].emplace_back(i);
}
ds[i].rank = 1;
ds[i].cost = 1;
ds[i].parent = i;
ds[i].forced = false;
ds[neg(i)].rank = 1;
ds[neg(i)].cost = 0;
ds[neg(i)].parent = neg(i);
ds[neg(i)].forced = false;
}
for (int i = 1; i <= n; i++) {
if (status[i - 1] == '1') {
if (subs[i].size() == 2) {
s1 = find(subs[i][0]);
s2 = find(subs[i][1]);
ns1 = find(neg(subs[i][0]));
ns2 = find(neg(subs[i][1]));
mini = -1;
if (ds[s1].forced) ans -= ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans -= ds[ns1].cost, mini = 0;
if (mini == -1) {
mini = min(ds[s1].cost, ds[ns1].cost);
ans -= mini;
}
mini = -1;
if (ds[s2].forced and s1 != s2 and ns1 != s2)
ans -= ds[s2].cost, mini = 0;
if (ds[ns2].forced and s1 != ns2 and ns1 != ns2)
ans -= ds[ns2].cost, mini = 0;
if (mini == -1 and s1 != s2 and s1 != ns2 and ns1 != s2 and
ns1 != ns2) {
mini = min(ds[s2].cost, ds[ns2].cost);
ans -= mini;
}
join(s1, s2);
join(ns1, ns2);
s1 = find(subs[i][0]);
s2 = find(subs[i][1]);
ns1 = find(neg(subs[i][0]));
ns2 = find(neg(subs[i][1]));
mini = -1;
if (ds[s1].forced) ans += ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans += ds[ns1].cost, mini = 0;
if (mini == -1) mini = min(ds[s1].cost, ds[ns1].cost);
ans += mini;
} else {
if (subs[i].size() == 1) {
s1 = find(subs[i][0]);
ns1 = find(neg(subs[i][0]));
if (not ds[ns1].forced) {
ans -= min(ds[s1].cost, ds[ns1].cost);
ds[ns1].forced = true;
ans += ds[ns1].cost;
}
}
}
} else {
if (subs[i].size() == 2) {
s1 = find(subs[i][0]);
ns2 = find(neg(subs[i][1]));
ns1 = find(neg(subs[i][0]));
s2 = find(subs[i][1]);
mini = -1;
if (ds[s1].forced) ans -= ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans -= ds[ns1].cost, mini = 0;
if (mini == -1) {
mini = min(ds[s1].cost, ds[ns1].cost);
ans -= mini;
}
mini = -1;
if (ds[s2].forced and s1 != s2 and ns1 != s2)
ans -= ds[s2].cost, mini = 0;
if (ds[ns2].forced and s1 != ns2 and ns1 != ns2)
ans -= ds[ns2].cost, mini = 0;
if (mini == -1 and s1 != s2 and s1 != ns2 and ns1 != s2 and
ns1 != ns2) {
mini = min(ds[s2].cost, ds[ns2].cost);
ans -= mini;
}
join(s1, ns2);
join(ns1, s2);
s1 = find(subs[i][0]);
ns2 = find(neg(subs[i][1]));
ns1 = find(neg(subs[i][0]));
s2 = find(subs[i][1]);
mini = -1;
if (ds[s1].forced) ans += ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans += ds[ns1].cost, mini = 0;
if (mini == -1) mini = min(ds[s1].cost, ds[ns1].cost);
ans += mini;
} else {
if (subs[i].size() == 1) {
s1 = find(subs[i][0]);
ns1 = find(neg(subs[i][0]));
if (not ds[s1].forced) {
ans -= min(ds[s1].cost, ds[ns1].cost);
ds[s1].forced = true;
ans += ds[s1].cost;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
cur_ans -= get_vrt_ans(p_v);
cur_ans -= get_vrt_ans(p_u);
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[p_u] != 2) {
if (r_st[p_v] == 2) {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
} else {
assert(r_st[p_v] == (r_st[p_u] ^ p_xor[p_u]));
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()), p(n), sz(n, 1), p_xor(n), r_st(r_st), ans(n) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void set_rst(int v, int st) {
int p_v = dsu_get(v);
if (r_st[p_v] != 2) {
assert(r_st[p_v] == (st ^ p_xor[v]));
return;
}
cur_ans -= get_vrt_ans(p_v);
r_st[p_v] = st ^ p_xor[v];
cur_ans += get_vrt_ans(p_v);
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(vector<int>(k, 2));
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.set_rst(v_mask[i][0], mask_st[v_mask[i][0]]);
}
cout << dsu.get_ans() << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MX = 1e6, INF = 1e9 + 10;
vector<long long> v[MX];
long long par[MX], s0[MX], s1[MX], col[MX], ans = 0;
pair<long long, long long> GETPAR(long long v) {
if (par[v] == v) return {v, 0};
pair<long long, long long> ans = GETPAR(par[v]);
return {par[v] = ans.first, col[v] ^= ans.second};
}
void MERGE(long long a, long long b, long long stt) {
pair<long long, long long> u = GETPAR(a), v = GETPAR(b);
if (u.first == v.first) return;
ans -= min(s0[u.first], s1[u.first]) + min(s0[v.first], s1[v.first]);
if (stt ^ u.second ^ v.second) {
swap(s0[v.first], s1[v.first]);
col[v.first] = 1;
}
par[v.first] = u.first;
s0[u.first] += s0[v.first];
s1[u.first] += s1[v.first];
ans += min(s0[u.first], s1[u.first]);
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, k;
string s;
cin >> n >> k >> s;
for (long long i = 0; i < k; i++) {
s0[i] = 1;
par[i] = i;
long long l;
cin >> l;
while (l--) {
long long x;
cin >> x;
x--;
v[x].push_back(i);
}
}
for (long long i = 0; i < n; i++) {
if (v[i].size() == 1) {
par[i + k] = i + k;
s0[i + k] = INF;
MERGE(v[i][0], i + k, s[i] == '0');
}
if (v[i].size() == 2) MERGE(v[i][0], v[i][1], s[i] == '0');
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 300009;
const long long MOD = 119 << 23 | 1;
class {
public:
vector<int> V[MAXN];
int fa[MAXN * 2], cnt[MAXN * 2];
int n, m;
int findfa(int x) {
if (fa[x] == x) return x;
return fa[x] = findfa(fa[x]);
}
void merge(int x, int y) {
x = findfa(x);
y = findfa(y);
if (x == y) return;
if (x > y) swap(x, y);
fa[y] = x;
cnt[x] += cnt[y];
}
void solve() {
cin >> n >> m;
string s;
cin >> s;
for (int i = 1; i <= m; ++i) {
int c;
cin >> c;
for (int j = 0; j < c; ++j) {
int v;
cin >> v;
V[v].push_back(i);
}
fa[i] = i;
fa[i + m] = i + m;
cnt[i + m] = 1;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
bool on = s[i - 1] == '1';
if (V[i].size() == 1) {
int p = V[i][0];
if (on) {
ans -= cost(p);
merge(p + m, 0);
ans += cost(p);
} else {
ans -= cost(p);
merge(p, 0);
ans += cost(p);
}
} else if (V[i].size() == 2) {
int p0 = V[i][0], p1 = V[i][1];
if (on) {
if (findfa(p0) != findfa(p1)) {
ans -= cost(p0);
ans -= cost(p1);
merge(p0, p1);
merge(p0 + m, p1 + m);
ans += cost(p0);
} else {
;
}
} else {
if (findfa(p0) != findfa(p1 + m)) {
ans -= cost(p0);
ans -= cost(p1);
merge(p0, p1 + m);
merge(p0 + m, p1);
ans += cost(p0);
} else {
;
}
}
}
cout << ans << '\n';
}
}
int cost(int x) {
int y;
if (x <= m) {
y = m + x;
} else {
y = x - m;
}
x = findfa(x);
y = findfa(y);
if (x == 0)
return cnt[y];
else if (y == 0)
return cnt[x];
return min(cnt[x], cnt[y]);
}
} NSPACE;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
NSPACE.solve();
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
const int MOD = 998857459;
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
const int maxm = 5e6 + 10;
int n, k, op[maxn][2], p[maxn << 2];
long long val[maxn << 2];
char str[maxn];
int find(int first) {
return p[first] == first ? p[first] : p[first] = find(p[first]);
}
void merge(int first, int second) {
if (first == second) return;
val[second] += val[first];
p[first] = second;
}
int main() {
cin >> n >> k;
scanf("%s", str + 1);
for (int i = 1, sz, a; i <= k; ++i) {
scanf("%d", &sz);
while (sz--) {
scanf("%d", &a);
if (!op[a][0])
op[a][0] = i;
else
op[a][1] = i;
}
}
for (int i = 1; i <= k; ++i) p[i] = i, p[i + k] = i + k, val[i] = 1;
p[k * 2 + 1] = k * 2 + 1;
val[k * 2 + 1] = INF;
int now = 0;
for (int i = 1; i <= n; ++i) {
if (!op[i][0]) goto out;
if (str[i] == '1') {
if (op[i][1]) {
if (find(op[i][0]) == find(op[i][1])) goto out;
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
now -= min(val[find(op[i][1])], val[find(op[i][1] + k)]);
merge(find(op[i][0]), find(op[i][1]));
merge(find(op[i][0] + k), find(op[i][1] + k));
now += min(val[find(op[i][1])], val[find(op[i][1] + k)]);
} else {
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
merge(find(op[i][0]), find(k * 2 + 1));
now += val[find(op[i][0] + k)];
}
} else {
if (op[i][1]) {
if (find(op[i][0]) == find(op[i][1] + k)) goto out;
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
now -= min(val[find(op[i][1])], val[find(op[i][1] + k)]);
merge(find(op[i][0]), find(op[i][1] + k));
merge(find(op[i][1]), find(op[i][0] + k));
now += min(val[find(op[i][1] + k)], val[find(op[i][0] + k)]);
} else {
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
merge(find(op[i][0] + k), find(k * 2 + 1));
now += val[find(op[i][0])];
}
}
out:
printf("%d\n", now);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
class DSU {
private:
vector<int> comp, siz;
vector<int> tp;
vector<int> onc, offc;
public:
DSU(int n) : comp(n), siz(n, 1), tp(n, 0), onc(n, 0), offc(n, 1) {
for (int i = 0; i < n; ++i) comp[i] = i;
offc[n - 1] = INF;
}
pair<int, int> getc(int i) {
if (comp[i] != i) {
auto sub = getc(comp[i]);
comp[i] = sub.first;
tp[i] ^= sub.second;
}
return {comp[i], tp[i]};
}
int join(int a, int b, int t) {
int at, bt;
tie(a, at) = getc(a);
tie(b, bt) = getc(b);
t ^= at ^ bt;
if (a == b) return 0;
int add = -(min(onc[a], offc[a]) + min(onc[b], offc[b]));
siz[a] += siz[b];
comp[b] = a;
tp[b] = t;
if (tp[b]) {
onc[a] += offc[b];
offc[a] += onc[b];
} else {
onc[a] += onc[b];
offc[a] += offc[b];
}
add += min(onc[a], offc[a]);
return add;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<pair<int, int>> lamps(n, {k, k});
string str;
cin >> str;
for (int j = 0; j < k; ++j) {
int c;
cin >> c;
for (int i = 0; i < c; ++i) {
int x;
cin >> x;
--x;
if (lamps[x].first == k)
lamps[x].first = j;
else if (lamps[x].second == k)
lamps[x].second = j;
}
}
DSU dsu(k + 1);
int res = 0;
for (int i = 0; i < n; ++i) {
res += dsu.join(lamps[i].first, lamps[i].second, str[i] == '0');
cout << res << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk;
fa[kk + kk + 1] = kk + kk + 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0]), find(k[i][1])),
uni(find(k[i][0] + kk), find(k[i][1] + kk));
ans += min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
} else {
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0]), find(kk + kk + 1));
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0]), find(k[i][1] + kk)),
uni(find(k[i][1]), find(k[i][0] + kk));
ans += min(val[find(k[i][1] + kk)], val[find(k[i][0] + kk)]);
} else {
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0] + kk), find(kk + kk + 1));
ans += val[find(k[i][0])];
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, const T& b) {
return a > b ? a = b, true : false;
}
template <class T>
bool umax(T& a, const T& b) {
return a < b ? a = b, true : false;
}
template <long long sz>
using tut = array<long long, sz>;
const long long N = 3e5 + 5;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long double Pi = acos(-1);
const long long MX = 2e16;
long long n, m, k, t, q, ans, res, a[N];
long long in[N][2], cc[N], cnt[N][2];
long long par[N], sz[N];
vector<long long> edges[N];
string s;
void dfs(long long u) {
cnt[u][cc[u]]++;
for (auto x : edges[u]) {
if (in[x][1] == -1) continue;
long long to = in[x][0] + in[x][1] - u;
if (~cc[to]) continue;
cc[to] = cc[u] ^ (s[x] == '0');
dfs(to);
}
}
long long f(long long x) { return (par[x] == x ? x : par[x] = f(par[x])); }
void add(long long a, long long s0, long long s1) {
a = f(a);
res -= min(cnt[a][0], cnt[a][1]);
cnt[a][0] += s0, cnt[a][1] += s1;
res += min(cnt[a][0], cnt[a][1]);
}
void merge(long long a, long long b) {
a = f(a), b = f(b);
if (a == b) return;
if (sz[b] > sz[a]) swap(a, b);
add(a, cnt[b][0], cnt[b][1]);
add(b, -cnt[b][0], -cnt[b][1]);
sz[a] += sz[b], par[b] = a;
}
void solve(long long t_case) {
cin >> n >> k >> s;
s = '$' + s;
memset(in, -1, sizeof in), memset(cc, -1, sizeof cc);
for (long long i = 0; i < k; i++) {
sz[i] = 1, par[i] = i;
long long c;
cin >> c;
for (long long j = 0; j < c; j++) {
long long x;
cin >> x, edges[i].push_back(x);
if (~in[x][0])
in[x][1] = i;
else
in[x][0] = i;
}
}
for (long long i = 0; i < k; i++) {
if (~cc[i]) continue;
cc[i] = 0, dfs(i);
}
for (long long i = 1; i <= n; i++) {
long long c0 = in[i][0], c1 = in[i][1];
if (~c0 && c1 == -1) {
if (s[i] == '0') {
if (cc[c0])
add(c0, mod, 0);
else
add(c0, 0, mod);
} else {
if (cc[c0])
add(c0, 0, mod);
else
add(c0, mod, 0);
}
}
if (~c0 && ~c1) {
merge(c0, c1);
}
cout << res << "\n";
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (0) {
long long t;
cin >> t;
for (long long t_case = 1; t_case <= t; t_case++) solve(t_case);
} else
solve(1);
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 7;
int n, k;
string s;
int l[maxn][2], r[maxn], cnt[maxn];
int getroot(int x) { return r[x] == x ? x : r[x] = getroot(r[x]); }
int calc(int x) {
int y = x <= k ? x + k : x - k;
x = getroot(x);
y = getroot(y);
if (x == 0 || y == 0) {
return cnt[x + y];
}
return min(cnt[x], cnt[y]);
}
void fmerge(int x, int y) {
x = getroot(x);
y = getroot(y);
if (y == 0) {
swap(x, y);
}
r[y] = x;
cnt[x] += cnt[y];
}
int main() {
cin >> n >> k;
cin >> s;
for (int i = 1; i <= k; i++) {
int c;
cin >> c;
for (int j = 0; j < c; j++) {
int x;
cin >> x;
if (l[x][0] == 0)
l[x][0] = i;
else
l[x][1] = i;
}
r[i] = i;
r[i + k] = i + k;
cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= calc(x);
if (s[i - 1] == '1') {
r[getroot(x + k)] = 0;
} else {
r[getroot(x)] = 0;
}
ans += calc(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (getroot(x) != getroot(y)) {
ans -= calc(x);
ans -= calc(y);
fmerge(x, y);
fmerge(x + k, y + k);
ans += calc(x);
}
} else {
if (getroot(x + k) != getroot(y)) {
ans -= calc(x);
ans -= calc(y);
fmerge(x + k, y);
fmerge(x, y + k);
ans += calc(x);
}
}
}
cout << ans << endl;
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
vector<int> g[maxn];
int f[maxn], d[maxn], L[maxn], R[maxn], fix[maxn], gt[maxn], ans = 0;
int n, k;
char str[maxn];
int calc(int x) {
if (fix[x])
return gt[x] ? R[x] : L[x];
else
return min(L[x], R[x]);
}
int find(int x) {
if (f[x] == x) return x;
int t = f[x];
f[x] = find(f[x]);
d[x] ^= d[t];
return f[x];
}
void merge(int u, int v, int dd) {
int fa = find(u), fb = find(v);
if (fa != fb) {
ans -= calc(fa) + calc(fb);
f[fa] = fb;
d[fa] = d[u] ^ d[v] ^ dd;
L[fb] += d[fa] ? R[fa] : L[fa];
R[fb] += d[fa] ? L[fa] : R[fa];
fix[fb] |= fix[fa];
if (fix[fa]) gt[fb] = gt[fa] ^ d[fa];
ans += calc(fb);
}
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
for (int i = 1; i <= k; i++) {
int c, x;
scanf("%d", &c);
for (int j = 1; j <= c; j++) scanf("%d", &x), g[x].push_back(i);
}
for (int i = 1; i <= k; i++) f[i] = i, d[i] = 0, L[i] = 1;
for (int i = 1; i <= n; i++) {
if (str[i] == '0') {
if (g[i].size() == 1) {
int x = g[i][0];
int c = find(x);
ans -= calc(c);
fix[c] = 1;
gt[c] = d[x];
ans += calc(c);
} else if (g[i].size() == 2) {
int x = g[i][0], y = g[i][1];
merge(x, y, 1);
}
} else {
if (g[i].size() == 1) {
int x = g[i][0];
int c = find(x);
ans -= calc(c);
fix[c] = 1;
gt[c] = d[x] ^ 1;
ans += calc(c);
} else if (g[i].size() == 2) {
int x = g[i][0], y = g[i][1];
merge(x, y, 0);
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int maxn = 6e5 + 5;
int fa[maxn], val[maxn], ans;
vector<int> V[maxn];
int n, k;
string s;
void init() {
for (int i = 0; i < maxn; ++i) fa[i] = i;
}
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void merge(int x, int y) {
int fx = getfa(x), fy = getfa(y);
if (fx != fy) {
fa[fx] = fy;
val[fy] += val[fx];
}
}
int va(int x) { return min(val[getfa(x)], val[getfa(x + k)]); }
int main() {
ios::sync_with_stdio(false);
cin >> n >> k >> s;
for (int i = 1; i <= k; ++i) {
int c;
cin >> c;
for (int j = 1; j <= c; ++j) {
int tmp;
cin >> tmp;
V[tmp].push_back(i);
}
}
for (int i = 1; i <= 2 * k; ++i) fa[i] = i, val[i] = i > k;
fa[2 * k + 1] = 2 * k + 1, val[2 * k + 1] = 0x3f3f3f3f;
for (int i = 1; i <= n; ++i) {
if (V[i].size() == 1) {
int x = V[i][0] + (s[i - 1] == '1') * k;
ans -= va(V[i][0]);
merge(x, k * 2 + 1);
ans += va(V[i][0]);
} else if (V[i].size() == 2) {
int x = V[i][0], y = V[i][1];
if (s[i - 1] == '0') {
if (getfa(x) != getfa(y + k)) {
ans -= va(x) + va(y);
merge(x, y + k);
merge(x + k, y);
ans += va(x);
}
} else {
if (getfa(x) != getfa(y)) {
ans -= va(x) + va(y);
merge(x, y);
merge(x + k, y + k);
ans += va(x);
}
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
enum class LampConstraint { Distinct, Same };
enum class LampSetState { On, Off, PhantomA, PhantomB };
bool isLampSetStateKnown(LampSetState st) {
return st == LampSetState::On || st == LampSetState::Off;
}
typedef struct Edge {
int nodeId;
LampConstraint cons;
Edge(int nodeId_, LampConstraint cons_) : nodeId(nodeId_), cons(cons_) {}
} Edge;
bool compareEdges(const Edge &a, const Edge &b) {
if (a.nodeId < b.nodeId) return true;
if (a.nodeId > b.nodeId) return true;
return a.cons < b.cons;
}
typedef struct LampSetInfo {
LampSetState state;
int ownerId;
int componentSize;
int onColorSize;
int offColorSize;
explicit LampSetInfo(int id)
: state(LampSetState::PhantomA),
ownerId(id),
componentSize(1),
onColorSize(1),
offColorSize(0){};
} LampSetInfo;
typedef std::set<Edge, decltype(compareEdges) *> Edges;
int componentOnBestSize(std::vector<LampSetInfo> &nodes, int nodeId) {
LampSetInfo &s = nodes[nodeId];
int ownerId = s.ownerId;
if (s.state == LampSetState::On || s.state == LampSetState::Off) {
return nodes[ownerId].onColorSize;
} else {
return std::min(nodes[ownerId].onColorSize, nodes[ownerId].offColorSize);
}
}
LampSetState deducedState(const LampConstraint &cons, LampSetState st) {
switch (cons) {
case LampConstraint::Same:
return st;
case LampConstraint::Distinct:
switch (st) {
case LampSetState::On:
return LampSetState::Off;
case LampSetState::Off:
return LampSetState::On;
case LampSetState::PhantomA:
return LampSetState::PhantomB;
case LampSetState::PhantomB:
return LampSetState::PhantomA;
}
}
}
void propagateLampState(const std::vector<Edges> &graph,
std::vector<LampSetInfo> &nodes, int startNodeId,
LampSetState learnedState) {
std::set<int> visited;
std::stack<std::pair<int, LampSetState>> nodeIds;
nodeIds.push(std::make_pair(startNodeId, learnedState));
int onCount = 0;
while (!nodeIds.empty()) {
std::pair<int, LampSetState> currentNode = nodeIds.top();
nodeIds.pop();
if (visited.find(currentNode.first) != visited.end()) continue;
nodes[currentNode.first].state = currentNode.second;
onCount += ((currentNode.second == LampSetState::On ||
currentNode.second == LampSetState::PhantomA)
? 1
: 0);
visited.insert(currentNode.first);
for (const Edge &e : graph[currentNode.first]) {
if (visited.find(e.nodeId) == visited.end())
nodeIds.push(
std::make_pair(e.nodeId, deducedState(e.cons, currentNode.second)));
}
}
int ownerId = nodes[startNodeId].ownerId;
nodes[ownerId].onColorSize = onCount;
nodes[ownerId].offColorSize = nodes[ownerId].componentSize - onCount;
}
void switchOwner(std::vector<Edges> &graph, std::vector<LampSetInfo> &nodes,
int startNodeId, int newOwner) {
std::set<int> visited;
std::stack<int> nodeIds;
nodeIds.push(startNodeId);
while (!nodeIds.empty()) {
int currentNode = nodeIds.top();
nodeIds.pop();
if (visited.find(currentNode) != visited.end()) continue;
visited.insert(currentNode);
nodes[currentNode].ownerId = newOwner;
for (auto &e : graph[currentNode]) {
if (visited.find(e.nodeId) == visited.end()) nodeIds.push(e.nodeId);
}
}
}
int main() {
int n, k;
long long result = 0;
std::string lamps;
std::vector<LampSetInfo> nodes;
std::cin >> n >> k;
std::cin >> lamps;
std::vector<std::vector<int>> setsForLamp(n);
std::vector<Edges> graph(k, Edges(compareEdges));
for (int i = 0; i < k; i++) {
int c;
nodes.push_back(LampSetInfo(i));
std::cin >> c;
for (int j = 0; j < c; j++) {
int a;
std::cin >> a;
setsForLamp[a - 1].push_back(i);
}
}
for (int i = 0; i < n; i++) {
LampSetState learnedState;
LampConstraint cons;
int startNode;
int a, b, ownerA, ownerB;
switch (setsForLamp[i].size()) {
case 0:
if (lamps[i] != '1')
throw std::runtime_error("Conflicting enforced states of lamps!");
break;
case 1:
startNode = setsForLamp[i][0];
learnedState = (lamps[i] == '1' ? LampSetState::Off : LampSetState::On);
if (!isLampSetStateKnown(nodes[startNode].state)) {
result -= componentOnBestSize(nodes, startNode);
propagateLampState(graph, nodes, startNode, learnedState);
result += componentOnBestSize(nodes, startNode);
} else {
if (learnedState != nodes[startNode].state)
throw std::runtime_error("Conflicting enforced states of lamps!");
}
break;
case 2:
a = setsForLamp[i][0];
b = setsForLamp[i][1];
ownerA = nodes[a].ownerId;
ownerB = nodes[b].ownerId;
cons =
(lamps[i] == '1' ? LampConstraint::Same : LampConstraint::Distinct);
if (nodes[ownerA].componentSize > nodes[ownerB].componentSize) {
std::swap(a, b);
std::swap(ownerA, ownerB);
}
if (ownerA != ownerB) {
result -= componentOnBestSize(nodes, ownerA);
result -= componentOnBestSize(nodes, ownerB);
if (isLampSetStateKnown(nodes[a].state) &&
!isLampSetStateKnown(nodes[b].state)) {
propagateLampState(graph, nodes, b,
deducedState(cons, nodes[a].state));
} else if (isLampSetStateKnown(nodes[b].state) &&
!isLampSetStateKnown(nodes[a].state)) {
propagateLampState(graph, nodes, a,
deducedState(cons, nodes[b].state));
} else if (!isLampSetStateKnown(nodes[b].state) &&
!isLampSetStateKnown(nodes[a].state)) {
propagateLampState(graph, nodes, a,
deducedState(cons, nodes[b].state));
}
nodes[ownerB].onColorSize += nodes[ownerA].onColorSize;
nodes[ownerB].offColorSize += nodes[ownerA].offColorSize;
nodes[ownerB].componentSize += nodes[ownerA].componentSize;
result += componentOnBestSize(nodes, ownerB);
switchOwner(graph, nodes, a, ownerB);
}
graph[a].insert(Edge(b, cons));
graph[b].insert(Edge(a, cons));
break;
default:
throw std::runtime_error("Malformed input data!");
}
std::cout << result << std::endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
int n, k, m, c, vas, vvas, cc = 1000000, kl;
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
vector<vector<int>> la(n, vector<int>(0));
vector<perem> pr(k);
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
int a = la[i][0];
bool d = s[i] == '1' ? 1 : 0;
for (;; a = pr[a].p) {
d ^= pr[a].q;
if (pr[a].p == -1) break;
}
if (la[i].size() == 1) {
if (!d) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
int b = la[i][1];
for (;; b = pr[b].p) {
d ^= pr[b].q;
if (pr[b].p == -1) break;
}
if (a != b) {
if (!d) {
if (pr[a].l > pr[b].l) swap(a, b);
vas += pr[a].l;
pr[a].q ^= 1;
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
cout << vvas + vas << endl;
vvas += vas;
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct DS {
int n;
vector<int> s;
vector<int> count;
DS(int n) : n(n), s(n, -1), count(n, 0) {}
int find(int i) { return s[i] < 0 ? i : (s[i] = find(s[i])); }
int counted(int i) { return count[find(i)]; }
void mark(int i, int x) { count[find(i)] += x; }
int onion(int a, int b) {
a = find(a);
b = find(b);
if (a == b) {
return 0;
}
if (s[a] == s[b]) s[a]--;
if (s[a] <= s[b]) {
s[b] = a;
count[a] += count[b];
} else {
s[a] = b;
count[b] += count[a];
}
return 1;
}
};
int main() {
int n, k;
cin >> n >> k;
vector<int> l(n);
string L;
cin >> L;
for (int i = 0; i < n; i++) {
l[i] = L[i] - '0';
}
vector<vector<int> > s(n);
for (int i = 0; i < k; i++) {
int c;
cin >> c;
while (c--) {
int l;
cin >> l;
l--;
s[l].push_back(i);
}
}
DS ds(2 * k + 1);
for (int i = 0; i < k; i++) ds.mark(i, 1);
int sum = 0;
vector<int> contrib(2 * k + 1, 0);
auto reset = [&](int i) {
sum -= contrib[ds.find(i)];
contrib[ds.find(i)] = 0;
};
auto set = [&](int i, int k) {
contrib[ds.find(i)] = k;
sum += contrib[ds.find(i)];
};
for (int i = 0; i < n; i++) {
if (false) {
cout << i << ":";
for (int p : s[i]) cout << " " << p;
cout << endl;
}
if (s[i].size() == 0) {
} else if (s[i].size() == 1) {
reset(2 * k);
reset(s[i][0] + k);
reset(s[i][0]);
if (l[i] == 1) {
ds.onion(2 * k, s[i][0] + k);
} else {
ds.onion(2 * k, s[i][0]);
}
set(2 * k, ds.counted(2 * k));
} else {
reset(s[i][0]);
reset(s[i][1]);
reset(s[i][0] + k);
reset(s[i][1] + k);
if (l[i] == 1) {
ds.onion(s[i][0], s[i][1]);
ds.onion(s[i][0] + k, s[i][1] + k);
} else {
ds.onion(s[i][0] + k, s[i][1]);
ds.onion(s[i][0], s[i][1] + k);
}
int A = ds.find(s[i][0]);
int B = ds.find(s[i][0] + k);
if (A == ds.find(2 * k)) {
set(A, ds.counted(A));
} else if (B == ds.find(2 * k)) {
set(B, ds.counted(B));
} else if (ds.counted(A) < ds.counted(B)) {
set(A, ds.counted(A));
} else {
set(B, ds.counted(B));
}
}
cout << sum << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int MAXN = 0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
void file() {}
const int N = 3e5 + 5;
int fat[N * 2], Size[N * 2], n, k;
vector<int> vec[N];
int find(int x) { return fat[x] == x ? x : fat[x] = find(fat[x]); }
int calc(int x) {
int y = (x <= k ? x + k : x - k);
x = find(x);
y = find(y);
if (x == 0 || y == 0) return Size[x + y];
return min(Size[x], Size[y]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
if (y == 0) swap(x, y);
fat[y] = x;
if (x != 0) Size[x] += Size[y];
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
string str;
cin >> n >> k >> str;
for (int i = 1; i <= k; i++) fat[i] = i, fat[i + k] = i + k, Size[i] = 1;
for (int i = 1; i <= k; i++) {
int tn;
cin >> tn;
for (int j = 0; j < tn; j++) {
int pos;
cin >> pos;
vec[pos].push_back(i);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
int x = vec[i][0];
ans -= calc(x);
if (str[i - 1] == '1')
fat[find(x)] = 0;
else
fat[find(x + k)] = 0;
ans += calc(x);
} else if (vec[i].size() == 2) {
int x = vec[i][0], y = vec[i][1];
if (str[i - 1] == '1') {
if (find(x) != find(y)) {
ans -= calc(x);
ans -= calc(y);
merge(x, y);
merge(x + k, y + k);
ans += calc(x);
}
} else {
if (find(x) != find(y + k)) {
ans -= calc(x);
ans -= calc(y);
merge(x, y + k);
merge(x + k, y);
ans += calc(x);
}
}
}
cout << ans << "\n";
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int lim = 1000 * 1000 + 5;
int nbElem, nbSub;
vector<int> subset[lim];
int side[lim];
int isIn[lim][2];
string ini;
int rep = 0;
int drepr[lim];
int dsz[lim];
int cnt[lim][2];
int dfind(int x) {
if (drepr[x] != x) drepr[x] = dfind(drepr[x]);
return drepr[x];
}
void add(int cc, int s0, int s1) {
cc = dfind(cc);
rep -= min(cnt[cc][0], cnt[cc][1]);
cnt[cc][0] = min(lim, cnt[cc][0] + s0);
cnt[cc][1] = min(lim, cnt[cc][1] + s1);
rep += min(cnt[cc][0], cnt[cc][1]);
}
void dmerge(int a, int b) {
a = dfind(a);
b = dfind(b);
if (a == b) return;
if (dsz[a] < dsz[b]) swap(a, b);
add(a, cnt[b][0], cnt[b][1]);
add(b, -cnt[b][0], -cnt[b][1]);
dsz[a] += dsz[b];
drepr[b] = a;
}
void dfs(int nod) {
cnt[nod][side[nod]] = 1;
for (int elem : subset[nod]) {
if (isIn[elem][1] == -1) continue;
int oth = isIn[elem][0] + isIn[elem][1] - nod;
if (side[oth] == -1) {
side[oth] = side[nod] ^ (ini[elem] == '0');
dfs(oth);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> nbElem >> nbSub;
cin >> ini;
fill(side, side + lim, -1);
iota(drepr, drepr + lim, 0);
fill_n(&isIn[0][0], 2 * lim, -1);
for (int sub = 0; sub < nbSub; ++sub) {
int st;
cin >> st;
subset[sub].resize(st);
for (int pos = 0; pos < st; ++pos) {
int elem;
cin >> elem;
--elem;
subset[sub][pos] = elem;
if (isIn[elem][0] == -1)
isIn[elem][0] = sub;
else
isIn[elem][1] = sub;
}
}
for (int sub = 0; sub < nbSub; ++sub) {
if (side[sub] == -1) {
side[sub] = 0;
dfs(sub);
}
}
for (int elem = 0; elem < nbElem; ++elem) {
int n0 = isIn[elem][0], n1 = isIn[elem][1];
if (n0 != -1 && n1 == -1) {
int destroy = side[n0] ^ (ini[elem] == '0');
if (destroy == 1)
add(n0, 0, lim);
else
add(n0, lim, 0);
} else if (n0 != -1) {
dmerge(n0, n1);
}
cout << rep << "\n";
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int pre[N], si[N], num[N / 2], n, m;
bool vis[N];
char s[N];
vector<vector<int> > v(N / 2);
int findd(int x) {
if (pre[x] == x) return x;
pre[x] = findd(pre[x]);
if (vis[x]) vis[pre[x]] = 1;
return pre[x];
}
inline int id(int x) { return x > m ? x - m : x; }
int un(int a, int b) {
int x = findd(a);
int y = findd(b);
if (x == y) return -1;
int aa = id(x);
int bb = id(y);
if (aa < bb) {
pre[y] = x, si[x] += si[y];
if (vis[y]) vis[x] = 1;
return aa;
} else {
pre[x] = y, si[y] += si[x];
if (vis[x]) vis[y] = 1;
return bb;
}
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d", &a);
while (a--) {
scanf("%d", &b);
v[b].push_back(i);
}
}
for (int i = 1; i <= m; i++) {
pre[i] = i, si[i] = 1;
pre[i + m] = i + m, si[i + m] = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
if (v[i].size() == 1) {
int a = findd(v[i][0]);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[id(a)];
num[id(a)] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b + m);
un(b, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
} else {
if (v[i].size() == 1) {
int a = findd(v[i][0] + m);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[id(a)];
num[id(a)] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b);
un(b + m, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
int dp[maxn][2], fa[maxn * 2], cnt[maxn * 2], n, k;
char buf[maxn];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
int calc(int x) {
int y;
if (x <= k)
y = x + k;
else
y = x - k;
x = Find(x), y = Find(y);
if (x == 0 || y == 0) return cnt[x + y];
return min(cnt[x], cnt[y]);
}
void Unite(int x, int y) {
x = Find(x), y = Find(y);
if (y == 0) swap(x, y);
fa[y] = x;
if (x) cnt[x] += cnt[y];
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", buf + 1);
for (int i = 1; i <= k; ++i) {
int q, id;
scanf("%d", &q);
while (q--) {
scanf("%d", &id);
if (dp[id][0])
dp[id][1] = i;
else
dp[id][0] = i;
}
fa[i] = i;
fa[i + k] = i + k;
cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (dp[i][1] == 0) {
int x = dp[i][0];
if (x) {
ans -= calc(x);
if (buf[i] == '1')
fa[Find(x + k)] = 0;
else
fa[Find(x)] = 0;
ans += calc(x);
}
} else {
int x = dp[i][0], y = dp[i][1];
if (buf[i] == '1') {
if (Find(x) != Find(y)) {
ans -= calc(x), ans -= calc(y);
Unite(x, y);
Unite(x + k, y + k);
ans += calc(x);
}
} else {
if (Find(x) != Find(y + k)) {
ans -= calc(x), ans -= calc(y);
Unite(x, y + k);
Unite(x + k, y);
ans += calc(x);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v[600005];
int k;
int mx[600005], a[300005], fa[600005];
int find(int x) {
if (fa[x] != x) {
fa[x] = find(fa[x]);
}
return fa[x];
}
void mer(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) {
mx[fx] += mx[fy];
fa[fy] = fx;
}
}
int get(int x) { return min(mx[find(x)], mx[find(x + k)]); }
int main(int argc, char** argv) {
int n;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%1d", &a[i]);
for (int i = 1; i <= 2 * k + 1; i++) {
fa[i] = i;
mx[i] = i > k;
}
mx[2 * k + 1] = 1e8;
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
while (c--) {
int x;
scanf("%d", &x);
v[x].push_back(i);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (v[i].size() == 1) {
int f = v[i][0];
if (a[i] == 1) {
ans -= get(f);
mer(f + k, 2 * k + 1);
ans += get(f);
} else {
ans -= get(f);
mer(f, 2 * k + 1);
ans += get(f);
}
}
if (v[i].size() == 2) {
int z = v[i][0], x = v[i][1];
if (a[i] == 1) {
if (find(x) != find(z)) {
ans -= get(x) + get(z);
mer(z + k, x + k);
mer(z, x);
ans += get(z);
}
} else {
if (find(x) != find(z + k)) {
ans -= get(x) + get(z);
mer(z + k, x);
mer(z, x + k);
ans += get(z);
}
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 10;
const long long inf = 1e9 + 10;
int f[N], vist[N];
char s[N];
long long val[N];
vector<int> vec[N];
int find(int x) {
if (f[x] == x) return x;
f[x] = find(f[x]);
return f[x];
}
void myunion(int x, int y) {
if (x < y) {
f[y] = x;
val[x] += val[y];
} else {
f[x] = y;
val[y] += val[x];
}
}
int main() {
int n, m, i, j, tot, x, y, fx, fy, type, ans = 0;
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (i = 1; i <= m; i++) {
scanf("%d", &tot);
for (j = 1; j <= tot; j++) {
scanf("%d", &x);
vec[x].push_back(i);
}
}
for (i = 1; i <= m; i++) {
f[i] = i;
f[i + m] = m + i;
val[i] = 1;
}
for (i = 1; i <= n; i++) {
type = s[i] - '0';
if (vec[i].size() == 1) {
x = vec[i][0];
y = x + m;
fx = find(x);
fy = find(y);
if (vist[fx] || vist[fy]) ans -= min(val[fx], val[fy]);
vist[fx] = vist[fy] = 1;
if (type == 0) {
val[fy] = inf;
ans += val[fx];
} else {
val[fx] = inf;
ans += val[fy];
}
} else if (vec[i].size() == 2) {
x = vec[i][0];
y = vec[i][1];
if (type == 0) {
if (find(x) != find(y + m)) {
if (vist[find(x)]) ans -= min(val[find(x)], val[find(x + m)]);
if (vist[find(y)]) ans -= min(val[find(y)], val[find(y + m)]);
myunion(find(x), find(y + m));
myunion(find(y), find(x + m));
ans += min(val[find(x)], val[find(x + m)]);
}
} else {
if (find(x) != find(y)) {
if (vist[find(x)]) ans -= min(val[find(x)], val[find(x + m)]);
if (vist[find(y)]) ans -= min(val[find(y)], val[find(y + m)]);
myunion(find(x), find(y));
myunion(find(x + m), find(y + m));
ans += min(val[find(x)], val[find(x + m)]);
}
}
vist[find(x)] = vist[find(x + m)] = vist[find(y)] = vist[find(y + m)] = 1;
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] ? fa[u] = find(fa[u]) : u; }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += mix(k[i][1]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += mix(k[i][0]);
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x;
vector<int> vec[300010];
char c[300010];
int fa[300010 << 1], sz[300010 << 1];
int lim[300010 << 1];
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d%s", &n, &k, c + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &m);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
fa[i * 2 - 1] = i * 2 - 1;
sz[i * 2 - 1] = 1;
fa[i * 2] = i * 2;
sz[i * 2] = 0;
lim[i * 2 - 1] = lim[i * 2] = -1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
int x = vec[i][0];
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f1] == -1)
ans -= min(s0, s1);
else
ans -= lim[f1] ? s1 : s0;
lim[f1] = (c[i] == '0');
lim[f0] = lim[f1] ^ 1;
ans += lim[f1] ? s1 : s0;
} else if (vec[i].size() == 2) {
int x = vec[i][0], y = vec[i][1];
int fx1 = find(x * 2 - 1), fx0 = find(x * 2);
int sx1 = sz[fx1], sx0 = sz[fx0];
int fy1 = find(y * 2 - 1), fy0 = find(y * 2);
int sy1 = sz[fy1], sy0 = sz[fy0];
if (fx0 != fy0 && fx0 != fy1) {
if (lim[fx1] == -1)
ans -= min(sx0, sx1);
else
ans -= lim[fx1] ? sx1 : sx0;
if (lim[fy1] == -1)
ans -= min(sy0, sy1);
else
ans -= lim[fy1] ? sy1 : sy0;
if (c[i] == '1') {
fa[fx1] = fy1;
fa[fx0] = fy0;
sz[fy1] += sz[fx1];
sz[fy0] += sz[fx0];
if (lim[fx1] != -1) {
lim[fy1] = lim[fx1];
lim[fy0] = lim[fy1] ^ 1;
}
} else {
fa[fx1] = fy0;
fa[fx0] = fy1;
sz[fy0] += sz[fx1];
sz[fy1] += sz[fx0];
if (lim[fx1] != -1) {
lim[fy0] = lim[fx1];
lim[fy1] = lim[fy0] ^ 1;
}
}
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f1] == -1)
ans += min(s0, s1);
else
ans += lim[f1] ? s1 : s0;
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int val[300000 + 100], sum[300000 + 100][2], head[300000 + 100];
int f[300000 + 100];
vector<int> V[300000 + 100];
int n, k;
int ans;
int findd(int x) {
if (f[x] == x) return x;
int F = f[x];
f[x] = findd(f[x]);
val[x] = val[x] ^ val[F];
return f[x];
}
char s[300000 + 100];
void init() {
for (int i = 0; i <= max(n, k); i++) f[i] = i;
for (int i = 1; i <= max(n, k); i++) {
sum[i][0] = 0;
sum[i][1] = 1;
}
memset(head, -1, sizeof(head));
}
int get(int x) {
if (head[x] == -1) return min(sum[x][0], sum[x][1]);
return sum[x][head[x]];
}
void build(int x, int y, int v) {
int X = findd(x);
int Y = findd(y);
if (X != Y) {
ans -= get(X);
ans -= get(Y);
sum[Y][0] += sum[X][v ^ val[x] ^ val[y]];
sum[Y][1] += sum[X][1 ^ v ^ val[x] ^ val[y]];
if (head[X] != -1 && head[Y] == -1) {
head[Y] = v ^ val[x] ^ val[y] ^ head[X];
}
val[X] = val[x] ^ val[y] ^ v;
f[X] = Y;
ans += get(Y);
}
}
void work(int x, int v) {
int X = findd(x);
ans -= get(X);
if (head[X] == -1) head[X] = v ^ val[x];
ans += get(X);
}
int main() {
int T;
cin >> n >> k;
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &T);
int cpy = T;
int x;
while (T--) {
scanf("%d", &x);
V[x].push_back(i);
}
}
init();
for (int i = 1; i <= n; i++) {
if (V[i].size() == 1) {
work(V[i][0], 1 ^ (s[i] - '0'));
} else if (V[i].size() == 2) {
build(V[i][0], V[i][1], 1 ^ (s[i] - '0'));
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
string s;
vector<int> a[300001];
int p[1000001];
long long f[1000001];
const int Max = 6e5;
const int T = 3e5;
int root(int u) {
if (u == p[u]) return u;
return p[u] = root(p[u]);
}
void Merge(int u, int v) {
u = root(u);
v = root(v);
int ru = root(u);
int rv = root(v);
p[rv] = ru;
f[ru] += f[rv];
}
int main() {
cin >> n >> k >> s;
s = '.' + s;
for (int i = 1; i <= Max; i++) {
p[i] = i;
f[i] = -1;
}
for (int i = 1; i <= k; i++) {
int len;
cin >> len;
while (len--) {
int x;
cin >> x;
a[x].push_back(i);
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
if (a[i].size() == 1) {
int u = a[i][0];
if (f[root(u + T)] == -1) {
f[root(u + T)] = 1;
res++;
}
if (f[root(u)] != -1) {
res -= min(f[root(u)], f[root(u + T)]);
f[root(u)] = 1e9;
res += min(f[root(u)], f[root(u + T)]);
}
f[root(u)] = 1e9;
} else if (a[i].size() == 2) {
int u = a[i][0];
int v = a[i][1];
if (f[root(u)] == -1) f[root(u)] = 0;
if (f[root(u + T)] == -1) f[root(u + T)] = 1;
if (f[root(v)] == -1) f[root(v)] = 0;
if (f[root(v + T)] == -1) f[root(v + T)] = 1;
if (root(u) != root(v + T)) {
res -= min(f[root(u)], f[root(u + T)]);
res -= min(f[root(v)], f[root(v + T)]);
Merge(u, v + T);
Merge(u + T, v);
res += min(f[root(u)], f[root(u + T)]);
}
}
} else {
if (a[i].size() == 1) {
int u = a[i][0];
if (f[root(u + T)] != -1) {
res -= min(f[root(u)], f[root(u + T)]);
f[root(u + T)] = 1e9;
res += min(f[root(u)], f[root(u + T)]);
}
if (f[root(u)] == -1) f[root(u)] = 0;
f[root(u + T)] = 1e9;
} else if (a[i].size() == 2) {
int u = a[i][0];
int v = a[i][1];
if (f[root(u)] == -1) f[root(u)] = 0;
if (f[root(u + T)] == -1) f[root(u + T)] = 1;
if (f[root(v)] == -1) f[root(v)] = 0;
if (f[root(v + T)] == -1) f[root(v + T)] = 1;
if (root(u) != root(v)) {
res -= min(f[root(u)], f[root(u + T)]);
res -= min(f[root(v)], f[root(v + T)]);
Merge(u, v);
Merge(u + T, v + T);
res += min(f[root(u)], f[root(u + T)]);
}
}
}
cout << res << "\n";
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6E5 + 10;
int n, k, l[N][2];
int fa[N], sc[N];
string s;
int find(int x) {
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int cal(int x) {
int y = x <= k ? x + k : x - k;
int xx = find(x), yy = find(y);
if (xx == 0 || yy == 0) return sc[xx + yy];
return min(sc[xx], sc[yy]);
}
void merge(int x, int y) {
int xx = find(x), yy = find(y);
if (xx == 0)
fa[yy] = xx, sc[xx] += sc[yy];
else
fa[xx] = yy, sc[yy] += sc[xx];
}
int main() {
scanf("%d%d", &n, &k);
cin >> s;
for (int i = 1; i <= k; i++) fa[i] = i, fa[i + k] = i + k, sc[i + k] = 1;
for (int i = 1, c; i <= k; i++) {
scanf("%d", &c);
for (int j = 0, v; j < c; j++) {
scanf("%d", &v);
if (l[v][0])
l[v][1] = i;
else
l[v][0] = i;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= cal(x);
if (s[i - 1] == '1')
fa[find(x + k)] = 0;
else
fa[find(x)] = 0;
ans += cal(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (find(x) != find(y)) {
ans -= cal(x);
ans -= cal(y);
merge(x, y);
merge(x + k, y + k);
ans += cal(x);
}
} else {
if (find(x) != find(y + k)) {
ans -= cal(x);
ans -= cal(y);
merge(x, y + k);
merge(x + k, y);
ans += cal(x);
}
}
}
cout << ans << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.math.*;
public class Solution {
public static void main(String[] args) throws IOException {
//PrintWriter out = new PrintWriter(new File("out.txt"));
PrintWriter out = new PrintWriter(System.out);
//Reader in = new Reader(new FileInputStream("in.txt"));
Reader in = new Reader();
Solution solver = new Solution();
solver.solve(out, in);
out.flush();
out.close();
}
static int maxn = 5*(int)1e5+5;
static long mod=(int)1e9+7;
static int n, m, t, k, q;
static int[] rank, clr, arr;
static Node[] par;
static ArrayList<Edge> adj[];
static boolean[] vis;
void solve(PrintWriter out, Reader in) throws IOException{
n = in.nextInt();
k = in.nextInt();
String s = in.next();
arr = new int[n+1];
for (int i = 0; i < n; i++) {
arr[i+1] = s.charAt(i)-'0';
}
adj = new ArrayList[k+1];
for (int i = 1; i <= k; i++)
adj[i] = new ArrayList<Edge>();
ArrayList<Integer> pos[] = new ArrayList[n+1];
for (int i = 1; i <= n; i++)
pos[i] = new ArrayList<Integer>();
for (int z = 1; z <= k; z++) {
int c = in.nextInt();
for (int i = 0; i < c; i++) {
pos[in.nextInt()].add(z);
}
}
int u = 0, v = 0;
for (int i = 1; i <= n; i++) {
if (pos[i].size() == 2) {
u = pos[i].get(0);
v = pos[i].get(1);
adj[u].add(new Edge(v, i));
adj[v].add(new Edge(u, i));
}
}
clr = new int[k+1];
vis = new boolean[k+1];
for (int i = 1; i <= k; i++) {
if (!vis[i]) {
clr[i] = 1;
DFS(i);
}
}
int[] ans = new int[n+1];
rank = new int[k+1];
par = new Node[k+1];
for (int i = 1; i <= k; i++) {
par[i] = new Node(i, clr[i], clr[i]^1);
}
for (int i = 1; i <= k; i++) vis[i] = false;
for (int i = 1; i <= n; i++) {
if (pos[i].size() == 2) {
u = pos[i].get(0); v = pos[i].get(1);
u = find(u); v = find(v);
if (u == v) {
vis[u] = true;
} else {
ans[i] -= (vis[u]?getans(u):0) + (vis[v]?getans(v):0);
//out.print(u+" "+v+" "+(vis[u]?getans(u):0) +" "+ (vis[v]?getans(v):0)+" ");
vis[u] = vis[v] = true;
union(u, v);
u = find(u);
//out.println(u+" "+getans(u)+" "+i);
ans[i] += getans(u);
}
} else if (pos[i].size() == 1) {
u = pos[i].get(0);
v = clr[u]^arr[i];
u = find(u);
if (vis[u]) ans[i] -= getans(u);
//out.print(vis[u]+" "+u+" "+getans(u));
vis[u] = true;
if (v == 1) par[u].redf = true;
else par[u].bluef = true;
ans[i] += getans(u);
//out.println(" "+getans(u)+" "+u+" "+i+" "+v);
}
ans[i] += ans[i-1];
out.println(ans[i]);
}
}
//<>
static void DFS(int s) {
vis[s] = true;
for (Edge e:adj[s]) {
if (!vis[e.to]) {
int tmp = clr[s]+arr[e.w];
clr[e.to] = (tmp%2)^1;
DFS(e.to);
}
}
}
static int getans(int x) {
if (par[x].redf) return par[x].red;
else if (par[x].bluef) return par[x].blue;
return Math.min(par[x].red, par[x].blue);
}
static int find(int x) {
if (x != par[x].rt) par[x].rt = find(par[x].rt);
return par[x].rt;
}
static void union(int xroot, int yroot) {
if (rank[xroot] < rank[yroot]) {
par[xroot].rt = yroot;
} else if (rank[xroot] > rank[yroot]) {
par[yroot].rt = xroot;
int tmp = xroot;
xroot = yroot;
yroot = tmp;
} else {
par[xroot].rt = yroot;
rank[yroot]++;
}
par[yroot].red += par[xroot].red;
par[yroot].blue += par[xroot].blue;
par[yroot].bluef |= par[xroot].bluef;
par[yroot].redf |= par[xroot].redf;
}
static class Node {
int rt, red, blue;
boolean bluef, redf;
Node (int rt, int red, int blue) {
this.rt = rt;
this.red = red;
this.blue = blue;
}
}
static class Edge {
int to, w;
Edge(int to, int w) {
this.to = to;
this.w = w;
}
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble()
{
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300000 + 10;
const int apsz = 26;
const int INF = 10000007;
const unsigned long long mod = 97;
const int maxm = 10000 + 10;
struct rev {
int c0, c1, tag;
void maintain() {
c0 = min(c0, INF);
c1 = min(c1, INF);
}
};
rev info[maxn];
int dsu[maxn];
pair<int, int> fd(int u) {
if (dsu[u] < 0) return {u, 0};
pair<int, int> tmp = fd(dsu[u]);
info[u].tag ^= tmp.second;
dsu[u] = tmp.first;
return (pair<int, int>){tmp.first, info[u].tag};
}
int st[maxn];
char s[maxn];
int n, k;
vector<int> edge[maxn];
int main(void) {
scanf("%d", &(n)), scanf("%d", &(k));
scanf("%s", s + 1);
for (int i = (1); i <= (n); ++i) st[i] = s[i] - '0';
for (int i = (1); i <= (k); ++i) {
info[i] = (rev){0, 1, 0};
dsu[i] = -1;
int sz;
scanf("%d", &(sz));
for (; sz--;) {
int x;
scanf("%d", &(x));
edge[x].push_back(i);
}
}
long long ans = 0;
for (int i = (1); i <= (n); ++i) {
int co = st[i];
if (edge[i].size() == 1) {
pair<int, int> t = fd(edge[i][0]);
int u = t.first;
ans -= min(info[u].c0, info[u].c1);
if (co == 1) {
if (t.second == 0)
info[u].c1 = INF;
else
info[u].c0 = INF;
} else if (co == 0) {
if (t.second == 0)
info[u].c0 = INF;
else
info[u].c1 = INF;
}
ans += min(info[u].c0, info[u].c1);
} else if (edge[i].size() == 2) {
pair<int, int> x, y;
x = fd(edge[i][0]);
y = fd(edge[i][1]);
if (x.first != y.first) {
if (dsu[x.first] > dsu[y.first]) swap(x, y);
ans -= min(info[x.first].c0, info[x.first].c1);
ans -= min(info[y.first].c0, info[y.first].c1);
if (co == 1) {
if (x.second != y.second) {
info[y.first].tag ^= 1;
swap(info[y.first].c0, info[y.first].c1);
}
} else {
if (x.second == y.second) {
info[y.first].tag ^= 1;
swap(info[y.first].c0, info[y.first].c1);
}
}
info[x.first].c0 += info[y.first].c0;
info[x.first].c1 += info[y.first].c1;
info[x.first].maintain();
ans += min(info[x.first].c0, info[x.first].c1);
dsu[x.first] += dsu[y.first];
dsu[y.first] = x.first;
}
}
printf("%lld\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk;
fa[kk + kk + 1] = kk + kk + 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += mix(k[i][1]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += mix(k[i][0]);
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); }
void uni(int a, int b) {
if (a == b) return;
val[b] += val[a];
fa[a] = b;
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk;
fa[kk + kk + 1] = kk + kk + 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0]), find(k[i][1])),
uni(find(k[i][0] + kk), find(k[i][1] + kk));
ans += min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
} else {
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0]), find(kk + kk + 1));
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0]), find(k[i][1] + kk)),
uni(find(k[i][1]), find(k[i][0] + kk));
ans += min(val[find(k[i][1] + kk)], val[find(k[i][0] + kk)]);
} else {
ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]);
uni(find(k[i][0] + kk), find(kk + kk + 1));
ans += val[find(k[i][0])];
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 810000;
int fa[MAXN], val[MAXN], vec[MAXN][3];
int n, k, ans;
char str[MAXN];
int getfa(int x) {
if (fa[x] == x) return x;
return fa[x] = getfa(fa[x]);
}
void merge(int x, int y) {
int p = getfa(x), q = getfa(y);
if (p == q) return;
fa[p] = q;
val[q] += val[p];
}
int getval(int x) { return min(val[getfa(x)], val[getfa(x + k)]); }
int main() {
scanf("%d%d%s", &n, &k, str + 1);
int tc;
for (int i = 1; i <= k; i++) {
scanf("%d", &tc);
int tx;
for (int j = 1; j <= tc; j++) {
scanf("%d", &tx);
vec[tx][++vec[tx][0]] = i;
}
}
for (int i = 1; i <= k; i++) fa[i] = i, val[i] = 0;
for (int i = k + 1; i <= 2 * k; i++) fa[i] = i, val[i] = 1;
fa[k << 1 | 1] = k << 1 | 1;
val[k << 1 | 1] = 1000000;
for (int i = 1; i <= n; i++) {
if (vec[i][0] == 2) {
if (str[i] == '0') {
if (getfa(vec[i][1]) != getfa(vec[i][2] + k)) {
ans -= getval(vec[i][1]) + getval(vec[i][2]);
merge(vec[i][1], vec[i][2] + k);
merge(vec[i][1] + k, vec[i][2]);
ans += getval(vec[i][1]);
}
} else {
if (getfa(vec[i][1]) != getfa(vec[i][2])) {
ans -= getval(vec[i][1]) + getval(vec[i][2]);
merge(vec[i][1] + k, vec[i][2] + k);
merge(vec[i][1], vec[i][2]);
ans += getval(vec[i][1]);
}
}
} else if (vec[i][0] == 1) {
if (str[i] == '0') {
ans -= getval(vec[i][1]);
merge(vec[i][1], k << 1 | 1);
ans += getval(vec[i][1]);
} else {
ans -= getval(vec[i][1]);
merge(vec[i][1] + k, k << 1 | 1);
ans += getval(vec[i][1]);
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 69;
int n, k;
string initial;
int dsu[N];
int sz[N];
int szsame[N];
int parity[N];
int restrict[N];
vector<int> belong[N];
int curr = 0;
int getVal(int u) {
if (restrict[u] == 0) return szsame[u];
if (restrict[u] == 1) return (sz[u] - szsame[u]);
return min(szsame[u], sz[u] - szsame[u]);
}
pair<int, int> find(int u) {
if (dsu[u] != u) {
pair<int, int> res = find(dsu[u]);
dsu[u] = res.first;
if (parity[u])
parity[u] = res.second;
else
parity[u] = res.second ^ 1;
return make_pair(dsu[u], parity[u]);
}
return make_pair(u, 1);
}
int funion(int u, int v, bool same) {
pair<int, int> r1 = find(u);
pair<int, int> r2 = find(v);
if (r1.first == r2.first) return 0;
if (sz[r1.first] < sz[r2.first]) swap(r1, r2);
int init = getVal(r1.first) + getVal(r2.first);
dsu[r2.first] = r1.first;
sz[r1.first] += sz[r2.first];
bool rsame = true;
if (same && r1.second != r2.second) rsame = false;
if (!same && r1.second == r2.second) rsame = false;
if (!rsame) parity[r2.first] = 0;
if (restrict[r2.first] < 2) {
if (rsame)
restrict[r1.first] = restrict[r2.first];
else
restrict[r1.first] = restrict[r2.first] ^ 1;
}
if (rsame)
szsame[r1.first] += szsame[r2.first];
else
szsame[r1.first] += sz[r2.first] - szsame[r2.first];
return getVal(r1.first) - init;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
cin >> initial;
for (int i = 0; i < k; i++) {
int t;
cin >> t;
for (int j = 0; j < t; j++) {
int l;
cin >> l;
l--;
belong[l].push_back(i);
}
dsu[i] = i;
sz[i] = 1;
szsame[i] = 1;
parity[i] = 1;
restrict[i] = 2;
}
for (int i = 0; i < n; i++) {
if (belong[i].size() == 1) {
int u = belong[i][0];
pair<int, int> res = find(u);
int init = getVal(res.first);
if (initial[i] == '0') {
if (res.second)
restrict[res.first] = 0;
else
restrict[res.first] = 1;
} else {
if (res.second)
restrict[res.first] = 1;
else
restrict[res.first] = 0;
}
curr += getVal(res.first) - init;
} else if (belong[i].size() == 2) {
bool same = (initial[i] == '1');
int u = belong[i][0];
int v = belong[i][1];
curr += funion(u, v, same);
}
cout << curr << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline long long read1() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 3e5 + 10;
int n, k;
vector<int> q[N];
int f[N], val[N], sz[N][2], vis[N], ans;
int Find(int x) {
if (x == f[x]) return x;
Find(f[x]);
val[x] ^= val[f[x]];
f[x] = f[f[x]];
return f[x];
}
int getval(int x) {
if (vis[x] == -1)
return min(sz[x][0], sz[x][1]);
else
return sz[x][vis[x]];
}
void merge(int u, int v, int c) {
Find(u);
Find(v);
if (f[u] != f[v]) {
ans -= getval(f[u]);
ans -= getval(f[v]);
c ^= val[u] ^ val[v];
u = f[u];
v = f[v];
sz[u][0] += sz[v][c];
sz[u][1] += sz[v][1 - c];
if (vis[v] != -1 && vis[u] == -1) vis[u] = vis[v] ^ c;
f[v] = u;
val[v] = c;
ans += getval(u);
}
}
void sure(int u, int c) {
Find(u);
c ^= val[u];
u = f[u];
ans -= getval(u);
if (vis[u] == -1) vis[u] = c;
ans += getval(u);
}
char ch[N];
int main() {
n = read();
k = read();
scanf("%s", ch + 1);
for (int i = 1; i <= k; i++) {
int num = read();
while (num--) {
int x = read();
q[x].push_back(i);
}
}
for (int i = 1; i <= k; i++)
f[i] = i, sz[i][1] = 1, sz[i][0] = 0, vis[i] = -1;
for (int i = 1; i <= n; i++) {
if (q[i].size() == 2)
merge(q[i][0], q[i][1], 1 - (ch[i] - '0'));
else if (q[i].size() != 0)
sure(q[i][0], 1 - (ch[i] - '0'));
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long n, k, pa[600010], ct[600010], siz[600010], ans = 0;
vector<long long> g[300010];
string s;
long long find(long long x) {
if (x == pa[x]) return x;
pa[x] = find(pa[x]);
return pa[x];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
cin >> s;
for (long long i = 0; i < k; i++) {
long long t;
cin >> t;
for (long long j = 0; j < t; j++) {
long long x;
cin >> x;
--x;
g[x].push_back(i);
}
}
for (long long i = 0; i < k; i++) {
ct[2 * i + 1] = 1;
ct[2 * i] = 0;
pa[2 * i] = 2 * i;
pa[2 * i + 1] = 2 * i + 1;
siz[2 * i + 1] = 1;
siz[2 * i] = 1;
}
for (long long i = 0; i < n; i++) {
long long a, b;
;
if (((long long)g[i].size()) == 2) {
a = g[i][0];
b = g[i][1];
;
;
long long x1 = find(2 * a), y1 = find(b * 2), x2 = find(2 * a + 1),
y2 = find(2 * b + 1);
if (s[i] == '0') swap(x1, x2);
;
;
if (x1 == y1) {
cout << ans << '\n';
continue;
}
ans -= (min(ct[x1], ct[x2]) + min(ct[y1], ct[y2]));
;
if (siz[x1] > siz[y1]) swap(x1, y1), swap(x2, y2);
siz[y1] += siz[x1];
pa[x1] = y1;
ct[y1] += ct[x1];
ct[x1] = 0;
siz[y2] += siz[x2];
pa[x2] = y2;
ct[y2] += ct[x2];
ct[x2] = 0;
ans += min(ct[y1], ct[y2]);
} else {
if (((long long)g[i].size()) == 0) {
cout << ans << '\n';
continue;
}
long long a = g[i][0], x, delx;
if (s[i] == '1')
x = find(2 * a), delx = find(2 * a + 1);
else
x = find(2 * a + 1), delx = find(2 * a);
ans -= min(ct[x], ct[delx]);
;
ct[delx] = 1e10;
ans += ct[x];
}
cout << ans << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)3e5 + 10;
int par[maxn << 1], num[maxn << 1];
inline void makeSet(int size) {
for (int i = 1; i <= size; i++) {
par[i] = i;
}
}
inline int find(int x) {
int k, j, r;
r = x;
while (r != par[r]) {
r = par[r];
}
k = x;
while (k != r) {
j = par[k];
par[k] = r;
k = j;
}
return r;
}
inline void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
par[x] = y;
num[y] += num[x];
}
inline int calc(int x, int k) { return min(num[find(x)], num[find(x + k)]); }
int belong[maxn][2];
char s[maxn];
int main() {
int n, k, c, tmp;
scanf("%d%d%s", &n, &k, s + 1);
for (int i = 1; i <= k; ++i) {
scanf("%d", &c);
for (int j = 0; j < c; ++j) {
scanf("%d", &tmp);
if (belong[tmp][0]) {
belong[tmp][1] = i;
} else {
belong[tmp][0] = i;
}
}
num[i] = 1;
}
makeSet(k << 1 | 1);
num[k << 1 | 1] = 0x3f3f3f3f;
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (belong[i][1]) {
if (s[i] == '0' && find(belong[i][0]) != find(belong[i][1] + k)) {
ans -= calc(belong[i][0], k) + calc(belong[i][1], k);
unite(belong[i][0], belong[i][1] + k);
unite(belong[i][1], belong[i][0] + k);
ans += calc(belong[i][0], k);
} else if (s[i] == '1' && find(belong[i][0]) != find(belong[i][1])) {
ans -= calc(belong[i][0], k) + calc(belong[i][1], k);
unite(belong[i][0], belong[i][1]);
unite(belong[i][0] + k, belong[i][1] + k);
ans += calc(belong[i][0], k);
}
} else if (belong[i][0]) {
ans -= calc(belong[i][0], k);
if (s[i] == '0') {
unite(belong[i][0] + k, k << 1 | 1);
} else {
unite(belong[i][0], k << 1 | 1);
}
ans += calc(belong[i][0], k);
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, inf = 0x3f3f3f3f;
int n, K, m, x, pos[N][2], ans, fa[N], val[N];
char s[N];
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
void add(int x, int y) {
int fx = getfa(x), fy = getfa(y);
if (fx != fy) fa[fx] = fy, val[fy] += val[fx];
}
int calc(int x) { return min(val[getfa(x)], val[getfa(x + K)]); }
int main() {
cin >> n >> K;
scanf("%s", s + 1);
for (int i = 1; i <= K; i++) {
scanf("%d", &m);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
if (pos[x][0])
pos[x][1] = i;
else
pos[x][0] = i;
}
}
for (int i = 0; i <= 2 * K + 1; i++) {
fa[i] = i;
if (i > K) val[i] = 1;
}
val[2 * K + 1] = inf;
for (int i = 1; i <= n; i++) {
if (!pos[i][0]) {
printf("%d\n", ans);
continue;
}
if (!pos[i][1]) {
ans -= calc(pos[i][0]);
if (s[i] == '1')
add(pos[i][0] + K, 2 * K + 1);
else
add(pos[i][0], 2 * K + 1);
ans += calc(pos[i][0]);
} else {
int x = pos[i][0], y = pos[i][1];
if (getfa(x) != getfa(y) && getfa(x) != getfa(y + K)) {
ans -= calc(x) + calc(y);
if (s[i] == '1')
add(x, y), add(x + K, y + K);
else
add(x, y + K), add(x + K, y);
ans += calc(x);
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
long long t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 1000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() == 2) {
long long a = la[i][0], b = la[i][1];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
long long kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else {
kl = min(pr[a].l + pr[b].l, cc);
}
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
} else if (la[i].size() == 1) {
bool b = 0;
int a = la[i][0];
while (a != -1) {
b ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if ((b and s[i] == '1') or (!b and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
x = 0;
char c = getchar();
bool f = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
if (f) x = -x;
return x;
}
template <class t>
inline void write(t x) {
if (x < 0) {
putchar('-'), write(-x);
} else {
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
}
const long long N = 3e5 + 5;
long long n, k, SsEeTt[N][3], f[N << 1], sz[N << 1], s[N], ans, oo;
inline long long min(const long long &x, const long long &y) {
return x < y ? x : y;
}
inline long long getf(const long long &x) {
return f[x] == x ? x : f[x] = getf(f[x]);
}
signed main() {
read(n);
oo = read(k);
for (long long i = 1; i <= n; i++) scanf("%1d", &s[i]);
for (long long i = 1, n, x; i <= k; i++) {
read(n);
while (n--) {
read(x);
if (!SsEeTt[x][1])
SsEeTt[x][1] = i;
else
SsEeTt[x][2] = i;
}
}
for (long long i = 1; i <= k; i++) {
f[i * 2 - 1] = i * 2 - 1;
f[i << 1] = i << 1;
sz[i << 1] = 1;
}
for (long long i = 1, fx, fy, gx, gy; i <= n; i++) {
if (SsEeTt[i][2]) {
if (!s[i]) {
fx = getf(SsEeTt[i][1] * 2 - 1), fy = getf(SsEeTt[i][2] << 1);
if (fx == fy) {
write(ans);
puts("");
continue;
}
gx = getf(SsEeTt[i][1] << 1), gy = getf(SsEeTt[i][2] * 2 - 1);
} else {
fx = getf(SsEeTt[i][1] * 2 - 1), fy = getf(SsEeTt[i][2] * 2 - 1);
if (fx == fy) {
write(ans);
puts("");
continue;
}
gx = getf(SsEeTt[i][1] << 1), gy = getf(SsEeTt[i][2] << 1);
}
ans -= min(sz[fx], sz[gx]) + min(sz[fy], sz[gy]);
f[fx] = fy;
f[gx] = gy;
sz[fy] += sz[fx];
sz[gy] += sz[gx];
ans += min(sz[fy], sz[gy]);
} else if (SsEeTt[i][1]) {
long long fx = getf(SsEeTt[i][1] * 2 - 1), fy = getf(SsEeTt[i][1] * 2);
ans -= min(sz[fx], sz[fy]);
if (!s[i])
sz[fx] += oo;
else
sz[fy] += oo;
ans += min(sz[fx], sz[fy]);
}
write(ans);
puts("");
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] ? fa[u] = find(fa[u]) : u; }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += mix(k[i][0]);
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 10;
char s[N];
int par[N], diff[N];
pair<int, int> find(int x) {
if (par[x] == x) return {x, 0};
auto r = find(par[x]);
par[x] = r.first, diff[x] ^= r.second;
return {par[x], diff[x]};
}
int f[N][2], col[N], ans;
int get(int x) {
if (col[x] == -1) return min(f[x][0], f[x][1]);
return f[x][col[x]];
}
vector<int> p[N];
int main() {
int n, k;
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
for (int j = 1; j <= x; j++) {
int y;
scanf("%d", &y);
p[y].push_back(i);
}
}
for (int i = 1; i <= k; i++)
par[i] = i, diff[i] = 0, f[i][0] = 0, f[i][1] = 1, col[i] = -1;
for (int i = 1; i <= n; i++) {
if ((int)p[i].size() == 0)
;
else if ((int)p[i].size() == 1) {
int x = p[i][0], y = find(x).first;
ans -= get(y);
col[y] = diff[x] ^ (s[i] == '0');
ans += get(y);
} else {
int x = find(p[i][0]).first, y = find(p[i][1]).first;
if (x != y) {
ans -= get(x), ans -= get(y);
par[x] = y;
diff[x] = (s[i] == '0') ^ diff[p[i][0]] ^ diff[p[i][1]];
f[y][0] += f[x][0 ^ diff[x]], f[y][1] += f[x][1 ^ diff[x]];
if (col[x] != -1) col[y] = col[x] ^ diff[x];
ans += get(y);
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
python3
|
from sys import stdin
input = stdin.readline
n , k = [int(i) for i in input().split()]
pairs = [i + k for i in range(k)] + [i for i in range(k)]
initial_condition = list(map(lambda x: x == '1',input().strip()))
data = [i for i in range(2*k)]
constrain = [-1] * (2*k)
h = [0] * (2*k)
L = [1] * k + [0] * k
dp1 = [-1 for i in range(n)]
dp2 = [-1 for i in range(n)]
for i in range(k):
input()
inp = [int(j) for j in input().split()]
for s in inp:
if dp1[s-1] == -1:dp1[s-1] = i
else:dp2[s-1] = i
pfsums = 0
ans = []
def remove_pfsum(s1):
global pfsums
if constrain[s1] == 1:
pfsums -= L[s1]
elif constrain[pairs[s1]] == 1:
pfsums -= L[pairs[s1]]
else:
pfsums -= min(L[s1],L[pairs[s1]])
def sh(i):
while i != data[i]:
i = data[i]
return i
def upd_pfsum(s1):
global pfsums
if constrain[s1] == 1:
pfsums += L[s1]
elif constrain[pairs[s1]] == 1:
pfsums += L[pairs[s1]]
else:
pfsums += min(L[s1],L[pairs[s1]])
def ms(i,j):
i = sh(i) ; j = sh(j)
cons = max(constrain[i],constrain[j])
if h[i] < h[j]:
data[i] = j
L[j] += L[i]
constrain[j] = cons
return j
else:
data[j] = i
if h[i] == h[j]:
h[i] += 1
L[i] += L[j]
constrain[i] = cons
return i
for i in range(n):
if dp1[i] == -1 and dp2[i] == -1:
pass
elif dp2[i] == -1:
s1 = sh(dp1[i])
remove_pfsum(s1)
constrain[s1] = 0 if initial_condition[i] else 1
constrain[pairs[s1]] = 1 if initial_condition[i] else 0
upd_pfsum(s1)
else:
s1 = sh(dp1[i]) ; s2 = sh(dp2[i])
if s1 == s2 or pairs[s1] == s2:
pass
else:
remove_pfsum(s1)
remove_pfsum(s2)
if initial_condition[i]:
new_s1 = ms(s1,s2)
new_s2 = ms(pairs[s1],pairs[s2])
else:
new_s1 = ms(s1,pairs[s2])
new_s2 = ms(pairs[s1],s2)
pairs[new_s1] = new_s2
pairs[new_s2] = new_s1
upd_pfsum(new_s1)
ans.append(pfsums)
for i in ans:
print(i)
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 300009;
const long long MOD = 119 << 23 | 1;
class {
public:
vector<int> V[MAXN];
int fa[MAXN * 2], cnt[MAXN * 2];
int n, m;
int curset[MAXN * 2], ans;
int findfa(int x) {
if (fa[x] == x) return x;
return fa[x] = findfa(fa[x]);
}
void merge(int x, int y) {
x = findfa(x);
y = findfa(y);
if (x == y) return;
if (x > y) swap(x, y);
fa[y] = x;
cnt[x] += cnt[y];
}
void solve() {
cin >> n >> m;
string s;
cin >> s;
for (int i = 1; i <= m; ++i) {
int c;
cin >> c;
for (int j = 0; j < c; ++j) {
int v;
cin >> v;
V[v].push_back(i);
}
fa[i] = i;
fa[i + m] = i + m;
cnt[i + m] = 1;
}
ans = 0;
for (int i = 1; i <= n; ++i) {
bool on = s[i - 1] == '1';
if (V[i].size() == 1) {
int p = V[i][0];
if (on) {
del(p);
del(p + m);
merge(p + m, 0);
add(p);
} else {
del(p);
del(p + m);
merge(p, 0);
add(p);
}
} else if (V[i].size() == 2) {
int p0 = V[i][0], p1 = V[i][1];
if (on) {
del(p0);
del(p1);
del(p1 + m);
del(p0 + m);
merge(p0, p1);
merge(p0 + m, p1 + m);
add(p0);
} else {
del(p0);
del(p1);
del(p1 + m);
del(p0 + m);
merge(p0, p1 + m);
merge(p0 + m, p1);
add(p0);
}
}
cout << ans << '\n';
}
}
void add(int x) {
int y = x + m;
x = findfa(x);
y = findfa(y);
if (x == 0)
curset[y] = 1, ans += cnt[y];
else if (y == 0)
curset[x] = 1, ans += cnt[x];
else if (cnt[x] > cnt[y])
curset[y] = 1, ans += cnt[y];
else
curset[x] = 1, ans += cnt[x];
}
void del(int x) {
x = findfa(x);
if (curset[x]) {
ans -= cnt[x];
curset[x] = 0;
}
}
} NSPACE;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
NSPACE.solve();
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
int n, k, m, c, vas, cc = 1000000, kl;
vector<int> vv;
string s;
vector<vector<int>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<int>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
int a = la[i][0];
bool b1 = s[i] == '1' ? 1 : 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if (!b1) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
int b = la[i][1];
while (b != -1) {
b1 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
if (!b1) {
if (pr[a].l > pr[b].l) swap(a, b);
vas += pr[a].l;
pr[a].q ^= 1;
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (int i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const int INF = 1e9 + 7;
struct node {
int l, r;
node(int _l = 0, int _r = 0) : l(_l), r(_r){};
void operator+=(node x) {
l = min(l + x.l, INF);
r = min(r + x.r, INF);
}
int get() { return min(l, r); }
} val[N];
int root[N], rev[N];
pair<int, int> getRoot(int u) {
if (u == root[u]) {
return make_pair(u, 0);
} else {
pair<int, int> tmp = getRoot(root[u]);
root[u] = tmp.first;
rev[u] ^= tmp.second;
return make_pair(root[u], rev[u]);
}
}
int n, k;
vector<int> adj[N];
string s;
int main() {
cin >> n >> k;
cin >> s;
for (int i = 1; i <= k; i++) {
val[i] = node(1, 0);
root[i] = i;
rev[i] = 0;
int c;
cin >> c;
while (c--) {
int x;
cin >> x;
adj[x].push_back(i);
}
}
int curAns = 0;
for (int i = 1; i <= n; i++) {
int t = (s[i - 1] - '0') ^ 1;
if (adj[i].size() == 1) {
pair<int, int> u = getRoot(adj[i][0]);
curAns -= val[u.first].get();
val[u.first] += node((u.second == t) * INF, (u.second != t) * INF);
curAns += val[u.first].get();
}
if (adj[i].size() == 2) {
pair<int, int> u = getRoot(adj[i][0]);
pair<int, int> v = getRoot(adj[i][1]);
if (u.first != v.first) {
curAns -= val[u.first].get();
curAns -= val[v.first].get();
if (u.first > v.first) swap(u, v);
if (u.second ^ v.second ^ t) {
rev[v.first] = 1;
swap(val[v.first].l, val[v.first].r);
}
root[v.first] = u.first;
val[u.first] += val[v.first];
curAns += val[u.first].get();
}
}
cout << curAns << endl;
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] ? fa[u] = find(fa[u]) : u; }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += mix(k[i][0]);
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += mix(k[i][0]);
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (J[i].size() == 0) {
std::cout << R << std::endl;
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j & (x[i] ^ (p_j > 0));
coa[abs_p_j] = coa_j & (x[i] ^ (p_j < 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
std::cout << R << std::endl;
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
std::cout << R << std::endl;
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 300005;
const long long inf = 1ll << 50;
int ans = 0;
bool init[maxn];
int par[maxn], sz[maxn], cnt[maxn][2], col[maxn];
vector<int> vec[maxn];
int find(int x) {
if (par[x] == x) {
return x;
}
par[x] = find(par[x]);
col[x] = col[x] ^ col[par[x]];
return par[x];
}
int get_val(int x) { return min(cnt[x][0], cnt[x][1]); }
void merge(int X, int Y, bool same) {
int x = find(X);
int y = find(Y);
if (x == y) {
return;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
sz[y] += sz[x];
par[x] = y;
same = (same ^ col[X] ^ col[Y]);
ans -= get_val(x);
ans -= get_val(y);
col[x] = same;
cnt[y][0] += cnt[x][0 ^ same];
cnt[y][1] += cnt[x][1 ^ same];
ans += get_val(y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
sz[0] = (1 << 28);
cnt[0][0] = (1 << 28);
for (int i = 1; i <= k; i++) {
par[i] = i;
sz[i] += 1;
cnt[i][0] = 1;
cnt[i][1] = 0;
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
init[i] = s[i - 1] - '0';
}
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
int a;
for (int j = 0; j < c; j++) {
cin >> a;
vec[a].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
assert(vec[i].size() <= 2);
if (vec[i].size() == 0) {
cout << ans << "\n";
assert(init[i] == 1);
continue;
}
if (vec[i].size() == 1) {
vec[i].push_back(0);
}
merge(vec[i][0], vec[i][1], init[i] ^ 1);
cout << ans << "\n";
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
int n, k, m, c, vas, cc = 1000000, kl;
vector<int> vv;
string s;
vector<vector<int>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<int>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
int a = la[i][0];
bool b1 = s[i] == '1' ? 1 : 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if (!b1) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
int b = la[i][1];
while (b != -1) {
b1 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
if (!b1) {
pr[a].l < pr[b].l ? vas += pr[a].l, pr[a].q ^= 1 : vas += pr[b].l,
pr[b].q ^= 1;
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (int i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9 + 5;
const int N = 600010;
class DSU {
public:
int p[N], cost[N];
int n;
int ans = 0;
DSU(int _n) : n(_n) {
for (int i = 0; i < N / 2; i++) {
cost[2 * i + 1] = 1;
cost[2 * i] = 0;
}
cost[2 * n] = inf;
cost[2 * n + 1] = 0;
for (int i = 0; i < N; i++) {
p[i] = i;
}
}
int find(int x) { return (x == p[x] ? x : (p[x] = find(p[x]))); }
void unite(int x, int y, bool same) {
int x_true = find(2 * x + 1), y_true = find(2 * y + 1);
int x_false = find(2 * x), y_false = find(2 * y);
if (same) {
if (x_true != y_true) {
assert(x_false != y_false);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
p[x_true] = y_true;
cost[y_true] += cost[x_true];
p[x_false] = y_false;
cost[y_false] += cost[x_false];
ans += min(cost[y_true], cost[y_false]);
} else {
assert(x_false == y_false);
}
} else {
if (x_true != y_false) {
assert(x_false != y_true);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
p[x_true] = y_false;
cost[y_false] += cost[x_true];
p[x_false] = y_true;
cost[y_true] += cost[x_false];
ans += min(cost[y_true], cost[y_false]);
} else {
assert(x_false == y_true);
}
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<vector<int>> has(n);
for (int i = 0; i < k; i++) {
int size;
cin >> size;
while (size--) {
int x;
cin >> x;
--x;
has[x].push_back(i);
}
}
DSU dsu(n);
int dummy = n;
for (int i = 0; i < n; i++) {
if ((int)has[i].size() == 2) {
dsu.unite(has[i][0], has[i][1], s[i] == '1');
} else if ((int)has[i].size() == 1) {
dsu.unite(has[i].back(), dummy, s[i] == '0');
} else {
assert(s[i] == '1');
}
cout << dsu.ans << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
long long t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 1000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
long long a = la[i][0];
bool b1 = s[i] == '1' ? 1 : 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if (!b1) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
long long b = la[i][1];
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
long long kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;
int n, k, fa[maxn], tot[maxn], ans;
char s[maxn];
vector<int> v[maxn];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void join(int x, int y) {
x = find(x);
y = find(y);
if (x != y) fa[x] = y, tot[y] += tot[x];
}
int get(int x) { return min(tot[find(x)], tot[find(x + k)]); }
int main() {
scanf("%d%d%s", &n, &k, s + 1);
for (int i = (1); i <= (k); ++i) {
int x, c;
scanf("%d", &c);
while (c--) scanf("%d", &x), v[x].push_back(i);
}
for (int i = (1); i <= (2 * k + 1); ++i) fa[i] = i, tot[i] = (i <= k);
tot[2 * k + 1] = mod;
for (int i = (1); i <= (n); ++i) {
if (v[i].size() == 1) {
int tmp = v[i][0] + (s[i] == '0') * k;
ans -= get(v[i][0]);
join(tmp, 2 * k + 1);
ans += get(v[i][0]);
}
if (v[i].size() == 2) {
int x = v[i][0], y = v[i][1];
if (s[i] == '1' && find(x) != find(y)) {
ans -= (get(x) + get(y));
join(x, y);
join(x + k, y + k);
ans += get(x);
}
if (s[i] == '0' && find(x) != find(y + k)) {
ans -= (get(x) + get(y + k));
join(x, y + k);
join(x + k, y);
ans += get(x);
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct UF {
vector<int> e;
UF(int n) : e(n, -1) {}
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b];
e[b] = a;
return true;
}
};
char s[300030];
pair<int, int> a[300030];
vector<pair<int, int>> g[300030];
int t, n, k, m, x, d[300030], cnt[3][300030], ans;
void dfs(int v, int p, int s) {
if (s)
d[v] = d[p];
else
d[v] = 3 - d[p];
for (auto u : g[v])
if (!d[u.first]) dfs(u.first, v, u.second);
}
int main() {
scanf("%d%d%s", &n, &k, s + 1);
UF uf(k + 1);
d[n + 1] = 2;
for (int i = 1; i <= n; i++) s[i] -= '0';
for (int i = 1; i <= k; i++)
for (int j = !scanf("%d", &m); j < m; j++)
if (scanf("%d", &x), a[x].first) {
a[x].second = i;
g[a[x].first].emplace_back(i, s[x]);
g[i].emplace_back(a[x].first, s[x]);
} else
a[x].first = i;
for (int i = 1; i <= n; i++)
if (a[i].first && a[i].second == 0) g[0].emplace_back(a[i].first, s[i]);
for (int i = 0; i <= k; i++)
if (!d[i]) dfs(i, n + 1, 0);
for (int i = 1; i <= k; i++) cnt[d[i]][i] = 1;
cnt[1][0] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) {
int u = uf.find(a[i].first), v = uf.find(a[i].second);
if (uf.join(u, v)) {
ans -= min(cnt[1][u], cnt[2][u]);
ans -= min(cnt[1][v], cnt[2][v]);
for (int j = 1; j <= 2; j++)
cnt[j][u] = cnt[j][v] = cnt[j][u] + cnt[j][v];
ans += min(cnt[1][u], cnt[2][u]);
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long sz[300009], par[300009];
long long res;
long long n, k;
std::vector<long long> pos[300009];
long long getparent(long long x) {
if (par[x] == x) return x;
return par[x] = getparent(par[x]);
}
void merge(long long x, long long y) {
long long p1 = getparent(x);
long long p2 = getparent(y);
if (p1 == p2) return;
par[p1] = p2;
res -= min(sz[p1], sz[p1 ^ 1]);
res -= min(sz[p2], sz[p2 ^ 1]);
sz[p2] += sz[p1];
res += min(sz[p2], sz[p2 ^ 1]);
sz[p1] = 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
string x;
cin >> x;
for (long long int i = 0; i < 2 * k + 2; i++) {
par[i] = i;
sz[i] = (i) % 2;
}
for (long long int i = 0; i < k; i++) {
long long ss;
cin >> ss;
for (long long int j = 0; j < ss; j++) {
long long yy;
cin >> yy;
yy--;
pos[yy].push_back(i);
}
}
for (long long int i = 0; i < n; i++) {
if (pos[i].size() == 0) {
cout << res << '\n';
continue;
}
if (pos[i].size() == 1) {
if (x[i] == '1') {
long long pp = getparent(2 * pos[i][0] + 1);
res -= min(sz[pp], sz[pp ^ 1]);
sz[pp] = 1e18;
res += sz[pp ^ 1];
} else {
long long pp = getparent(2 * pos[i][0]);
res -= min(sz[pp], sz[pp ^ 1]);
sz[pp] = 1e18;
res += sz[pp ^ 1];
}
cout << res << '\n';
continue;
}
if (x[i] == '1') {
merge(2 * pos[i][0], 2 * pos[i][1]);
merge((2 * pos[i][0]) + 1, (2 * pos[i][1]) + 1);
} else {
merge(2 * pos[i][0], (2 * pos[i][1]) + 1);
merge((2 * pos[i][0]) + 1, 2 * pos[i][1]);
}
cout << res << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int pre[N], si[N], num[N / 2], n, m;
bool vis[N];
char s[N];
vector<vector<int> > v(N / 2);
int findd(int x) {
if (pre[x] == x) return x;
return pre[x] = findd(pre[x]);
}
inline int id(int x) { return x > m ? x - m : x; }
int un(int a, int b) {
int x = findd(a);
int y = findd(b);
if (x == y) return -1;
int aa = id(x);
int bb = id(y);
if (aa < bb) {
pre[y] = x, si[x] += si[y];
if (vis[y]) vis[x] = 1;
return aa;
} else {
pre[x] = y, si[y] += si[x];
if (vis[x]) vis[y] = 1;
return bb;
}
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d", &a);
while (a--) {
scanf("%d", &b);
v[b].push_back(i);
}
}
for (int i = 1; i <= m; i++) {
pre[i] = i, si[i] = 1;
pre[i + m] = i + m, si[i + m] = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
if (v[i].size() == 1) {
int a = findd(v[i][0]);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[a];
num[a] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b + m);
un(b, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
} else {
if (v[i].size() == 1) {
int a = findd(v[i][0] + m);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[a];
num[a] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b);
un(b + m, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x;
vector<int> vec[300010];
char c[300010];
int fa[300010 << 1], sz[300010 << 1];
int lim[300010];
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d%s", &n, &k, c + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &m);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
fa[i * 2 - 1] = i * 2 - 1;
sz[i * 2 - 1] = 1;
fa[i * 2] = i * 2;
sz[i * 2] = 0;
lim[i] = -1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
int x = vec[i][0];
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f0 / 2] == -1)
ans -= min(s0, s1);
else
ans -= lim[f0 / 2] ? s1 : s0;
lim[f0 / 2] = (c[i] == '0');
ans += lim[f0 / 2] ? s1 : s0;
} else if (vec[i].size() == 2) {
int x = vec[i][0], y = vec[i][1];
int fx1 = find(x * 2 - 1), fx0 = find(x * 2);
int sx1 = sz[fx1], sx0 = sz[fx0];
int fy1 = find(y * 2 - 1), fy0 = find(y * 2);
int sy1 = sz[fy1], sy0 = sz[fy0];
if (fx0 != fy0 && fx0 != fy1) {
if (lim[fx0 / 2] == -1)
ans -= min(sx0, sx1);
else
ans -= lim[fx0 / 2] ? sx1 : sx0;
if (lim[fy0 / 2] == -1)
ans -= min(sy0, sy1);
else
ans -= lim[fy0 / 2] ? sy1 : sy0;
if (c[i] == '1') {
fa[fx1] = fy1;
fa[fx0] = fy0;
sz[fy1] += sz[fx1];
sz[fy0] += sz[fx0];
if (lim[fx0 / 2] != -1) lim[fy0 / 2] = lim[fx0 / 2];
} else {
fa[fx1] = fy0;
fa[fx0] = fy1;
sz[fy0] += sz[fx1];
sz[fy1] += sz[fx0];
if (lim[fx0 / 2] != -1) lim[fy0 / 2] = lim[fx0 / 2] ^ 1;
}
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f0 / 2] == -1)
ans += min(s0, s1);
else
ans += lim[f0 / 2] ? s1 : s0;
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 10000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() == 2) {
long long a = la[i][0], b = la[i][1];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
int kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else {
kl = pr[a].l + pr[b].l;
}
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
} else if (la[i].size() == 1) {
bool b = 0;
int a = la[i][0];
while (a != -1) {
b ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if ((b and s[i] == '1') or (!b and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
long long t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, kl, cc = 1000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
long long a = la[i][0];
bool b1 = 0, b2 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
long long b = la[i][1];
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a == b) continue;
b1 ^= b2;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
pr[a].l < pr[b].l ? vas += pr[a].l, pr[a].q ^= 1 : vas += pr[b].l,
pr[b].q ^= 1;
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[630000];
int fa[630000];
int val[630000];
vector<int> belong[630000];
int Push(int x) { return x; }
int Unpush(int x) { return m + x; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
int u = find(x), v = find(y);
if (u == v) return;
val[v] += val[u];
fa[u] = v;
}
bool vis[630000];
int value(int x) {
if (vis[find(x)]) return 1000000000;
return val[find(x)];
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m + m; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
val[Push(i)] = 1;
int c;
scanf("%d", &c);
for (int j = 1; j <= c; j++) {
int x;
scanf("%d", &x);
belong[x].push_back(i);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (belong[i].size() == 0) {
} else if (belong[i].size() == 1) {
int v = belong[i][0];
ans -= min(value(Push(v)), value(Unpush(v)));
if (s[i] == '0') {
vis[find(Unpush(v))] = 1;
} else {
vis[find(Push(v))] = 1;
}
ans += min(value(Push(v)), value(Unpush(v)));
} else {
int u = belong[i][0], v = belong[i][1];
if (find(Push(u)) == find(Push(v)) || find(Push(u)) == find(Unpush(v)) ||
find(Push(v)) == find(Unpush(u)) ||
find(Unpush(u)) == find(Unpush(v))) {
} else if (s[i] == '0') {
ans -= min(value(Push(u)), value(Unpush(u)));
ans -= min(value(Push(v)), value(Unpush(v)));
merge(Push(u), Unpush(v));
merge(Push(v), Unpush(u));
ans += min(value(Push(u)), value(Unpush(u)));
} else {
ans -= min(value(Push(u)), value(Unpush(u)));
ans -= min(value(Push(v)), value(Unpush(v)));
merge(Push(u), Push(v));
merge(Unpush(u), Unpush(v));
ans += min(value(Push(u)), value(Unpush(u)));
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
vector<char> active;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
if (r_st[v] != 2 && r_st[res] == 2) {
r_st[res] = r_st[v] ^ p_xor[v];
}
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
if (active[p_v]) {
cur_ans -= get_vrt_ans(p_v);
}
if (active[p_u]) {
cur_ans -= get_vrt_ans(p_u);
}
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[p_u] != 2) {
if (r_st[p_v] != 2) {
assert(r_st[p_v] == (r_st[p_u] ^ p_xor[p_u]));
} else {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
active[p_v] = true;
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()),
p(n),
sz(n, 1),
p_xor(n),
r_st(r_st),
ans(n),
active(n, false) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void activate(int v) {
int v_prv = v;
v = dsu_get(v);
if (!active[v]) {
if (r_st[v] == 2 && r_st[v_prv] != 2) {
r_st[v] = p_xor[v_prv] ^ r_st[v_prv];
} else if (r_st[v_prv] != 2) {
assert(r_st[v] == (p_xor[v_prv] ^ r_st[v_prv]));
}
cur_ans += get_vrt_ans(v);
active[v] = true;
}
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(mask_st);
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.activate(v_mask[i][0]);
}
cout << dsu.get_ans() << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long sz[300009], par[300009];
long long res;
long long n, k;
std::vector<long long> pos[300009];
long long getparent(long long x) {
if (par[x] == x) return x;
return par[x] = getparent(par[x]);
}
void merge(long long x, long long y) {
long long p1 = getparent(x);
long long p2 = getparent(y);
if (p1 == p2) return;
par[p1] = p2;
res -= min(sz[p1], sz[p1 ^ 1]);
res -= min(sz[p2], sz[p2 ^ 1]);
sz[p2] += sz[p1];
res += min(sz[p2], sz[p2 ^ 1]);
sz[p1] = 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
string x;
cin >> x;
for (long long int i = 0; i < 2 * k + 2; i++) {
par[i] = i;
sz[i] = (i) % 2;
}
for (long long int i = 0; i < k; i++) {
long long ss;
cin >> ss;
for (long long int j = 0; j < ss; j++) {
long long yy;
cin >> yy;
yy--;
pos[yy].push_back(i);
}
}
for (long long int i = 0; i < n; i++) {
if (pos[i].size() == 0) {
cout << res << '\n';
continue;
}
if (pos[i].size() == 1) {
if (x[i] == '1') {
long long pp = getparent(2 * pos[i][0] + 1);
res -= min(sz[pp], sz[pp ^ 1]);
sz[pp] = 1e18;
res += sz[pp ^ 1];
} else {
long long pp = getparent(2 * pos[i][0]);
res -= min(sz[pp], sz[pp ^ 1]);
sz[pp] = 1e10;
res += sz[pp ^ 1];
}
cout << res << '\n';
continue;
}
if (x[i] == '1') {
merge(2 * pos[i][0], 2 * pos[i][1]);
merge((2 * pos[i][0]) + 1, (2 * pos[i][1]) + 1);
} else {
merge(2 * pos[i][0], (2 * pos[i][1]) + 1);
merge((2 * pos[i][0]) + 1, 2 * pos[i][1]);
}
cout << res << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] ? fa[u] = find(fa[u]) : u; }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]);
ans += mix(k[i][1] + kk);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += mix(k[i][0]);
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
vector<char> active;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
if (active[p_v]) {
cur_ans -= get_vrt_ans(p_v);
}
if (active[p_u]) {
cur_ans -= get_vrt_ans(p_u);
}
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[u] != 2) {
if (r_st[v] != 2) {
} else {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
active[p_v] = true;
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()),
p(n),
sz(n, 1),
p_xor(n),
r_st(r_st),
ans(n),
active(n, false) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void activate(int v) {
if (!active[v]) {
int v_prv = v;
v = dsu_get(v);
if (r_st[v] == 2 && r_st[v_prv] != 2) {
r_st[v] = p_xor[v_prv] ^ r_st[v_prv];
} else if (r_st[v_prv] != 2) {
assert(r_st[v] == (p_xor[v_prv] ^ r_st[v_prv]));
}
cur_ans += get_vrt_ans(v);
active[v] = true;
}
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(mask_st);
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.activate(v_mask[i][0]);
}
cout << dsu.get_ans() << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long inf = 1000000;
const int N = 300000;
vector<int> tree(2 * N);
vector<int> ranks(2 * N, 0);
char str[N];
int find(int x) { return x == tree[x] ? x : tree[x] = find(tree[x]); }
void link(int x, int y) {
if (ranks[x] > ranks[y]) {
tree[y] = x;
} else {
tree[x] = y;
if (ranks[x] == ranks[y]) {
ranks[y]++;
}
}
}
long long sum = 0;
vector<vector<int>> graph(N);
vector<int> dists(2 * N);
bool visited[2 * N] = {false};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin.exceptions(cin.failbit);
int n, k;
cin >> n >> k;
cin >> str;
for (int i = 0; i < n; i++) {
tree[i] = i;
tree[i + N] = i + N;
dists[i] = 1;
dists[i + N] = 0;
}
for (int i = 0; i < k; i++) {
int len;
cin >> len;
for (int a = 0; a < len; a++) {
int e;
cin >> e;
e--;
graph[e].push_back(i);
}
}
for (int i = 0; i < n; i++) {
if (graph[i].empty()) {
cout << sum << "\n";
continue;
}
if (graph[i].size() == 1) {
if (str[i] == '0') {
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
sum++;
visited[graph[i][0] + N] = true;
dists[graph[i][0] + N] = inf;
} else {
int x = find(graph[i][0]);
int y = find(graph[i][0] + N);
sum -= min(dists[x], dists[y]);
dists[y] = inf;
sum += dists[x];
}
} else {
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
visited[graph[i][0] + N] = true;
dists[graph[i][0]] = inf;
} else {
int x = find(graph[i][0]);
int y = find(graph[i][0] + N);
sum -= min(dists[x], dists[y]);
dists[x] = inf;
sum += dists[y];
}
}
} else {
int x1 = find(graph[i][0]);
int x2 = find(graph[i][0] + N);
int y1 = find(graph[i][1]);
int y2 = find(graph[i][1] + N);
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
visited[graph[i][0] + N] = true;
}
if (!visited[graph[i][1]]) {
visited[graph[i][1]] = true;
visited[graph[i][1] + N] = true;
}
sum -= min(dists[x1], dists[x2]);
sum -= min(dists[y1], dists[y2]);
if (str[i] == '0') {
if (x1 == y2) {
sum += min(dists[x1], dists[x2]);
} else if (dists[x1] == inf) {
tree[y2] = x1;
y2 = x1;
} else if (dists[y2] == inf) {
tree[x1] = y2;
x1 = y2;
} else {
tree[y2] = x1;
dists[x1] += dists[y2];
y2 = x1;
}
if (dists[x2] == inf) {
tree[y1] = x2;
y1 = x2;
} else if (dists[y1] == inf) {
tree[x2] = y1;
x2 = y1;
} else if (x2 != y1) {
tree[x2] = y1;
dists[y1] += dists[x2];
x2 = y1;
}
sum += min(dists[x1], dists[x2]);
} else {
if (x1 == y1) {
sum += min(dists[x1], dists[x2]);
} else if (dists[x1] == inf) {
tree[y1] = x1;
y1 = x1;
} else if (dists[y1] == inf) {
tree[x1] = y1;
x1 = y1;
} else {
tree[y1] = x1;
dists[x1] += dists[y1];
y1 = x1;
}
if (dists[x2] == inf) {
tree[y2] = x2;
y2 = x2;
} else if (dists[y2] == inf) {
tree[x2] = y2;
x2 = y2;
} else if (x2 != y2) {
tree[x2] = y2;
dists[y2] += dists[x2];
x2 = y2;
}
sum += min(dists[x1], dists[x2]);
}
}
cout << sum << "\n";
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9 + 5;
const int N = 600010;
class DSU {
public:
int p[N], cost[N];
int n;
int ans = 0;
DSU(int _n) : n(_n) {
for (int i = 0; i < n; i++) {
cost[2 * i + 1] = 1;
cost[2 * i] = 0;
}
cost[2 * n] = inf;
cost[2 * n + 1] = 0;
for (int i = 0; i < 2 * n + 2; i++) {
p[i] = i;
}
}
int find(int x) { return (x == p[x] ? x : (p[x] = find(p[x]))); }
bool unite(int x, int y, bool same) {
int x_true = find(2 * x + 1), y_true = find(2 * y + 1);
int x_false = find(2 * x), y_false = find(2 * y);
if (same) {
if (x_true != y_true) {
assert(x_false != y_false);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
p[x_true] = y_true;
cost[y_true] += cost[x_true];
p[x_false] = y_false;
cost[y_false] += cost[x_false];
ans += min(cost[find(y_true)], cost[find(y_false)]);
return true;
} else {
assert(x_false == y_false);
return false;
}
} else {
if (x_true != y_false) {
assert(x_false != y_true);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
p[x_true] = y_false;
cost[y_false] += cost[x_true];
p[x_false] = y_true;
cost[y_true] += cost[x_false];
ans += min(cost[find(y_false)], cost[find(y_true)]);
return true;
} else {
assert(x_false == y_true);
return false;
}
}
return false;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<vector<int>> has(n);
for (int i = 0; i < k; i++) {
int size;
cin >> size;
while (size--) {
int x;
cin >> x;
--x;
has[x].push_back(i);
}
}
DSU dsu(n);
int dummy = n;
for (int i = 0; i < n; i++) {
if ((int)has[i].size() == 2) {
dsu.unite(has[i][0], has[i][1], s[i] == '1');
} else if ((int)has[i].size() == 1) {
dsu.unite(has[i].back(), dummy, s[i] == '0');
} else {
assert(s[i] == '1');
}
cout << dsu.ans << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int pre[N], si[N], num[N / 2], n, m;
bool vis[N];
char s[N];
vector<vector<int> > v(N / 2);
int findd(int x) {
if (pre[x] == x) return x;
pre[x] = findd(pre[x]);
if (vis[x]) vis[pre[x]] = 1;
return pre[x];
}
inline int id(int x) { return x > m ? x - m : x; }
int un(int a, int b) {
int x = findd(a);
int y = findd(b);
if (x == y) return -1;
int aa = id(x);
int bb = id(y);
if (aa < bb) {
pre[y] = x, si[x] += si[y];
if (vis[y]) vis[x] = 1;
return aa;
} else {
pre[x] = y, si[y] += si[x];
if (vis[x]) vis[y] = 1;
return bb;
}
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d", &a);
while (a--) {
scanf("%d", &b);
v[b].push_back(i);
}
}
for (int i = 1; i <= m; i++) {
pre[i] = i, si[i] = 1;
pre[i + m] = i + m, si[i + m] = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
if (v[i].size() == 1) {
int a = findd(v[i][0]);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[a];
num[id(a)] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b + m);
un(b, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
} else {
if (v[i].size() == 1) {
int a = findd(v[i][0] + m);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[a];
num[id(a)] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b);
un(b + m, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
template <class O, class Z, class A, class B, class C, class D, class E>
void debug(O& out, const Z& p, const A& J, const B& a, const C& coa, const D& S,
const E& coS) {
out << "p:";
for (auto x : p) out << " " << x;
out << std::endl;
out << "J:";
for (auto x : J) {
out << " {";
for (auto y : x) out << " " << y;
out << " }";
}
out << std::endl;
out << " a:";
for (auto x : a) out << " " << x;
out << std::endl;
out << "coa:";
for (auto x : coa) out << " " << x;
out << std::endl;
out << " S:";
for (auto x : S) out << " " << x;
out << std::endl;
out << "coS:";
for (auto x : coS) out << " " << x;
out << std::endl;
}
template <class O, class Z, class X, class A, class B, class C, class D,
class E>
void debug(O& out, int i, Z& p, const X& x, const A& J, const B& a,
const C& coa, const D& S, const E& coS) {
for (auto y : J[i])
out << x[i] << ' ' << y << ' ' << p[y] << ' ' << p[abs(get_p(p, y))] << ' '
<< a[abs(get_p(p, y))] << ' ' << coa[abs(get_p(p, y))] << ' '
<< S[abs(get_p(p, y))] << ' ' << coS[abs(get_p(p, y))] << ' ';
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (i == 10781) debug(std::cout, i, p, x, J, a, coa, S, coS);
if (J[i].size() == 0) {
if (n < 1000) std::cout << R << std::endl;
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j && !x[i];
coa[abs_p_j] = coa_j && x[i];
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct UF {
vector<int> e;
UF(int n) : e(n, -1) {}
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b];
e[b] = a;
return true;
}
};
char s[300030];
pair<int, int> a[300030];
vector<pair<int, int>> g[300030];
int t, n, k, m, x, d[300030], cnt[3][300030], tot[300030], ans;
void dfs(int v, int p, int s) {
if (s)
d[v] = d[p];
else
d[v] = 3 - d[p];
for (auto u : g[v])
if (!d[u.first]) dfs(u.first, v, u.second);
}
int main() {
scanf("%d%d%s", &n, &k, s + 1);
UF uf(k + 1);
d[n + 1] = 2;
for (int i = 1; i <= n; i++) s[i] -= '0';
for (int i = 1; i <= k; i++)
for (int j = !scanf("%d", &m); j < m; j++)
if (scanf("%d", &x), a[x].first) {
a[x].second = i;
g[a[x].first].emplace_back(i, s[x]);
g[i].emplace_back(a[x].first, s[x]);
} else
a[x].first = i;
for (int i = 1; i <= n; i++)
if (a[i].first && a[i].second == 0) g[0].emplace_back(a[i].first, s[i]);
for (int i = 0; i <= k; i++)
if (!d[i]) dfs(i, n + 1, 0);
for (int i = 1; i <= k; i++) cnt[d[i]][i] = 1;
cnt[1][0] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) {
int u = uf.find(a[i].first), v = uf.find(a[i].second);
uf.join(u, v);
if (u != v) {
ans -= tot[u] + tot[v];
for (int j = 1; j <= 2; j++)
cnt[j][u] = cnt[j][v] = cnt[j][u] + cnt[j][v];
ans += min(cnt[1][u], cnt[2][u]);
tot[u] = tot[v] = min(cnt[1][u], cnt[2][u]);
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define endl '\n'
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(0,1e9);
//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
const int N = 3e5 + 5;
int p[N], diff[N], par[N], szz[N], ans = 0;
pii sz[N];
int find(int u){
if(u == p[u])
return u;
return p[u] = find(p[u]);
}
bool same(int u){
if(u == par[u])
return 1;
return same(par[u]) ^ diff[u];
}
void join(int u, int v, bool same_col){
int _u = find(u), _v = find(v);
if(szz[_u] > szz[_v]){
swap(u, v);
swap(_u, _v);
}
if(_u != _v){
p[_u] = _v;
szz[_v] += szz[_u];
if(!same(u))
same_col ^= 1;
if(!same(v))
same_col ^= 1;
ans -= min(sz[_u].fi, sz[_u].se);
ans -= min(sz[_v].fi, sz[_v].se);
if(same_col)
sz[_v].fi += sz[_u].fi, sz[_v].se += sz[_u].se;
else
sz[_v].fi += sz[_u].se, sz[_v].se += sz[_u].fi;
par[u] = v;
diff[u] = !same_col;
ans += min(sz[_v].fi, sz[_v].se);
}
}
void solve(){
int n, k;
cin >> n >> k;
vvi g(n+1);
fr(i,1,k)
p[i] = i, par[i] = i, sz[i] = {1, 0}, szz[i] = 1;
string s;
cin >> s;
fr(i,1,k){
int x;
cin >> x;
rep(_,0,x){
int y;
cin >> y;
g[y].pb(i);
}
}
fr(i,1,n){
if(sz(g[i]) == 2){
join(g[i][0], g[i][1], s[i-1] - '0');
}
else if(sz(g[i]) == 1){
int col = !same(g[i][0]);
int x = find(g[i][0]);
ans -= min(sz[x].fi, sz[x].se);
if(s[i-1] == '1'){
if(col)
sz[x].se = infi, ans += sz[x].fi;
else
sz[x].fi = infi, ans += sz[x].se;
}
else{
if(col)
sz[x].fi = infi, ans += sz[x].se;
else
sz[x].se = infi, ans += sz[x].fi;
}
}
cout << ans << endl;
}
}
signed main() {
#ifndef ONLINE_JUDGE
(void)!freopen("input.txt","r",stdin);
(void)!freopen ("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
int powm(int a, int b){
int res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int divide(int a, int b) {
return (a % mod) * powm(b % mod, mod - 2) % mod;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct DS {
int rank, cost, parent;
bool forced;
} ds[4 * 300100];
int ans;
char status[300100];
vector<int> subs[300100];
int neg(int n) { return n + 300100; }
int find(int n) {
if (n == ds[n].parent) return n;
return ds[n].parent = find(ds[n].parent);
}
void join(int n1, int n2) {
int s1 = find(n1), s2 = find(n2);
if (s1 != s2) {
if (ds[s1].rank < ds[s2].rank) swap(s1, s2);
ds[s2].parent = s1;
ds[s1].cost += ds[s2].cost;
ds[s1].rank += ds[s2].rank;
ds[s1].forced |= ds[s2].forced;
}
}
int main() {
int n, k, qtd, v, s1, s2, ns1, ns2, mini;
scanf("%d %d", &n, &k);
getchar();
scanf("%s", status);
for (int i = 0; i < k; i++) {
scanf("%d", &qtd);
for (int j = 0; j < qtd; j++) {
scanf("%d", &v);
subs[v].emplace_back(i);
}
ds[i].rank = 1;
ds[i].cost = 1;
ds[i].parent = i;
ds[i].forced = false;
ds[neg(i)].rank = 1;
ds[neg(i)].cost = 0;
ds[neg(i)].parent = neg(i);
ds[neg(i)].forced = false;
}
for (int i = 1; i <= n; i++) {
if (status[i - 1] == '1') {
if (subs[i].size() == 2) {
s1 = find(subs[i][0]);
s2 = find(subs[i][1]);
ns1 = find(neg(subs[i][0]));
ns2 = find(neg(subs[i][1]));
mini = -1;
if (ds[s1].forced) ans -= ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans -= ds[ns1].cost, mini = 0;
if (mini == -1) {
mini = min(ds[s1].cost, ds[ns1].cost);
ans -= mini;
}
mini = -1;
if (ds[s2].forced and s1 != s2) ans -= ds[s2].cost, mini = 0;
if (ds[ns2].forced and s1 != s2) ans -= ds[ns2].cost, mini = 0;
if (mini == -1 and s1 != s2 and s1 != ns2) {
mini = min(ds[s2].cost, ds[ns2].cost);
ans -= mini;
}
join(s1, s2);
join(ns1, ns2);
s1 = find(subs[i][0]);
s2 = find(subs[i][1]);
ns1 = find(neg(subs[i][0]));
ns2 = find(neg(subs[i][1]));
mini = -1;
if (ds[s1].forced) ans += ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans += ds[ns1].cost, mini = 0;
if (mini == -1) mini = min(ds[s1].cost, ds[ns1].cost);
ans += mini;
} else {
if (subs[i].size() == 1) {
s1 = find(subs[i][0]);
ns1 = find(neg(subs[i][0]));
if (not ds[ns1].forced) {
ans -= min(ds[s1].cost, ds[ns1].cost);
ds[ns1].forced = true;
ans += ds[ns1].cost;
}
}
}
} else {
if (subs[i].size() == 2) {
s1 = find(subs[i][0]);
ns2 = find(neg(subs[i][1]));
ns1 = find(neg(subs[i][0]));
s2 = find(subs[i][1]);
mini = -1;
if (ds[s1].forced) ans -= ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans -= ds[ns1].cost, mini = 0;
if (mini == -1) {
mini = min(ds[s1].cost, ds[ns1].cost);
ans -= mini;
}
mini = -1;
if (ds[s2].forced and s1 != s2) ans -= ds[s2].cost, mini = 0;
if (ds[ns2].forced and s1 != s2) ans -= ds[ns2].cost, mini = 0;
if (mini == -1 and s1 != s2 and s1 != ns2) {
mini = min(ds[s2].cost, ds[ns2].cost);
ans -= mini;
}
join(s1, ns2);
join(ns1, s2);
s1 = find(subs[i][0]);
ns2 = find(neg(subs[i][1]));
ns1 = find(neg(subs[i][0]));
s2 = find(subs[i][1]);
mini = -1;
if (ds[s1].forced) ans += ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans += ds[ns1].cost, mini = 0;
if (mini == -1) mini = min(ds[s1].cost, ds[ns1].cost);
ans += mini;
} else {
if (subs[i].size() == 1) {
s1 = find(subs[i][0]);
ns1 = find(neg(subs[i][0]));
if (not ds[s1].forced) {
ans -= min(ds[s1].cost, ds[ns1].cost);
ds[s1].forced = true;
ans += ds[s1].cost;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
long long t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 1000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
fin.tie(0);
cout.tie(0);
cerr.tie(0);
fin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
fin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
fin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
long long a = la[i][0];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
long long b = la[i][1];
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
long long kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
char op[300005];
int n, k, fa[300005], v[300005], num[300005], sz[300005], ans, gx[300005];
vector<int> vec[300005];
int ff(int x) {
if (fa[x] == x) return x;
int f = ff(fa[x]);
v[x] ^= v[fa[x]];
return fa[x] = f;
}
inline void merge(int x, int y, int p) {
if (ff(x) == ff(y)) return;
int fx = ff(x), fy = ff(y);
if (fx == 0) swap(fx, fy);
v[fx] = p ^ v[x] ^ v[y];
sz[fy] += sz[fx];
num[fy] += v[fx] ? sz[fx] - num[fx] : num[fx];
ans -= gx[fy] + gx[fx];
if (fy == 0)
gx[fy] = num[fy];
else
gx[fy] = min(sz[fy] - num[fy], num[fy]);
ans += gx[fy], fa[ff(y)] = ff(x);
}
int main() {
scanf("%d%d%s", &n, &k, op + 1);
for (int i = 0; i <= k; i++) fa[i] = i, sz[i] = 1;
for (int i = 1, c, x; i <= k; i++) {
scanf("%d", &c);
while (c--) scanf("%d", &x), vec[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) vec[i].push_back(0);
}
for (int i = 1; i <= n; i++) {
if (!vec[i].empty()) merge(vec[i][0], vec[i][1], op[i] == '0');
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long inf = 1000000;
const int N = 300002;
vector<int> tree(2 * N);
vector<int> ranks(2 * N, 0);
char str[N];
int find(int x) { return x == tree[x] ? x : tree[x] = find(tree[x]); }
void link(int x, int y) {
if (ranks[x] > ranks[y]) {
tree[y] = x;
} else {
tree[x] = y;
if (ranks[x] == ranks[y]) {
ranks[y]++;
}
}
}
long long sum = 0;
vector<vector<int>> graph(N);
vector<int> dists(2 * N);
bool visited[2 * N] = {false};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin.exceptions(cin.failbit);
int n, k;
cin >> n >> k;
cin >> str;
for (int i = 0; i < n; i++) {
tree[i] = i;
tree[i + N] = i + N;
dists[i] = 1;
dists[i + N] = 0;
}
for (int i = 0; i < k; i++) {
int len;
cin >> len;
for (int a = 0; a < len; a++) {
int e;
cin >> e;
e--;
graph[e].push_back(i);
if (k == N - 2 && e < 3) {
cout << i << " " << e << '\n';
}
}
}
for (int i = 0; i < n; i++) {
if (graph[i].empty()) {
cout << sum << "\n";
continue;
}
if (graph[i].size() == 1) {
if (str[i] == '0') {
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
sum++;
visited[graph[i][0] + N] = true;
dists[graph[i][0] + N] = inf;
} else {
int x = find(graph[i][0]);
int y = find(graph[i][0] + N);
sum -= min(dists[x], dists[y]);
dists[y] = inf;
sum += dists[x];
}
} else {
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
visited[graph[i][0] + N] = true;
dists[graph[i][0]] = inf;
} else {
int x = find(graph[i][0]);
int y = find(graph[i][0] + N);
sum -= min(dists[x], dists[y]);
dists[x] = inf;
sum += dists[y];
}
}
} else {
if (graph[i].size() > 2) {
cout << "?????\n";
}
int x1 = find(graph[i][0]);
int x2 = find(graph[i][0] + N);
int y1 = find(graph[i][1]);
int y2 = find(graph[i][1] + N);
if (!visited[graph[i][0]]) {
visited[graph[i][0]] = true;
visited[graph[i][0] + N] = true;
}
if (!visited[graph[i][1]]) {
visited[graph[i][1]] = true;
visited[graph[i][1] + N] = true;
}
sum -= min(dists[x1], dists[x2]);
sum -= min(dists[y1], dists[y2]);
if (str[i] == '0') {
if (x1 == y2) {
sum += min(dists[x1], dists[x2]);
} else if (dists[x1] == inf) {
tree[y2] = x1;
y2 = x1;
} else if (dists[y2] == inf) {
tree[x1] = y2;
x1 = y2;
} else {
tree[y2] = x1;
dists[x1] += dists[y2];
y2 = x1;
}
if (dists[x2] == inf) {
tree[y1] = x2;
y1 = x2;
} else if (dists[y1] == inf) {
tree[x2] = y1;
x2 = y1;
} else if (x2 != y1) {
tree[x2] = y1;
dists[y1] += dists[x2];
x2 = y1;
}
sum += min(dists[x1], dists[x2]);
} else {
if (x1 == y1) {
sum += min(dists[x1], dists[x2]);
} else if (dists[x1] == inf) {
tree[y1] = x1;
y1 = x1;
} else if (dists[y1] == inf) {
tree[x1] = y1;
x1 = y1;
} else {
tree[y1] = x1;
dists[x1] += dists[y1];
y1 = x1;
}
if (dists[x2] == inf) {
tree[y2] = x2;
y2 = x2;
} else if (dists[y2] == inf) {
tree[x2] = y2;
x2 = y2;
} else if (x2 != y2) {
tree[x2] = y2;
dists[y2] += dists[x2];
x2 = y2;
}
sum += min(dists[x1], dists[x2]);
}
}
cout << sum << "\n";
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
char op[300005];
int n, k, fa[300005], v[300005], num[300005], sz[300005], ans, gx[300005];
vector<int> vec[300005];
int ff(int x) {
if (fa[x] == x) return x;
int f = ff(fa[x]);
v[x] ^= v[fa[x]];
return fa[x] = f;
}
inline void merge(int x, int y, int p) {
if (ff(x) == ff(y)) return;
int fx = ff(x), fy = ff(y);
if (fx == 0) swap(fx, fy), swap(x, y);
v[fx] = p ^ v[x] ^ v[y];
sz[fy] += sz[fx];
num[fy] += v[fx] ? sz[fx] - num[fx] : num[fx];
ans -= gx[fy] + gx[fx];
if (fy == 0)
gx[fy] = num[fy];
else
gx[fy] = min(sz[fy] - num[fy], num[fy]);
ans += gx[fy], fa[ff(x)] = ff(y);
}
int main() {
scanf("%d%d%s", &n, &k, op + 1);
for (int i = 0; i <= k; i++) fa[i] = i, sz[i] = 1;
for (int i = 1, c, x; i <= k; i++) {
scanf("%d", &c);
while (c--) scanf("%d", &x), vec[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) vec[i].push_back(0);
}
for (int i = 1; i <= n; i++) {
if (!vec[i].empty()) merge(vec[i][0], vec[i][1], op[i] == '0');
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const int INF = 1e9 + 7;
struct node {
int l, r;
node(int _l = 0, int _r = 0) : l(_l), r(_r){};
void operator+=(node x) {
l = min(l + x.l, INF);
r = min(r + x.r, INF);
}
};
node val[N];
pair<int, int> root[N];
void build(int n = 0) {
for (int i = 1; i <= n; i++) val[i] = node(1, 0), root[i] = make_pair(i, 0);
}
pair<int, int> getRoot(pair<int, int> u) {
while (u.first != root[u.first].first) {
u.second ^= root[u.first].second;
u.first = root[u.first].first;
}
return u;
}
void join(int x, int y, bool s) {
pair<int, int> u = getRoot(make_pair(x, 0));
pair<int, int> v = getRoot(make_pair(y, s));
if (u.first == v.first) return;
if (u.first > v.first) swap(u, v);
if (u.second ^ v.second) swap(val[v.first].l, val[v.first].r);
root[v.first] = make_pair(u.first, u.second ^ v.second);
val[u.first] += val[v.first];
val[v.first] = node(0, 0);
}
int get(int u) {
pair<int, int> tmp = getRoot(make_pair(u, 0));
return min(val[tmp.first].l, val[tmp.first].r);
}
int update(int x, bool s) {
pair<int, int> u = getRoot(make_pair(x, s ^ 1));
if (u.second == 0)
val[u.first].l = INF;
else
val[u.first].r = INF;
}
int n, k;
vector<int> adj[N];
string s;
int main() {
cin >> n >> k;
cin >> s;
for (int i = 1; i <= k; i++) {
int si;
cin >> si;
for (int j = 1; j <= si; j++) {
int x;
cin >> x;
adj[x].push_back(i);
}
}
int curAns = 0;
build(n);
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 1) {
curAns -= get(adj[i][0]);
update(adj[i][0], (s[i - 1] == '1'));
curAns += get(adj[i][0]);
}
if (adj[i].size() == 2) {
pair<int, int> u = getRoot(make_pair(adj[i][0], 0));
pair<int, int> v = getRoot(make_pair(adj[i][1], 0));
if (u.first != v.first) {
curAns -= get(u.first);
curAns -= get(v.first);
join(adj[i][0], adj[i][1], (s[i - 1] == '0'));
curAns += get(adj[i][0]);
}
}
cout << curAns << endl;
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 900000;
int papa[MAXN], peso[MAXN], custo[MAXN];
int k;
bool obg[MAXN];
int find(int a) {
if (papa[a] == -1) return a;
return papa[a] = find(papa[a]);
}
bool juntos(int a, int b) { return find(a) == find(b); }
void uni(int a, int b) {
a = find(a);
b = find(b);
if (peso[a] < peso[b]) swap(a, b);
peso[a] += peso[b];
papa[b] = a;
custo[a] += custo[b];
obg[a] = obg[a] || obg[b];
}
int con(int a) {
a = find(a);
int aa = find(a + k);
if (obg[a]) return custo[a];
if (obg[aa]) return custo[aa];
return min(custo[a], custo[aa]);
}
char word[MAXN];
vector<int> lam[MAXN];
int main() {
int n;
scanf("%d%d", &n, &k);
scanf("%s", word);
for (int i = 0; i < k; i++) {
int c;
scanf("%d", &c);
for (int j = 0; j < c; j++) {
int num;
scanf("%d", &num);
lam[num - 1].emplace_back(i);
}
}
memset(papa, -1, sizeof papa);
int res = 0;
for (int i = 0; i < k; i++) custo[i] = 1;
for (int i = 0; i < n; i++) {
if (lam[i].size() == 0) {
} else if (lam[i].size() == 1) {
int a = find(lam[i][0]);
res -= con(a);
obg[find(word[i] == '0' ? a : a + k)] = true;
res += con(a);
} else if (lam[i].size() == 2) {
int a = lam[i][0];
int b = lam[i][1];
if (!juntos(a, b) && !juntos(a, b + k)) {
res -= con(a) + con(b);
if (word[i] == '0') {
uni(a, b + k);
uni(b, a + k);
} else {
uni(a, b);
uni(a + k, b + k);
}
res += con(a);
}
}
printf("%d\n", res);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 1000000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() == 2) {
long long a = la[i][0], b = la[i][1];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
int kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else {
kl = pr[a].l + pr[b].l;
}
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
} else if (la[i].size() == 1) {
bool b = 0;
int a = la[i][0];
while (a != -1) {
b ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if ((b and s[i] == '1') or (!b and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& x) {
x = 0;
int fl = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
x *= fl;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
read(t);
read(args...);
}
const int N = 300005;
const int INF = 1e9;
int n, k, c;
vector<int> vec[N];
char s[N];
int fa[N << 1], sz[N << 1];
inline int lowbit(int x) { return x & -x; }
inline int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
inline void unionn(int x, int y) {
x = findfa(x), y = findfa(y);
fa[y] = x;
sz[x] += sz[y];
}
int main() {
read(n, k);
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
read(c);
for (int j = 1; j <= c; j++) {
int x;
read(x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= k * 2; i++) fa[i] = i, sz[i] = (i <= k);
fa[k * 2 + 1] = k * 2 + 1, sz[k * 2 + 1] = INF;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
if (findfa(k * 2 + 1) != findfa(vec[i][0] + k * (s[i] == '0'))) {
ans -= min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
unionn(k * 2 + 1, vec[i][0] + k * (s[i] == '0'));
ans += min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
}
} else if (vec[i].size() == 2) {
if (s[i] == '0') {
if (findfa(vec[i][0]) != findfa(vec[i][1] + k)) {
ans -= min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
ans -= min(sz[findfa(vec[i][1])], sz[findfa(vec[i][1] + k)]);
unionn(vec[i][0], vec[i][1] + k);
unionn(vec[i][0] + k, vec[i][1]);
ans += min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
}
} else {
if (findfa(vec[i][0]) != findfa(vec[i][1])) {
ans -= min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
ans -= min(sz[findfa(vec[i][1])], sz[findfa(vec[i][1] + k)]);
unionn(vec[i][0], vec[i][1]);
unionn(vec[i][0] + k, vec[i][1] + k);
ans += min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 10, inf = 0x3f3f3f3f;
vector<int> v[maxn];
int fa[maxn], cnt[maxn];
int n, k;
string s;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
cnt[u] += cnt[v];
fa[v] = u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> s;
for (int i = 1, c, t; i <= k; i++) {
cin >> c;
while (c--) {
cin >> t;
v[t].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
fa[i] = i, fa[i + k] = i + k;
cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (v[i].size() == 0) {
printf("%d\n", ans);
continue;
} else if (v[i].size() == 1) {
int x = v[i][0];
ans -= min(cnt[find(x)], cnt[find(x + k)]);
if (s[i - 1] == '0')
cnt[find(x)] = inf;
else
cnt[find(x + k)] = inf;
ans += min(cnt[find(x)], cnt[find(x + k)]);
printf("%d\n", ans);
} else {
int x1 = v[i][0], x2 = v[i][1];
if (find(x1) == find(x2) || find(x1) == find(x2 + k)) {
printf("%d\n", ans);
continue;
}
ans -= min(cnt[find(x1)], cnt[find(x1 + k)]);
ans -= min(cnt[find(x2)], cnt[find(x2 + k)]);
if (s[i - 1] == '0') {
merge(x1, x2 + k);
merge(x1 + k, x2);
} else {
merge(x1, x2);
merge(x1 + k, x2 + k);
}
ans += min(cnt[find(x1)], cnt[find(x1 + k)]);
printf("%d\n", ans);
}
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 7;
int n, k, fa[maxn], val[maxn], big;
char a[maxn];
vector<int> E[maxn];
int fid(int x) { return fa[x] == x ? x : fa[x] = fid(fa[x]); }
int uni(int x, int y) {
int fx = fid(x), fy = fid(y);
if (fx != fy) {
fa[fx] = fy;
val[fy] += val[fx];
}
}
int gmin(int x) { return min(val[fid(x)], val[fid(x + k)]); }
int main() {
ios::sync_with_stdio(0);
cin >> n >> k;
cin >> (a + 1);
for (int i = 1; i <= k; ++i) {
int x, y;
cin >> x;
while (x--) {
cin >> y;
E[y].push_back(i);
}
}
for (int i = 1; i <= 2 * k + 1; ++i) {
fa[i] = i;
val[i] = (i <= k);
}
big = 2 * k + 1;
val[big] = 1e9;
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (E[i].size() == 1) {
int tmp = E[i][0] + k * (a[i] == '0');
ans -= gmin(E[i][0]);
uni(tmp, big);
ans += gmin(E[i][0]);
} else if (E[i].size() == 2) {
int x = E[i][0], y = E[i][1];
if (a[i] == '0' && fid(x) != fid(x + k)) {
ans = ans - gmin(x) - gmin(y);
uni(x, y + k);
uni(y, x + k);
ans += gmin(x);
}
if (a[i] == '1' && fid(x) != fid(y)) {
ans = ans - gmin(x) - gmin(y);
uni(x, y);
uni(x + k, y + k);
ans += gmin(x);
}
}
cout << ans << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
python3
|
from sys import stdin
input = stdin.readline
n , k = [int(i) for i in input().split()]
pairs = [i + n for i in range(n)] + [i for i in range(n)]
initial_condition = list(map(lambda x: x == '1',input().strip()))
data = [i for i in range(2*n)]
constrain = [-1] * (2*n)
h = [0] * (2*n)
L = [1] * n + [0] * n
dp1 = [-1 for i in range(n)]
dp2 = [-1 for i in range(n)]
for i in range(k):
input()
inp = [int(j) for j in input().split()]
for s in inp:
if dp1[s-1] == -1:dp1[s-1] = i
else:dp2[s-1] = i
pfsums = 0
ans = []
def remove_pfsum(s1):
global pfsums
if constrain[s1] == 1:
pfsums -= L[s1]
elif constrain[pairs[s1]] == 1:
pfsums -= L[pairs[s1]]
else:
pfsums -= min(L[s1],L[pairs[s1]])
def sh(i):
while i != data[i]:
i = data[i]
return i
def upd_pfsum(s1):
global pfsums
if constrain[s1] == 1:
pfsums += L[s1]
elif constrain[pairs[s1]] == 1:
pfsums += L[pairs[s1]]
else:
pfsums += min(L[s1],L[pairs[s1]])
def ms(i,j):
i = sh(i) ; j = sh(j)
cons = max(constrain[i],constrain[j])
if h[i] < h[j]:
data[i] = j
h[i] += 1
L[j] += L[i]
constrain[j] = cons
return j
else:
data[j] = i
h[j] += 1
L[i] += L[j]
constrain[i] = cons
return i
for i in range(n):
if dp1[i] == -1 and dp2[i] == -1:
pass
elif dp2[i] == -1:
s1 = sh(dp1[i])
remove_pfsum(s1)
constrain[s1] = 0 if initial_condition[i] else 1
constrain[pairs[s1]] = 1 if initial_condition[i] else 0
upd_pfsum(s1)
else:
s1 = sh(dp1[i]) ; s2 = sh(dp2[i])
if s1 == s2 or pairs[s1] == s2:
pass
else:
remove_pfsum(s1)
remove_pfsum(s2)
if initial_condition[i]:
new_s1 = ms(s1,s2)
new_s2 = ms(pairs[s1],pairs[s2])
else:
new_s1 = ms(s1,pairs[s2])
new_s2 = ms(pairs[s1],s2)
pairs[new_s1] = new_s2
pairs[new_s2] = new_s1
upd_pfsum(new_s1)
ans.append(pfsums)
for i in ans:
print(i)
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x;
vector<int> vec[300010];
char c[300010];
int fa[300010 << 1], sz[300010 << 1];
int lim[300010];
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d%s", &n, &k, c + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &m);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
fa[i * 2 - 1] = i * 2 - 1;
sz[i * 2 - 1] = 1;
fa[i * 2] = i * 2;
sz[i * 2] = 0;
lim[i * 2 - 1] = lim[i * 2] = -1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
int x = vec[i][0];
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f1] == -1)
ans -= min(s0, s1);
else
ans -= lim[f1] ? s1 : s0;
lim[f1] = (c[i] == '0');
lim[f0] = lim[f1] ^ 1;
ans += lim[f1] ? s1 : s0;
} else if (vec[i].size() == 2) {
int x = vec[i][0], y = vec[i][1];
int fx1 = find(x * 2 - 1), fx0 = find(x * 2);
int sx1 = sz[fx1], sx0 = sz[fx0];
int fy1 = find(y * 2 - 1), fy0 = find(y * 2);
int sy1 = sz[fy1], sy0 = sz[fy0];
if (fx0 != fy0 && fx0 != fy1) {
if (lim[fx1] == -1)
ans -= min(sx0, sx1);
else
ans -= lim[fx1] ? sx1 : sx0;
if (lim[fy1] == -1)
ans -= min(sy0, sy1);
else
ans -= lim[fy1] ? sy1 : sy0;
if (c[i] == '1') {
fa[fx1] = fy1;
fa[fx0] = fy0;
sz[fy1] += sz[fx1];
sz[fy0] += sz[fx0];
if (lim[fx1] != -1) {
lim[fy1] = lim[fx1];
lim[fy0] = lim[fy1] ^ 1;
} else if (lim[fx0] != -1) {
lim[fy0] = lim[fx0];
lim[fy1] = lim[fy0] ^ 1;
}
} else {
fa[fx1] = fy0;
fa[fx0] = fy1;
sz[fy0] += sz[fx1];
sz[fy1] += sz[fx0];
if (lim[fx1] != -1) {
lim[fy0] = lim[fx1];
lim[fy1] = lim[fy0] ^ 1;
} else if (lim[fx0] != -1) {
lim[fy1] = lim[fx0];
lim[fy0] = lim[fy1] ^ 1;
}
}
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f1] == -1)
ans += min(s0, s1);
else
ans += lim[f1] ? s1 : s0;
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 300005;
const long long inf = 1ll << 50;
int ans = 0;
bool init[maxn];
int par[maxn], sz[maxn], cnt[maxn][2], col[maxn];
vector<int> vec[maxn];
int find(int x) {
if (par[x] == x) {
return x;
}
par[x] = find(par[x]);
col[x] = col[x] ^ col[par[x]];
return par[x];
}
int get_val(int x) { return min(cnt[x][0], cnt[x][1]); }
void merge(int X, int Y, bool same) {
int x = find(X);
int y = find(Y);
if (x == y) {
return;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
sz[y] += sz[x];
par[x] = y;
same = (same ^ col[X] ^ col[Y]);
ans -= get_val(x);
ans -= get_val(y);
if (same) {
col[x] = 0;
cnt[y][0] += cnt[x][0];
cnt[y][1] += cnt[x][1];
} else {
col[x] = 1;
cnt[y][0] += cnt[x][1];
cnt[y][1] += cnt[x][0];
}
ans += get_val(y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
sz[0] = (1 << 28);
cnt[0][0] = (1 << 28);
for (int i = 1; i <= k; i++) {
par[i] = i;
sz[i] += 1;
cnt[i][0] = 1;
cnt[i][1] = 0;
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
init[i] = s[i - 1] - '0';
}
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
int a;
for (int j = 0; j < c; j++) {
cin >> a;
vec[a].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
assert(vec[i].size() <= 2);
if (vec[i].size() == 0) {
cout << ans << "\n";
assert(init[i] == 1);
continue;
}
if (vec[i].size() == 1) {
vec[i].push_back(0);
}
merge(vec[i][0], vec[i][1], init[i] == 1);
cout << ans << "\n";
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct DS {
int rank, cost, parent;
bool forced;
} ds[4 * 300100];
int ans;
char status[300100];
vector<int> subs[300100];
int neg(int n) { return n + 300100; }
int find(int n) {
if (n == ds[n].parent) return n;
return ds[n].parent = find(ds[n].parent);
}
void join(int n1, int n2) {
int s1 = find(n1), s2 = find(n2);
if (s1 != s2) {
if (ds[s1].rank < ds[s2].rank) swap(s1, s2);
ds[s2].parent = s1;
ds[s1].cost += ds[s2].cost;
ds[s1].rank += ds[s2].rank;
ds[s1].forced |= ds[s2].forced;
}
}
int main() {
int n, k, qtd, v, s1, s2, ns1, ns2, mini;
scanf("%d %d", &n, &k);
getchar();
scanf("%s", status);
for (int i = 0; i < k; i++) {
scanf("%d", &qtd);
for (int j = 0; j < qtd; j++) {
scanf("%d", &v);
subs[v].emplace_back(i);
}
ds[i].rank = 1;
ds[i].cost = 1;
ds[i].parent = i;
ds[i].forced = false;
ds[neg(i)].rank = 1;
ds[neg(i)].cost = 0;
ds[neg(i)].parent = neg(i);
ds[neg(i)].forced = false;
}
for (int i = 1; i <= n; i++) {
if (status[i - 1] == '1') {
if (subs[i].size() == 2) {
s1 = find(subs[i][0]);
s2 = find(subs[i][1]);
ns1 = find(neg(subs[i][0]));
ns2 = find(neg(subs[i][1]));
mini = -1;
if (ds[s1].forced) ans -= ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans -= ds[ns1].cost, mini = 0;
if (mini == -1) {
mini = min(ds[s1].cost, ds[ns1].cost);
ans -= mini;
}
mini = -1;
if (ds[s2].forced and s1 != s2) ans -= ds[s2].cost, mini = 0;
if (ds[ns2].forced and s1 != s2) ans -= ds[ns2].cost, mini = 0;
if (mini == -1 and s1 != s2 and s1 != ns2 and ns1 != s2 and
ns1 != ns2) {
mini = min(ds[s2].cost, ds[ns2].cost);
ans -= mini;
}
join(s1, s2);
join(ns1, ns2);
s1 = find(subs[i][0]);
s2 = find(subs[i][1]);
ns1 = find(neg(subs[i][0]));
ns2 = find(neg(subs[i][1]));
mini = -1;
if (ds[s1].forced) ans += ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans += ds[ns1].cost, mini = 0;
if (mini == -1) mini = min(ds[s1].cost, ds[ns1].cost);
ans += mini;
} else {
if (subs[i].size() == 1) {
s1 = find(subs[i][0]);
ns1 = find(neg(subs[i][0]));
if (not ds[ns1].forced) {
ans -= min(ds[s1].cost, ds[ns1].cost);
ds[ns1].forced = true;
ans += ds[ns1].cost;
}
}
}
} else {
if (subs[i].size() == 2) {
s1 = find(subs[i][0]);
ns2 = find(neg(subs[i][1]));
ns1 = find(neg(subs[i][0]));
s2 = find(subs[i][1]);
mini = -1;
if (ds[s1].forced) ans -= ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans -= ds[ns1].cost, mini = 0;
if (mini == -1) {
mini = min(ds[s1].cost, ds[ns1].cost);
ans -= mini;
}
mini = -1;
if (ds[s2].forced and s1 != s2) ans -= ds[s2].cost, mini = 0;
if (ds[ns2].forced and s1 != s2) ans -= ds[ns2].cost, mini = 0;
if (mini == -1 and s1 != s2 and s1 != ns2 and ns1 != s2 and
ns1 != ns2) {
mini = min(ds[s2].cost, ds[ns2].cost);
ans -= mini;
}
join(s1, ns2);
join(ns1, s2);
s1 = find(subs[i][0]);
ns2 = find(neg(subs[i][1]));
ns1 = find(neg(subs[i][0]));
s2 = find(subs[i][1]);
mini = -1;
if (ds[s1].forced) ans += ds[s1].cost, mini = 0;
if (ds[ns1].forced) ans += ds[ns1].cost, mini = 0;
if (mini == -1) mini = min(ds[s1].cost, ds[ns1].cost);
ans += mini;
} else {
if (subs[i].size() == 1) {
s1 = find(subs[i][0]);
ns1 = find(neg(subs[i][0]));
if (not ds[s1].forced) {
ans -= min(ds[s1].cost, ds[ns1].cost);
ds[s1].forced = true;
ans += ds[s1].cost;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
string s;
vector<int> a[300001];
int p[1000001];
int f[1000001];
const int Max = 6e5;
const int T = 3e5;
int root(int u) {
if (u == p[u]) return u;
return p[u] = root(p[u]);
}
void Merge(int u, int v) {
u = root(u);
v = root(v);
int ru = root(u);
int rv = root(v);
p[rv] = ru;
f[ru] += f[rv];
}
int main() {
cin >> n >> k >> s;
s = '.' + s;
for (int i = 1; i <= Max; i++) {
p[i] = i;
f[i] = -1;
}
for (int i = 1; i <= k; i++) {
int len;
cin >> len;
while (len--) {
int x;
cin >> x;
a[x].push_back(i);
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
if (a[i].size() == 1) {
int u = a[i][0];
if (f[root(u + T)] == -1) {
f[root(u + T)] = 1;
res++;
}
if (f[root(u)] != -1) {
res -= min(f[root(u)], f[root(u + T)]);
f[root(u)] = 1e9;
res += min(f[root(u)], f[root(u + T)]);
}
f[root(u)] = 1e9;
} else if (a[i].size() == 2) {
int u = a[i][0];
int v = a[i][1];
if (f[root(u)] == -1) f[root(u)] = 0;
if (f[root(u + T)] == -1) f[root(u + T)] = 1;
if (f[root(v)] == -1) f[root(v)] = 0;
if (f[root(v + T)] == -1) f[root(v + T)] = 1;
if (root(u) != root(v + T)) {
res -= min(f[root(u)], f[root(u + T)]);
res -= min(f[root(v)], f[root(v + T)]);
Merge(u, v + T);
Merge(u + T, v);
res += min(f[root(u)], f[root(u + T)]);
}
}
} else {
if (a[i].size() == 1) {
int u = a[i][0];
if (f[root(u + T)] != -1) {
res -= min(f[root(u)], f[root(u + T)]);
f[root(u + T)] = 1e9;
res += min(f[root(u)], f[root(u + T)]);
}
if (f[root(u)] == -1) f[root(u)] = 0;
f[root(u + T)] = 1e9;
} else if (a[i].size() == 2) {
int u = a[i][0];
int v = a[i][1];
if (f[root(u)] == -1) f[root(u)] = 0;
if (f[root(u + T)] == -1) f[root(u + T)] = 1;
if (f[root(v)] == -1) f[root(v)] = 0;
if (f[root(v + T)] == -1) f[root(v + T)] = 1;
if (root(u) != root(v)) {
res -= min(f[root(u)], f[root(u + T)]);
res -= min(f[root(v)], f[root(v + T)]);
Merge(u, v);
Merge(u + T, v + T);
res += min(f[root(u)], f[root(u + T)]);
}
}
}
cout << res << "\n";
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
vector<char> active;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
if (active[p_v]) {
cur_ans -= get_vrt_ans(p_v);
}
if (active[p_u]) {
cur_ans -= get_vrt_ans(p_u);
}
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[u] != 2) {
if (r_st[v] != 2) {
} else {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
active[p_v] = true;
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()),
p(n),
sz(n, 1),
p_xor(n),
r_st(r_st),
ans(n),
active(n, false) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void activate(int v) {
v = dsu_get(v);
if (!active[v]) {
cur_ans += get_vrt_ans(v);
active[v] = true;
}
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(mask_st);
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.activate(v_mask[i][0]);
}
cout << dsu.get_ans() << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
template <class O, class Z, class A, class B, class C, class D, class E>
void debug(O& out, const Z& p, const A& J, const B& a, const C& coa, const D& S,
const E& coS) {
out << "p:";
for (auto x : p) out << " " << x;
out << std::endl;
out << "J:";
for (auto x : J) {
out << " {";
for (auto y : x) out << " " << y;
out << " }";
}
out << std::endl;
out << " a:";
for (auto x : a) out << " " << x;
out << std::endl;
out << "coa:";
for (auto x : coa) out << " " << x;
out << std::endl;
out << " S:";
for (auto x : S) out << " " << x;
out << std::endl;
out << "coS:";
for (auto x : coS) out << " " << x;
out << std::endl;
}
template <class O, class Z, class X, class A, class B, class C, class D,
class E>
void debug(O& out, int i, int R, Z& p, const X& x, const A& J, const B& a,
const C& coa, const D& S, const E& coS) {
for (auto y : J[i])
out << x[i] << ' ' << R << ' ' << y << ' ' << p[y] << ' '
<< p[abs(get_p(p, y))] << ' ' << a[abs(get_p(p, y))] << ' '
<< coa[abs(get_p(p, y))] << ' ' << S[abs(get_p(p, y))] << ' '
<< coS[abs(get_p(p, y))] << ' ';
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (i == 10781) debug(std::cout, i, R, p, x, J, a, coa, S, coS);
if (J[i].size() == 0) {
if (n < 1000) std::cout << R << std::endl;
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j && !x[i];
coa[abs_p_j] = coa_j && x[i];
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 250000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() == 2) {
long long a = la[i][0], b = la[i][1];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
int kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else {
kl = pr[a].l + pr[b].l;
}
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
} else if (la[i].size() == 1) {
bool b = 0;
int a = la[i][0];
while (a != -1) {
b ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if ((b and s[i] == '1') or (!b and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
int n, k, m, c, vas, cc = 1000000000;
vector<int> vv;
string s;
vector<vector<int>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<int>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() == 2) {
int a = la[i][0], b = la[i][1];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
int kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else {
kl = pr[a].l + pr[b].l;
}
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
} else if (la[i].size() == 1) {
bool b = 0;
int a = la[i][0];
while (a != -1) {
b ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if ((b and s[i] == '1') or (!b and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (int i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
long long val[600010];
int have[600010][2], fa[600010];
char str[600010];
int n, k;
int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
int other(int u) { return u <= k ? u + k : u - k; }
void update(int u, long long x, long long y) {
ans -= min(val[u], val[other(u)]);
ans += min(x, y);
val[u] = x;
val[other(u)] = y;
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
while (c--) {
int a;
scanf("%d", &a);
if (have[a][0])
have[a][1] = i;
else
have[a][0] = i;
}
val[i] = 0, val[i + k] = 1;
fa[i] = i, fa[i + k] = i + k;
}
for (int i = 1; i <= n; i++) {
if (str[i] == '0') {
int x = find(have[i][0]), y = find(have[i][1]);
if (!y)
update(x, 1e7, val[other(x)]);
else {
int uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = other(x);
fa[other(y)] = x;
update(x, val[x] + vy, val[other(x)] + uy);
}
} else {
int x = find(have[i][0]), y = find(have[i][1]);
if (!x) {
printf("%lld\n", ans);
continue;
}
if (!y)
update(x, val[x], 1e7);
else {
int uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = x;
fa[other(y)] = other(x);
update(x, val[x] + uy, val[other(x)] + vy);
}
}
printf("%lld\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
template <class O, class Z, class A, class B, class C, class D, class E>
void debug(O& out, const Z& p, const A& J, const B& a, const C& coa, const D& S,
const E& coS) {
out << "p:";
for (auto x : p) out << " " << x;
out << std::endl;
out << "J:";
for (auto x : J) {
out << " {";
for (auto y : x) out << " " << y;
out << " }";
}
out << std::endl;
out << " a:";
for (auto x : a) out << " " << x;
out << std::endl;
out << "coa:";
for (auto x : coa) out << " " << x;
out << std::endl;
out << " S:";
for (auto x : S) out << " " << x;
out << std::endl;
out << "coS:";
for (auto x : coS) out << " " << x;
out << std::endl;
}
template <class O, class Z, class X, class A, class B, class C, class D,
class E>
void debug(O& out, int i, int R, Z& p, const X& x, const A& J, const B& a,
const C& coa, const D& S, const E& coS) {
for (auto y : J[i])
out << x[i] << ' ' << R << ' ' << y << ' ' << p[y] << ' '
<< p[abs(get_p(p, y))] << ' ' << a[abs(get_p(p, y))] << ' '
<< coa[abs(get_p(p, y))] << ' ' << S[abs(get_p(p, y))] << ' '
<< coS[abs(get_p(p, y))] << ' ';
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (i == 10780) debug(std::cout, i, R, p, x, J, a, coa, S, coS);
if (J[i].size() == 0) {
if (n < 1000 || i >= 10779) std::cout << R << std::endl;
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j && !x[i];
coa[abs_p_j] = coa_j && x[i];
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
if (i == 10780) debug(std::cout, i, R, p, x, J, a, coa, S, coS);
if (n < 1000 || i >= 10779) std::cout << R << std::endl;
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
if (i == 10780) debug(std::cout, i, R, p, x, J, a, coa, S, coS);
if (n < 1000 || i >= 10779) std::cout << R << std::endl;
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 3e5 + 5;
long long inf = (long long)30000;
long long mod = (long long)998244353;
long long ans = 0, vert;
long long n, k, par[MAX], sz[MAX], cnt[MAX][2], clr[MAX], pos[MAX];
vector<long long> sets[MAX];
vector<long long> b[MAX];
void x_or(long long dad) {
for (int i = 0; i < sets[dad].size(); i++) {
clr[sets[dad][i]] ^= 1;
}
cnt[dad][0] = 0;
cnt[dad][1] = 0;
for (int i = 0; i < sets[dad].size(); i++) {
long long v = sets[dad][i];
if (clr[v] == 0)
cnt[dad][0]++;
else
cnt[dad][1]++;
}
}
long long get(long long u) {
if (par[u] == u)
return u;
else {
long long v = get(par[u]);
par[u] = v;
return v;
}
}
void unite(long long a, long long b) {
if (get(a) == get(b)) return;
if (sz[get(a)] < sz[get(b)]) swap(a, b);
if (vert == 1 && clr[a] != clr[b]) x_or(get(b));
if (vert == 0 && clr[a] == clr[b]) x_or(get(b));
a = get(a);
b = get(b);
for (int i = 0; i < sets[b].size(); i++) sets[a].push_back(sets[b][i]);
sets[b].clear();
par[b] = a;
sz[a] += sz[b];
ans -= min(cnt[b][0], cnt[b][1]) + min(cnt[a][0], cnt[a][1]);
cnt[a][0] += cnt[b][0];
cnt[a][1] += cnt[b][1];
ans += min(cnt[a][0], cnt[a][1]);
}
void unite_1(long long v, long long u) {
if (vert == 1 && clr[v] != clr[u]) x_or(get(u));
if (vert == 0 && clr[v] == clr[u]) x_or(get(v));
v = get(v);
u = get(u);
par[u] = v;
for (int i = 0; i < sets[u].size(); i++) {
sets[v].push_back(sets[u][i]);
}
sets[u].clear();
ans -= min(cnt[u][1], cnt[u][0]);
ans += cnt[u][1];
cnt[v][1] += cnt[u][1];
cnt[v][0] += cnt[u][0];
}
void solve() {
cin >> n >> k;
string s;
cin >> s;
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
for (int j = 0; j < c; j++) {
long long a;
cin >> a;
b[a].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
sz[i] = 1;
clr[i] = 0;
cnt[i][0] = 1;
par[i] = i;
sets[i].push_back(i);
}
for (int i = 1; i <= n; i++) {
vert = s[i - 1] - 48;
if (b[i].size() == 1 && !pos[get(b[i][0])]) {
long long v = b[i][0];
if (clr[v] == 0 && vert == 0) x_or(get(v));
if (clr[v] == 1 && vert == 1) x_or(get(v));
ans -= min(cnt[get(v)][0], cnt[get(v)][1]);
ans += cnt[get(v)][1];
pos[get(v)] = 1;
} else if (b[i].size() == 2) {
if (pos[get(b[i][0])] || pos[get(b[i][1])]) {
if (pos[get(b[i][0])] && pos[get(b[i][1])])
;
else {
long long u, v;
v = b[i][0];
u = b[i][1];
if (pos[get(b[i][1])]) swap(u, v);
unite_1(v, u);
}
} else {
unite(b[i][0], b[i][1]);
}
}
cout << ans << "\n";
}
}
int main() {
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ios::sync_with_stdio(0);
long long q;
q = 1;
while (q--) solve();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, β¦, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 β€ i_1 < i_2 < i_3 β€ k, A_{i_1} β© A_{i_2} β© A_{i_3} = β
.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 β€ i β€ n.
Input
The first line contains two integers n and k (1 β€ n, k β€ 3 β
10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 β€ c β€ n) β the number of elements in the subset.
The second line of the description contains c distinct integers x_1, β¦, x_c (1 β€ x_i β€ n) β the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i β the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i β₯ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i β€ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i β₯ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int N, K, a[MAXN];
vector<int> V[MAXN];
char S[MAXN];
int f1[MAXN], v1[MAXN], f2[MAXN], chs[MAXN], ans;
int get1(int x) {
if (f1[x] != x) f1[x] = get1(f1[x]);
return f1[x];
}
void merge1(int x, int y) {
x = get1(x), y = get1(y);
if (x != y) f1[x] = y, v1[y] += v1[x];
}
int get2(int x) {
if (f2[x] != x) f2[x] = get2(f2[x]);
return f2[x];
}
inline int val(int x) {
x = get2(x);
if (chs[x] == 1)
return v1[x];
else if (chs[x] == 2)
return v1[x + K];
else
return min(v1[x], v1[x + K]);
}
void merge2(int x, int y, int k) {
if (get2(x) != get2(y)) {
ans -= val(x) + val(y);
int qwq = 0, c = chs[get2(x)];
if (chs[get2(x)]) {
if (chs[get2(x)] == 1)
qwq = get2(x);
else
qwq = get2(x) + K;
}
f2[get2(x)] = get2(y);
if (k == 1)
merge1(x, y), merge1(x + K, y + K);
else
merge1(x, y + K), merge1(x + K, y);
if (!chs[get2(y)] && qwq) {
if (get1(qwq) == get2(y))
chs[get2(y)] = c;
else
chs[get2(y)] = 3 - c;
}
ans += val(y);
}
}
int main() {
scanf("%d%d%s", &N, &K, S + 1);
int i, j;
for (i = 1; i <= K; ++i) {
int c, x;
scanf("%d", &c);
while (c--) scanf("%d", &x), V[x].push_back(i);
}
for (i = 1; i <= K * 2; ++i) f1[i] = i, v1[i] = i > K;
for (i = 1; i <= K; ++i) f2[i] = i;
for (i = 1; i <= N; ++i) {
if (V[i].size() == 1) {
if (S[i] == '0')
ans -= val(V[i][0]), chs[get2(V[i][0])] = 2, ans += val(V[i][0]);
else
ans -= val(V[i][0]), chs[get2(V[i][0])] = 1, ans += val(V[i][0]);
} else if (V[i].size() == 2) {
if (S[i] == '0')
merge2(V[i][0], V[i][1], 2);
else
merge2(V[i][0], V[i][1], 1);
}
printf("%d\n", ans);
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.