Codeforces Round #311 (Div. 2) D. Vitaly and Cycle

48 篇文章 0 订阅
11 篇文章 0 订阅

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

Copy

(copy)

4 4
1 2
1 3
4 2
4 3

Output

Copy

(copy)

1 2

Input

Copy

(copy)

3 3
1 2
2 3
3 1

Output

Copy

(copy)

0 1

Input

Copy

(copy)

3 0

Output

Copy

(copy)

3 1

Note

The simple cycle is a cycle that doesn't contain any vertex twice.

题意:给你n个连续木板的长度ai和给长度加1的花费bi,你可以给每块木板增加任意长度,问最少花多少钱,才能使相邻两块木板长度都不同

思路:枚举第i-1和i个+0,+1,+2的九种情况,在不相等的情况中取花费最小的就可以了

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<queue>
    #include<map>
    #include<algorithm>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define maxn 400000
    #include<vector>
    #define ll long long
    ll a[maxn],b[maxn],dp[maxn][5];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                dp[i][0]=dp[i][1]=dp[i][2]=1e18+7;
                scanf("%lld %lld",&a[i],&b[i]);
            }
            //dp[1][0]=dp[1][1]=d[1][2]=dp[2][0]=dp[2][1]=dp[2][2]=inf;
            for(ll i=1;i<=n;i++)
            {
     
                for(ll j=0;j<=2;j++)//前一项
                {
                    for(ll k=0;k<=2;k++)//后一项
                    {
                        if(a[i-1]+j!=a[i]+k)dp[i][k]=min(dp[i][k],dp[i-1][j]+b[i]*k);
                    }
                }
            }
            printf("%lld\n",min(min(dp[n][0],dp[n][1]),dp[n][2]));
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值