Topic description
has a length of 01 string, given information. information Express . find the smallest , so that the first Words must be lies.
answer
This topic is related to
resemblance.
we know
.
This puts each prefix and
Split into two nodes: the same
, heterogeneous
.
For the next steps, see
Algorithm problem solving.
This topic is also related to
resemblance.
The blogger did not write in the late stage of lazy cancer.
Summarize
There are two methods for this question:
"Sideband right" and check the set:
"Extended Domain" and lookup:
Such questions can be used.
Standard range
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5050;
int fa[N << 1];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
fa[find(x)] = find(y);
}
int main()
{
int n, m = 0; scanf("%*d%d", &n);
static int a[N], b[N], d[N << 1];
static bool c[N];
for (int i = 1; i <= n; i++)
{
char opt[10];
scanf("%d%d%s", a + i, b + i, opt);
a[i]--; c[i] = opt[0] == 'o';
d[++m] = a[i]; d[++m] = b[i];
}
sort(d + 1, d + m + 1);
m = unique(d + 1, d + m + 1) - d - 1;
for (int i = 1; i <= n; i++)
{
a[i] = lower_bound(d + 1, d + m + 1, a[i]) - d;
b[i] = lower_bound(d + 1, d + m + 1, b[i]) - d;
}
for (int i = 1; i <= m * 2; i++)
fa[i] = i;
int ans = n + 1;
for (int i = 1; i <= n; i++)
{
if (c[i])
{
if (find(a[i]) == find(b[i]))
{
ans = i;
break;
}
merge(a[i], b[i] + m);
merge(a[i] + m, b[i]);
}
else
{
if (find(a[i]) == find(b[i] + m))
{
ans = i;
break;
}
merge(a[i], b[i]);
merge(a[i] + m, b[i] + m);
}
}
printf("%d\n", ans - 1);
return 0;
}