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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here is an example implementation in Java: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Student { private String name; private ArrayList<String> hobbies; public Student(String name, ArrayList<String> hobbies) { this.name = name; this.hobbies = hobbies; } public String getName() { return name; } public ArrayList<String> getHobbies() { return hobbies; } public static void main(String[] args) { ArrayList<Student> students = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader("students.txt"))) { String line; while ((line = br.readLine()) != null) { String[] parts = line.split(","); String name = parts[0]; ArrayList<String> hobbies = new ArrayList<>(); for (int i = 1; i < parts.length; i++) { hobbies.add(parts[i]); } students.add(new Student(name, hobbies)); } } catch (IOException e) { System.err.format("IOException: %s%n", e); } Collections.sort(students, (s1, s2) -> s1.getName().compareTo(s2.getName())); for (Student student : students) { System.out.println(student.getName() + ": " + student.getHobbies()); } } } ``` This code reads the student data from a file called "students.txt" (in the same directory as the Java file), creates a `Student` object for each line, and stores them in an `ArrayList`. It then sorts the list by student name using a lambda expression, and finally prints out each student's name and hobbies using a `for` loop.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值