题意

1-n排列,构成一个圆;1-n每个点有个值0或者1,0代表点的度为偶数,1代表点的度为计数;询问能否构成一棵树,树的连边在圆内不会相交,在圆边上可以相交,可以则输出方案。

提示

1. 首先考虑什么时候无解,显然,奇数点个数是偶数,并且>=2

2. 由奇数点个数为偶数可以发现,它们可以连到同一个偶数点上(并非直接连)

3. 剩下的偶数点可以直接顺时针串联,直到连到最近的一个奇数点上

4. 相当于每个奇数点后面有一条偶数链,或者没有偶数链只有一个奇点(这都是一样的,因为链最后一个点都只剩下一个需要连的点),直接把链的最后一个点连在一起就好了

代码

#include<bits/stdc++.h>

using namespace std;
char s[200005]; void run() {
int n;
cin >> n;
cin >> s;
int ans = 0;
for (int i = 0; s[i] != '\0'; i++) {
ans += s[i] - '0';
}
if (ans % 2 || ans == 0) {
puts("NO");
return;
} else {
puts("YES");
}
int cnt = n - ans;
if (cnt == 0) {
for (int i = 2; i <= n; i++) {
cout << 1 << ' ' << i << '\n';
}
return;
}
vector<vector<int>> vec;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == '1') {
vector<int> res;
res.emplace_back(i);
for (int j = i + 1; j <= n; j++) {
if (s[j - 1] == '0')res.emplace_back(j);
else {
i = j - 1;
break;
}
}
vec.emplace_back(res);
}
}
for (int i = 1; i <= n; i++) {
if (s[i - 1] == '0') {
vec.back().emplace_back(i);
} else
break;
}
int root = 1;
for (auto k: vec) { for (int i = 1; i < k.size(); i++) {
cout << k[i-1] << ' ' << k[i] << '\n';
}
}
for (int i = 0; i < vec.size(); i++) {
if (vec[i].size() > 1) {
root = i;
}
}
for (int i = 0; i < vec.size(); i++) {
if (i == root)continue;
cout << vec[root].back() << ' ' << vec[i].back() << '\n';
} } int main() {
int t;
cin >> t;
while (t--)
run(); return 0;
}

Codeforces 1682 D Circular Spanning Tree的更多相关文章

  1. Codeforces Edu3 E. Minimum spanning tree for each edge

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. CF1682D Circular Spanning Tree

    题意: 构造题,节点1~n顺时针排列成圆形,告诉你每个点度数奇偶性,让你构造一棵树,树边不相交. 思路: 因为每条边给总度数贡献2,因此如果度数为1的点有奇数个,直接输出no.显然0个度数为1的,也输 ...

  4. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  5. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  6. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  7. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  8. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  9. Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

随机推荐

  1. 编码GBK的不可映射字符,最新版sublime

    最近开始学java了,跟着老师写一个hello world,刚执行javac就报错:编码GBK的不可映射字符 然后去网上找了一堆,总结来说的就是编码不对,最新版的sublime只要设置utf-8保存即 ...

  2. SpringCloud之Sentinel

    一. sentinel是什么? 1.概念: 分布式服务架构的流量治理组件. 2.sentinel有什么作用? 2.1 流控:QPS.线程数 2.2 熔断降级:降级-->熔断策略.时长.请求数等 ...

  3. pytest-fixture执行顺序

    作用域-scope 作用域越大,越先执行,session>package>module>class>function. 是否自动调用fixture 自动调用(autouse=T ...

  4. Spring源码 03 IOC原理

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  5. java-分支重载以及构造方法

    1:方法的签名: 方法名+参数列表就是一个方法的签名 2.方法的重载(Overload): 1)发生在同一个类中,方法名称相同,参数列表不同 2)编译器在编译时会根据方法的签名自动绑定方法 3.构造方 ...

  6. 利用 Word 表格对文字、图文进行排版

    在以前,Web 前端工程师利用 <table /> 元素对网页布局进行排版,但是如今却不推荐此元素排版了,而是改用 <div /> 元素和 CSS 弹性布局(或网格布局)对网页 ...

  7. 牛客IOI周赛26-提高组 A. 逆序对

    题面 逆序对 有一个长度为 N \tt N N 的排列 a a a,进行 M \tt M M 次操作,操作有 4 \tt 4 4 种: 1 l r :交换 a l \tt a_l al​ 和 a r ...

  8. MyBatis快速上手与知识点总结

    目录 1.MyBatis概述 1.1 MyBatis概述 1.2 JDBC缺点 1.3 MyBatis优化 2.MyBatis快速入门 3.Mapper代理开发 3.1 Mapper代理开发概述 3. ...

  9. Sqoop 组件安装与配置

    下载和解压 Sqoop Sqoop相关发行版本可以通过官网 https://mirror-hk.koddos.net/apache/sqoop/ 来获取 安装 Sqoop组件需要与 Hadoop环境适 ...

  10. ubuntu 搭建网络文件系统

    一.安装 miniserver sudo curl -L https://github.com/svenstaro/miniserve/releases/download/v0.4.1/miniser ...