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 构造题的更多相关文章

  1. 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 ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  8. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. JS日历控件特效代码layDate

    https://www.js-css.cn/a/jscode/date/2015/0405/1461.html

  2. WordPress友情链接插件的安装

    插件:link manager 这样就安装成功! 在外观小工具里 把右边即可 这样在前台页面上就可看见添加的友情链接了!!!

  3. BeanPostProcessor的五大接口

    BeanPostProcessor 关于对象初始化前后的回调. public interface BeanPostProcessor { //该方法在bean实例化完毕(且已经注入完毕),在after ...

  4. git命令大全【转】

    转自:http://www.jqhtml.com/8235.html 初始化本地git仓库(创建新仓库) git init 配置用户名 git config --global user.name &q ...

  5. MySQL删除数据几种情况以及是否释放磁盘空间【转】

    MySQL删除数据几种情况以及是否释放磁盘空间: 1.drop table table_name 立刻释放磁盘空间 ,不管是 Innodb和MyISAM ; 2.truncate table tabl ...

  6. Linux下多路径multipath配置【转】

    一.multipath在redhat 6.2中的基本配置: 1. 通过命令:lsmod |grep dm_multipath  检查是否正常安装成功.如果没有输出说明没有安装那么通过yum功能安装一下 ...

  7. HOJ 1108

    题目链接:HOJ-1108 题意为给定N和M,找出最小的K,使得K个N组成的数能被M整除.比如对于n=2,m=11,则k=2. 思路是抽屉原理,K个N组成的数modM的值最多只有M个. 具体看代码: ...

  8. selenium grid应用2-多节点执行用例

    启动远程 node我们目前启动的 Hub 与 node 都是在一台主机.那么要在其它主机启动 node 必须满足以下几个要求: 1)本地 hub 主机与远程 node 主机之间可以相互 ping 通 ...

  9. java程序改错题(常见)

    最近跑校招,做了一套java的笔试题. abstract class Name { private String name; public abstract boolean isStupidName( ...

  10. sicily 1154. Easy sort (tree sort& merge sort)

    Description You know sorting is very important. And this easy problem is: Given you an array with N ...