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+ ...
随机推荐
- jquery 生成二维码
jquery的二维码生成插件qrcode,在页面中调用该插件就能生成对应的二维码 <!DOCTYPE html> <html> <head> <meta ch ...
- bootstrap通过ajax请求JSON数据后填充到模态框
1. JSP页面中准备模态框 <!-- 详细信息模态框(Modal) --> <div> <div class="modal fade" id=& ...
- webgote的例子(6)SQL注入(盲注)
SQL Injection - Blind (WS/SOAP) 本期演示的是盲注的手法.有些网站在与数据库交互的地方进行了很好的修饰,将报错的语句进行修改,即使你找到了注入点也无法下手拿数据,这个时候 ...
- webgote的例子(5)Sql注入(Blog)
SQL Injection - Stored (Blog) (本章内容):留言板的注入 看到这个页面先看以下这个页面是做什么的.进行正常的写入发现我每写一句话,其内容都会写到下面的entry里面 在尝 ...
- ubuntu之安装pycharm编辑器
pycharm是Java写的,运行需要Java环境. 安装java jdk sudo add-apt-repository ppa:webupd8team/java sudo apt-get upda ...
- linux常用命令一些解释
ls 命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文 件及文件夹清单. 通过ls 命令不仅可以查看li ...
- mysql 操作时间戳
1.将long显示成时间 SELECT FROM_UNIXTIME(1249488000, '%Y%m%d' ) 2.日期格式化成时间戳 SELECT UNIX_TIMESTAMP('2016-05- ...
- 【hdoj_1042】N!(大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目说明待求阶乘的数最大为10000,而10000!的位数为35660(这个数是上网查的),所以已经 ...
- 在microsoft/dotnet:2.0.0-sdk中安装vim编辑器
在Docker中安装了dotnetcore 2.0.0 的sdk,没有发现可用的编辑器,该sdk是基于debian:stretch构建的,以前没玩过debian,因为CentOS玩的多一些所以对vim ...
- 【洛谷】P4585 [FJOI2015]火星商店问题
题解 题目太丧,OJ太没有良心,我永远喜欢LOJ! (TLE报成RE,垃圾洛谷,我永远喜欢LOJ) 好的,平复一下我debug了一上午崩溃的心态= =,写一写这道题的题解 把所有限制去掉,给出一个值, ...