CF1685D Circular Spanning Tree题解

考虑无解情况,一定是可以保证偶数个1

考虑一种构造 对于环上连续的一段我们连续构造边

可以形成从00000000->10000001的变换

但是光是这样是不够的,因为要形成树,这个上边至少还要连一个点

注意偶数个1,我们可以将某些关于剩下的1搞下来,做一个构造

接下来的思路是将所有的连续边上的1000这种构造的最后一个0连到之上

比较巧妙,马上看Faze打Navi辣!

随便炒的代码:

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
#define lowbit(s) (s&-s)
using namespace std;
const int N=5e5+11;
const ll mod=998244353;
int n;
string a;
int nxt(int w){
	if(w<n)return w+1;
	else return 1;
}
int las(int w){
	if(w>1)return w-1;
	else return n;
}
void solve(){
	cin>>n>>a;a=' '+a;
	int cnt=0;
	for(int i=1;i<=n;i++)
		if(a[i]=='1') ++cnt;
	if(cnt&1){
		cout<<"NO"<<endl;
		return;
	}
	int rt=0;
	for(int i=1;i<=n;i++)
		if(a[i]=='1') rt=i;
	if(!rt){
		cout<<"NO"<<endl;
		return;
	}
	cout<<"YES"<<endl;
	bool t=1;
	for(int i=1;i<=n;i++){
		if(i==rt)continue;
		if(a[i]=='1'){
			int w=i,nx=nxt(i);
			while(nx!=rt&&a[nx]=='0'){
				cout<<w<<" "<<nx<<endl;
				w=nx;nx=nxt(w);
			}
			if(!t)cout<<w<<" "<<rt<<endl;
			else{
				nx=las(i);
				while(nx!=rt) {
					cout<<w<<" "<<nx<<endl;
					w=nx;nx=las(w);
				}
				cout<<w<<" "<<rt<<endl;
				t=0;
			}
		}
	}
} 
int main(){
	ios::sync_with_stdio(false);
	int Testcase; cin>>Testcase;
	while(Testcase--) solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值