Codeforces 557D - Vitaly and Cycle [二分图染色]【图论】

题目链接:http://codeforces.com/problemset/problem/557/D
——————————————————————————————————————
D. Vitaly and Cycle
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
After Vitaly was expelled from the university, he became interested in the graph theory.

Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.

Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w — the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.

Two ways to add edges to the graph are considered equal if they have the same sets of added edges.

Since Vitaly does not study at the university, he asked you to help him with this task.

Input
The first line of the input contains two integers n and m ( — the number of vertices in the graph and the number of edges in the graph.

Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n) — the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.

It is guaranteed that the given graph doesn’t contain any loops and parallel edges. The graph isn’t necessarily connected.

Output
Print in the first line of the output two space-separated integers t and w — the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.

Examples
input
4 4
1 2
1 3
4 2
4 3
output
1 2
input
3 3
1 2
2 3
3 1
output
0 1
input
3 0
output
3 1
Note
The simple cycle is a cycle that doesn’t contain any vertex twice.
——————————————————————————————————————
题目大意:
就是现在一个N个节点,M条边的无向图,问你最少添加多少条边,能找到一个奇圈,添加的方法有多少个

解题思路:

判断奇圈偶圈,很容易想到二分图染色,那么通过二分图染色,就能确定很多信息了,

首先能确定的是最多也就添加3条边, 3条边就能将三个点变成一个奇圈了,

那么添加边就是要变成一个3个节点的奇圈

那么就分这四种情况讨论

1.添加0条边
就是原图中有奇圈了, 通过染色判断,方案数就是1了
2.添加1条边
只需添加一条边的时候就是一个点a连向点b,c. 这时候将b,c连上就好了
这种情况需要在一侧的多个节点都能和另一边的节点直接或间接连上才行,
所以我用了一个并查集维护联通性,然后统计个数
3.添加2条边
如果一个节点至多被一条边连着,
那么想要构成奇圈,就要将边的两个端点连上另外的一个一个节点构成奇圈
方案数就是 m(n2)
4.添加3条边
只有一条边都没有的时候才要添加三条边,
方案数就是在n个点取3个节点的方案数

分类讨论即可

附本题代码
——————————————————————————————————————

#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;

const int N = 1e5+7;

/************************************/
int k,n,m;

int pre[N];

int findi(int x){
    int r=x;
    while(r!=pre[r])r=pre[r];
    for(int i=x,j;r!=i;){
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}

void join(int x,int y){
    int fx=findi(x),fy=findi(y);
    pre[fx]=fy;
}


vector<int >G[N];

int col[N];
//二分图染色  用来判断是不是二分图
int color(int s){
    queue<int>q;int ans =0 ;
    q.push(s);col[s]=1;
    while(!q.empty()){
        int u = q.front();q.pop();
        int gz=G[u].size();
        for(int i=0,to;i<gz;i++){
            to=G[u][i];
            if(col[to]==0){
                col[to]=3-col[u];
                q.push(to);
            }
            else if(col[to]==col[u])
                ans++;
        }
    }
    return ans>>1;
}

void init(){
    for(int i=0;i<=n;i++) G[i].clear(),col[i]=0,pre[i]=i;
}

void add(int u,int v){
    G[u].push_back(v);
    G[v].push_back(u); //无向图才需要
}

int x[N],y[N];

int main(){
    scanf("%d%d",&n,&m);
    init();
    for(int i=1,u,v;i<=m;i++){
        scanf("%d%d",&u,&v);
        add(u,v);join(u,v);
    }

    if(m==0) return 0*printf("3 %lld\n",(LL)n*(n-1)*(n-2)/6);

    int flag=0;
    for(int i=1;i<=n;i++)
        if(col[i]==0) flag+=color(i);

    if(flag) return 0*puts("0 1");

    LL ans = 0;
    flag=0;
    for(int i=1;i<=n;i++){
        if(G[i].size()>1) flag=1;
        if(col[i]==1)ans+=x[findi(i)]++;
        else         ans+=y[findi(i)]++;

    }
    if(flag) printf("1 %lld\n",ans);
    else     printf("2 %lld\n",(LL)m*(n-2));
    return 0;
}
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值