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+ ...
随机推荐
- VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
暂时可以通过 在 scope template 中自己处理格式化解决 相关issue: 2548
- 64_m3
molequeue-doc-0.8.0-2.20161222giteb397e.fc26.no..> 05-Apr-2017 10:04 451570 molequeue-libs-0.8.0- ...
- Machine Learning系列--归一化方法总结
一.数据的标准化(normalization)和归一化 数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间.在某些比较和评价的指标处理中经常会用到,去除数据的单位限 ...
- Java Eclipse 配置
1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...
- Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070058 android-studio 3.0 from canary 5 to canary 6
我升级android-studio到了3.0 canary 6打包编译安装出现如下错误: 07-11 13:00:39.523 8913-8913/dcpl.com.myapplication E/A ...
- caffe源码整个训练过程
Caffe源码 Blob protected: shared_ptr<SyncedMemory> data_; shared_ptr<SyncedMemory> diff_; ...
- Fiddler Web Session 列表(1)
Web Session 列表 位置: Web Session 列表 位于Fiddler界面的左侧 ,是Fiddler所抓取到的所有Session会话的列表集合. Web Session 列表 栏名词解 ...
- 【caffe-Windows】微软官方caffe之matlab接口配置,以及安装caffe的注意事项
1.在此之前,记录一下之前的错误,在参考博客[caffe-Windows]caffe+VS2013+Windows+GPU配置+cifar使用进行caffe的安装时,其中的一些步骤可以不做,具体见下图 ...
- spring_150807_hibernate_transaction_annotation
实体类: package com.spring.model; import javax.persistence.Entity; import javax.persistence.Id; import ...
- 一个完整的 JS 身份证校验代码
一个完整的 JS 身份证校验代码 身份证号码是由 18 位数字组成的,它们分别表示: (1) 前 1.2 位数字表示: 所在省份的代码; (2) 第 3.4 位数字表示: 所在城市的代码; (3) 第 ...