CCPC2021网络赛重赛 1010 Bigraph Extension

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<ll,ll> >vp;
ll uset[200005];
ll find(ll x)
{
    ll p=x,t;
    while(uset[p]!=p)p=uset[p];
    while(x!=uset[x])
    {
        t=uset[x];
        uset[x]=p;
        x=t;
    }
    return x;
}
void unionset(ll x,ll y)
{
    if((x=find(x)) == (y=find(y)) )return;
    uset[x]=y;
}
void solve()
{
    vp.clear();
    int n,m,deg[100005]={};
    cin>>n>>m;
    for(int i=1;i<=2*n;i++)uset[i]=i;
    for(int i=1;i<=m;i++)
    {
        ll c,d;
        cin>>c>>d;
        unionset(c,d+n);
        deg[c]++;
        deg[d+n]++;
    }
    int l=1,r=1;
    int res=-1,flag=0;
    queue<int> q;
    for(int i=1;i<=2*n-m;i++)
    {
        if(flag==1)r=res;
        flag=0;
        while(deg[l]==2)l++;
        if(i==2*n-m){for(int k=1;k<=n;k++)if(deg[k]==1)l=k;for(int j=n+1;j<=2*n;j++)if(deg[j]==1)r=j-n;}
        else
        {
            while((find(l)==find(r+n)||deg[r+n]==2))
            {
                if(find(l)==find(r+n) && deg[r+n]!=2 && flag==0){res=r;flag=1;}
                r++;
            }
            unionset(l,r+n);
        }
        vp.push_back(make_pair(l,r));
        deg[l]++;
        deg[r+n]++;
        
    }
    cout<<vp.size()<<endl;
    for(int i=0;i<vp.size();i++)
    {
        cout<<vp[i].first<<' '<<vp[i].second<<endl;
    }
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    for(int i=1;i<=T;i++)solve();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值