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;
}