[ARC105E] Keep Graph Disconnected题解

题目

考虑加任意一条边时都会输的图的状态:图被分成两个强联通分量,每一个强联通分量都是一个完全图。

示例
也就是说,假设一开始节点 1 1 1 和节点 n n n 不联通,那么还可以加 n ( n − 1 ) 2 − m − c n t 1 ( n − c n t 1 ) \frac{n(n-1)}2-m-cnt_1(n-cnt_1) 2n(n1)mcnt1(ncnt1) 次,其中 c n t x cnt_x cntx 代表 x x x 所在联通块大小。

  • n n n 为奇数
    • 此时 c n t 1 ( n − c n t 1 ) cnt_1(n-cnt_1) cnt1(ncnt1) 一定是偶数,只用对 n ( n − 1 ) 2 − m \frac{n(n-1)}2-m 2n(n1)m 进行讨论,你偏要加上 c n t 1 ( n − c n t 1 ) cnt_1(n-cnt_1) cnt1(ncnt1) 也不是不行
  • 否则:
    • c n t 1 = c n t n cnt_1=cnt_n cnt1=cntn
      • 此时先手一定可以选择 c n t 1 cnt_1 cnt1 c n t n cnt_n cntn 的奇偶性(通过增加联通块的点),所以先手必胜。
      • 否则,对 n ( n − 1 ) 2 − m − c n t 1 ( n − c n t 1 ) \frac{n(n-1)}2-m-cnt_1(n-cnt_1) 2n(n1)mcnt1(ncnt1) 进行判断。

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, n, m;
int f[200100], cnt[200100];
int find(int x) {return x == f[x] ? x : f[x] = find(f[x]);}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			f[i] = i;
			cnt[i] = 0;
		}
		for (int i = 1; i <= m; i++) {
			int a, b;
			cin >> a >> b;
			f[find(b)] = find(a);
		}
		for (int i = 1; i <= n; i++) {
			cnt[find(i)]++;
		}
		if (find(1) == find(n)) {
			cout << "Second\n";
		}
		else if (n % 2) {
			if (((n * (n - 1) / 2) - m) % 2) cout << "First\n";
			else cout << "Second\n";
		}
		else {
			if (cnt[find(1)] % 2 != cnt[find(n)] % 2) {
				cout << "First\n";
			}
			else {
				if (((n * (n - 1) / 2) - m - cnt[find(1)] * (n - cnt[find(1)])) % 2) cout << "First\n";
				else cout << "Second\n";
			}
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值