CF #747 div2 D. The Number of Imposters

题意

n个人 有m个查询
每组查询格式如下
x(int) y(int) s(string)
如果s是imposter 则说明x指责y是会说谎者
否则 x指责y是诚实人
诚实人只说真话
说谎者只说假话
问有最多有多少说谎者

思路

可以很明显发现
如果x指责y是imposter 他们两个人身份相反
反之则身份相同
然后可以用扩展域并查集
具体见代码

AC代码

#include <bits/stdc++.h>
#define endl "\n" 
#define INF 0x3f3f3f3f3f3f3f3f
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
using namespace std;
typedef long long ll;
const  ll mod = 1000000007;
const   double  PI = acos(-1.0);
const double EI = exp(1.0);
const int  N = 2e6 + 10;
const double  eps = 1e-8;
string a[2] = { "imposter","crewmate" };
int F[N];
int son[N];
int find(int x)
{
	if (F[x] == x) return x;
	else return F[x] = find(F[x]);
}
void inserter(int x, int y)
{
	x = find(x), y = find(y);
	if (x == y) return;
	else
	{
		F[x] = y;
		son[y] += son[x];
		son[x] = 0;
	}
}
void solve()
{
	string s;
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= 2 * n; i++)
	{
		F[i] = i;
		if (i <= n)son[i] = 0;
		else son[i] = 1;
	}
	while (m--)
	{
		int x, y;
		cin >> x >> y>>s;
		if (s == a[0])
		{
			inserter(x, y + n);inserter(y, x + n);
		}
		else
		{
			inserter(x, y); inserter(x + n, y + n);
		}
	}
	int ans = 0;
	for (int i = 1; i <= 2 * n; i++)
	{
		int fu = find(i), fv;
		if (i <= n)fv = find(i + n);
		else fv = find(i - n);
		if (fv ==fu)
		{
			cout << -1 << endl;
			return;
		}
		if (i == fu)ans += max(son[fu], son[fv]);(这里其实会+两遍,所以后面要除二)
	}
	cout << ans / 2 << endl;

}
int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t; while (t--)
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值