//https://codeforces.com/contest/1594/problem/D
#include<bits/stdc++.h>
#include<unordered_map>
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
int n, m, fa[N * 2], sz[N * 2];
int find(int x)
{
return x == fa[x] ? fa[x] : fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
x = find(x);
y = find(y);
if (sz[x] > sz[y])
swap(x, y);
if(x != y)
fa[x] = y, sz[y] += sz[x];
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= 2 * n; i++)
fa[i] = i, sz[i] = i > n;//[1,n]为真话, [n + 1, 2 * n]为假话
for (int i = 1; i <= m; i++)
{
int u, v; string s;
cin >> u >> v >> s;
if (s == "crewmate")
merge(u, v), merge(u + n, v + n);
else
merge(u + n, v), merge(u, v + n);
}
for(int i = 1; i <= n; i++)
if (find(i) == find(n + i))
{
cout << -1 << '\n';
return;
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
int fx = find(i);
int fy = find(i + n);
ans += max(sz[fx], sz[fy]);//i作为好人坏人哪个多,加上i这个块中多的那一个, 若真多,反转就为假
sz[fx] = sz[fy] = 0;
}
cout << ans << '\n';
}
signed main()
{
IOS;
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
扩展域并查集
最新推荐文章于 2024-08-31 21:59:22 发布