CodeForces - 9E Interesting Graph and Apples【并查集】

【题目描述】
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.

【输入】
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.

【输出】
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.

【样例输入】
3 2
1 2
2 3

【样例输出】
YES
1
1 3

题目链接:https://codeforces.com/contest/9/problem/E

代码如下:

#include <bits/stdc++.h>
using namespace std;
static const int MAXN=50;
int fa[MAXN+10],deg[MAXN+10];
int n,m;
bool flag;
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin>>u>>v;
        deg[u]++,deg[v]++;
        if(find(u)==find(v)) flag=true;
        fa[fa[u]]=fa[v];
    }
    bool flag1=true;
    for(int i=1;i<=n;i++) if(deg[i]!=2) flag1=false;
    for(int i=2;i<=n;i++) if(find(i)!=find(1)) flag1=false;
    if(flag1)
    {
        cout<<"YES"<<endl<<0<<endl;
        return 0;
    }
    bool flag2=false;
    for(int i=1;i<=n;i++) if(deg[i]>2) flag2=true;
    if(flag2 || m>=n || flag)
    {
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl<<n-m<<endl;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(deg[i]<2 && deg[j]<2 && find(i)!=find(j))
            {
                fa[fa[i]]=fa[j];
                deg[i]++,deg[j]++;
                cout<<i<<" "<<j<<endl;
            }
    for(int i=1;i<=n;i++)
        if(deg[i]<2)
        {
            cout<<i<<" ";
            break;
        }
    for(int i=n;i>=1;i--)
        if(deg[i]<2)
        {
            cout<<i<<endl;;
            break;
        }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值