zoukankan      html  css  js  c++  java
  • cf311div2 D Vitaly and Cycle

    题目
    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.

    给出一个无向图,问最少加多少边可以使图中存在奇数边的圈,输出最小边数及方法数
    可以分成四种情况讨论

    • 图中没有边,此时需要加三条边,方法数为C(3,n);
    • 图中点的最大度数为1,此时需要加两条边,方法数为m*(n-2),即一条边与任意一个单独的点组合;
    • 图中已有奇数边的圈;
    • 最大度数大于1且不存在奇数边的圈。

    最后两种情况只需用染色法判断二分图即可,用bfs,如果碰到相邻两点是同种颜色的情况即说明有奇数边的圈。对最后一种情况在每个联通块中任选两点相同颜色的点连接即可构成一个奇数边的圈。

    /* ***********************************************
    Author        :letter-song
    Created Time  :2018/3/26 15:33:46
    File Name     :3_26.cpp
    ************************************************ */
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <stack>
    #include <cmath>
    #include <cstdlib>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    
    #define lson o<<1,l,m
    #define rson o<<1|1,m+1,r
    #define pii pair<int,int>
    #define mp make_pair
    #define ll long long
    #define INF 0x3f3f3f3f
    const int maxn=1e5+5;
    ll n,m;
    int ind[maxn],col[maxn];    //度数,染色
    vector <int>G[maxn];
    ll jud()
    {
        ll ans=0;
        ll num[2];
        memset(col,-1,sizeof(col));
        queue<int>q;
        for(int i=1;i<=n;i++)
        {
            if(col[i]==-1)
            {
                num[0]=1;
                num[1]=0;
                q.push(i);
                col[i]=0;
                while(!q.empty())
                {
                    int u=q.front();
                    q.pop();
                    for(int j=0;j<G[u].size();j++)
                    {
                        int tmp=G[u][j];
                        if(col[tmp]==-1)
                        {
                            q.push(tmp);
                            col[tmp]=!col[u];
                            num[col[tmp]]++;
                        }   
                        else if(col[tmp]==col[u])
                          return 0;
                        else
                          continue;
                    }
                }
                ans+=num[0]*(num[0]-1)/2+num[1]*(num[1]-1)/2;    //C(2,num[0])+C(2,num[1])
            }
        }
        return ans;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        cin>>n>>m;
        int u,v,maxind=0;
        for(int i=0;i<m;i++)
        {
            cin>>u>>v;
            G[u].push_back(v);
            G[v].push_back(u);
            ind[v]++;
            ind[u]++;
            maxind=max(maxind,max(ind[u],ind[v]));
        }
        if(m==0)
          cout<<3<<' '<<n*(n-1)*(n-2)/6<<endl;
        else if(maxind==1)
          cout<<2<<' '<<m*(n-2)<<endl;
        else
        {
            ll t=jud();
            if(!t)
              cout<<0<<' '<<1<<endl;
            else
              cout<<1<<' '<<t<<endl;
        }
        return 0;
    }
  • 相关阅读:
    L3-1 二叉搜索树的结构 (30 分)
    L3-2 森森快递 (30 分)(贪心+线段树/分块)
    三分(凸函数)
    (三分入门)(凹函数)
    Print Article(斜率DP入门+单调队列)
    PTA 逆散列问题 (30 分)(贪心)
    二叉树遍历相关
    7-5 堆中的路径 (25 分)
    Grouping ZOJ
    D
  • 原文地址:https://www.cnblogs.com/acagain/p/9180717.html
Copyright © 2011-2022 走看看