Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题
E. Interesting Graph and Apples
题目连接:
http://www.codeforces.com/contest/9/problem/E
Description
Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life «interesting graph and apples». An undirected graph is called interesting, if each of its vertices belongs to one cycle only — a funny ring — and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too.
She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (x1, y1), (x2, y2), ..., (xn, yn), where xi ≤ yi, is lexicographically smaller than the set (u1, v1), (u2, v2), ..., (un, vn), where ui ≤ vi, provided that the sequence of integers x1, y1, x2, y2, ..., xn, yn is lexicographically smaller than the sequence u1, v1, u2, v2, ..., un, vn. If you do not cope, Hexadecimal will eat you. ...eat you alive.
Input
The first line of the input data contains a pair of integers n and m (1 ≤ n ≤ 50, 0 ≤ m ≤ 2500) — the amount of vertices and edges respectively. The following lines contain pairs of numbers xi and yi (1 ≤ xi, yi ≤ n) — the vertices that are already connected by edges. The initial graph may contain multiple edges and loops.
Output
In the first line output «YES» or «NO»: if it is possible or not to construct an interesting graph. If the answer is «YES», in the second line output k — the amount of edges that should be added to the initial graph. Finally, output k lines: pairs of vertices xj and yj, between which edges should be drawn. The result may contain multiple edges and loops. k can be equal to zero.
Sample Input
3 2
1 2
2 3
Sample Output
YES
1
1 3
Hint
题意
告诉你这个图的所有点都在一个环内,这个图就是一个环
然后现在给你一些边,让你添加一些边使得成为一个环
且你构造的解的字典序应该是最小的。
题解:
首先所有点应该都是度数为2的
那么存在一个度数大于2的,显然就是NO
然后我们一开始暴力连接两个不是一个连通块,且度数为1的点
然后再暴力连接同一个连通块的两个端点
对了,还得判断只有一个点的那种情况
然后再判断一遍是否非法就好了……
这道E题好水……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 52;
int pa[maxn];
int e1[maxn*maxn],e2[maxn*maxn];
int d[maxn];
vector<pair<int,int> > ans;
int fi(int x)
{
if(pa[x]==x)return x;
return pa[x]=fi(pa[x]);
}
void uni(int x,int y)
{
int p1 = fi(x),p2 = fi(y);
if(p1==p2)return;
pa[p2]=p1;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)pa[i]=i;
for(int i=1;i<=m;i++)
scanf("%d%d",&e1[i],&e2[i]);
for(int i=1;i<=m;i++)
{
uni(e1[i],e2[i]);
d[e1[i]]++,d[e2[i]]++;
if(d[e1[i]]>2||d[e2[i]]>2)return puts("NO"),0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
if(d[j]<=1&&d[i]<=1&&fi(i)!=fi(j))
{
ans.push_back(make_pair(j,i));
uni(i,j);
d[i]++,d[j]++;
}
}
}
for(int i=1;i<=n;i++)
{
if(d[i]==2)continue;
for(int j=1;j<i;j++)
{
if(d[j]==1)
{
ans.push_back(make_pair(j,i));
uni(i,j);
d[i]++,d[j]++;
}
}
}
for(int i=1;i<=n;i++)
if(d[i]==0)ans.push_back(make_pair(i,i));
int p = fi(1);
for(int i=1;i<=n;i++)
if(fi(i)!=p)return puts("NO"),0;
puts("YES");
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题的更多相关文章
- Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题
C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- 按键精灵MySql数据库操作
查询 MySql服务器地址="192.168.1.166" 用户名 = "root" 密码 = " 数据库名="bookshop" ...
- Linux 下解决安装多个node冲突的问题(重新安装node)
一个系统中不经意安装了多个node版本,结果更新后还是原来的版本,下面思考一下解决办法: 敲黑板: 1. nodejs 用 包管理器安装一般在 /usr/local/bin 2. 查看当前目录下的no ...
- MYSQL的隐式类型转换
官方文档中是这么说的 当操作者使用不同类型的操作数,操作数类型兼容的出现使 转换.一些 发生隐式转换.例如,MySQL会自动 将数字转换为字符串的必要,反之亦然. 也可以将数字转换为字符串明确 使用( ...
- eclipse 常见问题之字体更改、添加注释模板
有些同学可能会和我有一样的困扰,每次想要更改字体大小.背景颜色等,都需要百度一下才知道怎么去做...不知道有没有这种情况的孩子,反正我经常遇到,老是记不住,今天写下来,顺带自己忘记的时候可以查看一下. ...
- python RSA加密解密及模拟登录cnblog
1.公开密钥加密 又称非对称加密,需要一对密钥,一个是私人密钥,另一个则是公开密钥.公钥加密的只能私钥解密,用于加密客户上传数据.私钥加密的数据,公钥可以解密,主要用于数字签名.详细介绍可参见维基百科 ...
- jquery如何获取第一个或最后一个子元素
jquery如何获取第一个或最后一个子元素? 通过children方法,children("input:first-child") $(this).children(" ...
- python-unittest学习2--生成报告
上个是小练习 ,这次将unittest模块化一下,也就是吧用例放在case目录下,start放在bin目录下面 -------------------start------------------- ...
- vue 子父组件之间的通信,及在调用组件的地方
这里是用了 element ui 你们也可以看一下管方的文档 http://element.eleme.io/#/zh-CN/component/installation 组件html <div ...
- Hive2.x 版本的安装及配置 以及要注意的事项
博主学习Hadoop学习到Hive,一开始跟着资料去安装Hive 1.x一点问题也没有,方便快捷啊,但是看了一下官方文档,上面好像说Hive 2.0修复了很多bug,那么我想,我还是用Hive2.0好 ...
- [你必须知道的.NET]第二十一回:认识全面的null
发布日期:2008.7.31 作者:Anytao © 2008 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 null.nullable.??运算符.null ...