Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图

32 篇文章 0 订阅
24 篇文章 0 订阅

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.

Sample test(s)
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.
题意要求,求连边使图存在奇数边的环,图不一定连通,也没有自连和重边。
要求连边成奇边的环,分情况讨论:
1:如果只有单个的点形成的图,那任选三个点就可以组成一个奇连环,总个数就是c(n,3);
2:最多只有两个点连在一起(只需要判定入度最大只有1就可以了),两个点连在一起的个数是m个,则这两个点,再加上任意别的一个点可以组成一条奇边环,最大只要加二条边,方案数是m * (n-2);
3:存在三个点一上的图,这样的图如果存在奇环,那么不要加边答案就是0,1,如何判断是否有奇环呢,有奇环就不是二分图,如果没有奇环,就一定是二分图了,所有只要dfs,染色成x y集合,如果能构成二分图,说明没有奇环,答案就是sum(c(x,2)+ c(y,2)),也就是在x集合任选两点连一条边构成奇环,y集合任选两点连一条边构成奇环。
全部就解决了,总的复杂度为o(m),因为每个点,边,最多走一次。

#define N 100050
#define M 100050
#define maxn 205
#define MOD 1000000000000000007
int n,m,x,y,in[N],out[N],color[N];
ll ans = 0,ansCount[2];
vector<int> p[N];
bool isOne(){
    ans = (ll)n * ((ll)n-1) * ((ll)n-2) / 6;
    FI(n){if(in[i] > 0) return false;}
    return true;
}
bool isTwo(){
    ll mm = 0;
    FI(n){
        if(in[i] >= 2 ) return false;
        else if(in[i] == 1)
        mm++;
    }
    ans = mm * ((ll)n-2ll)/2ll;
    return true;
}
bool DFS(int pos,int flag){
    color[pos] = flag;
    ansCount[flag]++;
    FI(p[pos].size()){
        int goal = p[pos][i];
        if(color[goal] == -1){
            if(!DFS(goal,flag ^ 1))
            return false;
        }
        else if(color[goal] != (flag^1))
            return false;
    }
    return true;
}
ll getC2(ll x){
    if(x <= 1) return 0;
    return x * (x - 1) /2;
}
ll FindTwo(int pos){
    ansCount[0] = ansCount[1] = 0;
    if(!DFS(pos,0)) return -1;
    else {
        return getC2(ansCount[0]) + getC2(ansCount[1]);
    }
}
int main()
{
    while(S2(n,m)!=EOF)
    {
        FI(n){p[i].clear();}
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(color,-1,sizeof(color));
        FI(m){
            S2(x,y);x--;y--;
            p[x].push_back(y);p[y].push_back(x);in[x]++;out[x]++;in[y]++;out[y]++;
        }
        if(isOne()){cout<<"3 "<<ans<<endl;}
        else if(isTwo()){cout<<"2 "<<ans<<endl;}
        else {
            ans = 0;
            FI(n){
                if(color[i] == -1){
                    ll temp = FindTwo(i);
                    if(temp == -1){
                        ans = -1;
                        break;
                    }
                    ans += temp;
                }
            }
            if(ans == -1)
            cout<<"0 1"<<endl;
            else
            cout<<"1 "<<ans<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Amber 大牛关于图论的总结 ,1.1M 大小.... 1. 图论 Graph Theory 1.1. 定义与术语 Definition and Glossary 1.1.1. 图与网络 Graph and Network 1.1.2. 图的术语 Glossary of Graph 1.1.3. 路径与回路 Path and Cycle 1.1.4. 连通性 Connectivity 1.1.5. 图论中特殊的集合 Sets in graph 1.1.6. 匹配 Matching 1.1.7. 树 Tree 1.1.8. 组合优化 Combinatorial optimization 1.2. 图的表示 Expressions of graph 1.2.1. 邻接矩阵 Adjacency matrix 1.2.2. 关联矩阵 Incidence matrix 1.2.3. 邻接表 Adjacency list 1.2.4. 弧表 Arc list 1.2.5. 星形表示 Star 1.3. 图的遍历 Traveling in graph 1.3.1. 深度优先搜索 Depth first search (DFS) 1.3.1.1. 概念 1.3.1.2. 求无向连通图中的桥 Finding bridges in undirected graph 1.3.2. 广度优先搜索 Breadth first search (BFS) 1.4. 拓扑排序 Topological sort 1.5. 路径与回路 Paths and circuits 1.5.1. 欧拉路径或回路 Eulerian path 1.5.1.1. 无向图 1.5.1.2. 有向图 1.5.1.3. 混合图 1.5.1.4. 无权图 Unweighted 1.5.1.5. 有权图 Weighed — 中国邮路问题The Chinese post problem 1.5.2. Hamiltonian Cycle 哈氏路径与回路 1.5.2.1. 无权图 Unweighted 1.5.2.2. 有权图 Weighed — 旅行商问题The travelling salesman problem 1.6. 网络优化 Network optimization 1.6.1. 最小生成树 Minimum spanning trees 1.6.1.1. 基本算法 Basic algorithms 1.6.1.1.1. Prim 1.6.1.1.2. Kruskal 1.6.1.1.3. Sollin(Boruvka) 1.6.1.2. 扩展模型 Extended models 1.6.1.2.1. 度限制生成树 Minimum degree-bounded spanning trees 1.6.1.2.2. k小生成树 The k minimum spanning tree problem(k-MST) 1.6.2. 最短路Shortest paths 1.6.2.1. 单源最短路 Single-source shortest paths 1.6.2.1.1. 基本算法 Basic algorithms 1.6.2.1.1.1. Dijkstra 1.6.2.1.1.2. Bellman-Ford 1.6.2.1.1.2.1. Shortest path faster algorithm(SPFA) 1.6.2.1.2. 应用Applications 1.6.2.1.2.1. 差分约束系统 System of difference constraints 1.6.2.1.2.2. 有向无环图上的最短路 Shortest paths in DAG 1.6.2.2. 所有顶点对间最短路 All-pairs shortest paths 1.6.2.2.1. 基本算法 Basic algorithms 1.6.2.2.1.1. Floyd-Warshall 1.6.2.2.1.2. Johnson 1.6.3. 网络流 Flow network 1.6.3.1. 最大流 Maximum flow 1.6.3.1.1. 基本算法 Basic algorithms 1.6.3.1.1.1. Ford-Fulkerson method 1.6.3.1.1.1.1. Edmonds-Karp algorithm 1.6.3.1.1.1.1.1. Minimum length path 1.6.3.1.1.1.1.2. Maximum capability path 1.6.3.1.1.2. 预流推进算法 Preflow push method 1.6.3.1.1.2.1. Push-relabel 1.6.3.1.1.2.2. Relabel-to-front 1.6.3.1.1.3. Dinic method 1.6.3.1.2. 扩展模型 Extended models 1.6.3.1.2.1. 有上下界的流问题 1.6.3.2. 最小费用流 Minimum cost flow 1.6.3.2.1. 找最小费用路 Finding minimum cost path 1.6.3.2.2. 找负权圈 Finding negative circle 1.6.3.2.3. 网络单纯形 Network simplex algorithm 1.6.4. 匹配 Matching 1.6.4.1. 二分图 Bipartite Graph 1.6.4.1.1. 无权图-匈牙利算法 Unweighted - Hopcroft and Karp algorithm 1.6.4.1.2. 带权图-KM算法 Weighted –Kuhn-Munkres(KM) algorithm 1.6.4.2. 一般图General Graph 1.6.4.2.1. 无权图-带花树算法 Unweighted - Blossom (Edmonds) 1.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值