E. Interesting Graph and Apples

E. Interesting Graph and Apples
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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 ≤ 500 ≤ m ≤ 2500) — the amount of vertices and edges respectively. The following lines contain pairs of numbers xi and yi (1 ≤ xiyi ≤ 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.

Examples
input
3 2
1 2
2 3
output
YES
1
1 3

题意:给出了一个图,有n个点,m条边。然后问该图形是否能添加尽量少的边使之成为一个环。输出yes或者no,如果是yes,同时按字典序输出最少添加的边。

题解:就是先考虑一个点,入度最大只能为2。如果大于2,在补边使之成为的环的过程中,必然会出现两个环,因为要经过所有点。可以这么想,先想象出一个环,当环不止一个时,即往环上切一刀,那么就有点的入度要大于2,所以要满足条件,入度只能小于等于2。然后运用并查集。如果新输入的边之前已经属于同一个集合,那么再加上一条边,就必然会成环,故这种情况也是不可能的。接着只要从小到大搜索,找到两个入度小于2的,且不属于一个集合,就连边。最后找出剩下所有入度小于2的,连边,就可以了。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int maxn = 60;
struct node{
    int fa[maxn];
    void init(){
        for (int i = 0;i < maxn;i++){
            fa[i] = i;
        }
    }
    int found(int x) {return fa[x] = fa[x]==x?x:found(fa[x]);}
    bool combine(int x,int y){
        int a = found(x),b = found(y);
        if(a==b) return 0;
        else fa[b] = a;
        return 1;
    }
} uf;

int n,m;
int s[maxn];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        bool flag = 1;
        uf.init();
        for (int i = 0;i <= n;i++) s[i] = 2;
        for (int i = 0;i < m;i++){
            int c,d;
            scanf("%d %d",&c,&d);
            s[c]--,s[d]--;
            if(s[c]<0||s[d]<0) flag = 0;
            if(uf.found(c)==uf.found(d)&&i!=n-1)
                flag = 0;
            uf.combine(c,d);
        }
        if(flag){
            puts("YES");
            printf("%d\n",n-m);
            for (int i = 1;i <= n;i++){
                //cout<



你可能感兴趣的:(OnlineJudge,codeforce)