n个结点围成一个圆(编号为顺时针方向从1到n),给定01串s,s[i] = ‘0‘表示结点i的度为偶数,否则为奇数,若u,v之间有边,就在圆中对于结点间连一条直线,任意两条直线不能在圆内相交,构造该树

文章描述了一个C++程序,通过输入的字符串和位操作,判断是否能将0变为1,并输出转换路径。涉及循环、条件判断和位运算等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

思路:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
const int maxn = 1e6 + 5, inf = 1e9 + 5, maxm = 4e4 + 5, mod = 1e9 + 7, N = 1e6;
int a[maxn], p[maxn];
int n, m;
string s;

int qpow(int a, int b){
    int res = 1;
    while(b){
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

void solve(){
    int res = 0;
    cin >> n;
    // for(int i = 1; i <= n; i++){
    //     cin >> a[i];
    // }
    // cout << res << '\n';
    cin >> s;
    int t = 0;
    int pos = -1;
    for(int i = 0; i < n; i++){
        if(s[i] == '1'){
            t ^= 1;
            pos = i;
        }
    }
    if(t || pos == -1){
        cout << "No\n";
        return;
    }
    pos = (pos + 1) % n;
    int j = (pos + 1) % n;
    cout << "Yes\n";
    while(j != pos){
        cout << pos + 1 << ' ' << j + 1 << '\n';
        while(s[j] != '1'){
            cout << j + 1 << ' ' << (j + 1) % n + 1 << '\n';
            j = (j + 1) % n;
        }
        j = (j + 1) % n;
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    // fac[0] = 1;
    // for(int i = 1; i <= N; i++){
    //     fac[i] = fac[i - 1] * i % mod;
    // }
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__night_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值