CF 1682 D. Circular Spanning Tree 思维 2000

12 篇文章 0 订阅

题意:n 个点按瞬时针从 1 到 n 排成一个圆,有一个二进制字符串 s,连接 n - 1 条边使第 i 个点的度数的奇偶性和 si 的奇偶性一样。圆内的边不能有交点。

思路:一共 n - 1 条边,所以总度数和为 2n - 2,小于 2n 并且为偶数,所以当 s 中 1 的个数为奇数或 0 时无解。找到一个点作为树的顶点,顺时针连接,当连接到第一个 1 时,断开。将顶点和下一个点连接,重复刚才的过程,直到连接到顶点的前一个点。注意顶点的前一个点必须是 1。

代码:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10, P = 1e9 + 7, mod = 998244353;
int n;
int re(int x){
	if(x == n + 1) x = 1;
	if(!x) x = n;
	return x;
}
void solve(){
	string s;
	cin >> n >> s;
	s = " " + s;
	int cnt = 0;
	for(int i = 1; i <= n; i++)
		cnt += s[i] == '1';
	if(cnt & 1 || !cnt){
		cout << "NO" << endl;
		return;
	}
	cout << "YES" << endl;
	int rt = 1;
	while(s[re(rt - 1)] != '1') rt++;
	for(int i = 1; i <= n; i++) 
		if(i != rt)
			cout << i << " " << (s[re(i - 1)] == '1' ? rt : re(i - 1)) << endl;	
}                 
int main(){
	ios::sync_with_stdio(0), 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、付费专栏及课程。

余额充值