Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题
2016-04-20 22:36
381 查看
E. Interesting Graph and Apples
题目连接:
http://www.codeforces.com/contest/9/problem/EDescription
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 21 2
2 3
Sample Output
YES1
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; }
相关文章推荐
- 如何实现android和服务器长连接
- 浅谈Android onTouchEvent 与 onInterceptTouchEvent的区别详解
- Android Fragment生命周期
- Android TV磁贴类app自动化框架二次改造(基于UiAutomator)
- Android Studio快捷键
- android onTouch()与onTouchEvent()的区别
- iOS视图控制器的生命周期
- Android Small插件化框架解读——Activity注册和生命周期[阿里工程师分享]
- Android复习
- Android API之android.net.wifi.WifiConfiguration
- android获取指定路径下目录文件
- Andriod中绘(画)图----Canvas的使用详解
- Learn Java for Android Development (第三版)---封面
- ios 创建控制器三种方式
- Android-->Realm(数据库ORM)使用体验,lambda表达式
- IOS综合练习 通讯录 包括顺向传值,代理传值,监听,增删改,归档存储,偏好设置,画线,第三方框架
- 第三方登录之微信登录,基于ThinkSDK
- [android] 帧动画和补间动画
- Android 2.3.5/4.0.3/4.1PowerManager简单总结和心得体会
- Java版本微信企业号的开发--01