FOJ FZU 2112 Tickets【欧拉通路】

来源:互联网 发布:影响二次销售 淘宝介入 编辑:程序博客网 时间:2024/05/16 13:28

 Problem 2112 Tickets

Accept: 424    Submit: 731
Time Limit: 3000 mSec    Memory Limit : 32768 KB

 Problem Description

You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction between the 2 different cities printed on the ticket. Your prize gives you free airfare to any city to start your cruising, and free airfare back home from wherever you finish your cruising.

You love to sail and don't want to waste any of your free tickets. How many additional tickets would you have to buy so that your cruise can use all of your tickets?

Now giving the free tickets you have won. Please compute the smallest number of additional tickets that can be purchased to allow you to use all of your free tickets.

 Input

There is one integer T (T≤100) in the first line of the input.

Then T cases, for any case, the first line contains 2 integers n, m (1≤n, m≤100,000). n indicates the identifier of the cities are between 1 and n, inclusive. m indicates the tickets you have won.

Then following m lines, each line contains two integers u and v (1≤u, v≤n), indicates the 2 cities printed on your tickets, respectively.

 Output

For each test case, output an integer in a single line, indicates the smallest number of additional tickets you need to buy.

 Sample Input

35 31 31 24 56 51 31 21 61 51 43 21 21 2

 Sample Output

120


题目大意:使用完所有飞机票,问最少购买额外的票数。

对于有票的路径和需要购买的票所构成的路径,最终要形成一个欧拉通路。

对于无向图的欧拉通路:图中形成的通路只有两个或者零个奇数度的点,换句话说:这个图最终能够形成欧拉路径或者是欧拉回路。

假设对于样例2

建图之后得到的奇数度的点为6,那么我们最贪心能够构成一条欧拉通路的奇数度的点为两个,这个时候我们需要连接四个度为奇数的点。相当于构建两条路径。

输出为(6-2)/2;

假设对于样例3

建图之后得到的奇数度的点为0,那么我们最贪心能够构成一条欧拉通路的奇数度的点为零个,这个时候直接输出0即可。

AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int degree[100005];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        memset(degree,0,sizeof(degree));        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            degree[x]++;degree[y]++;        }        int cont=0;        for(int i=1;i<=n;i++)        {            if(degree[i]%2==1)cont++;        }        if(cont>=2)        printf("%d\n",(cont-2)/2);        else printf("0\n");    }}










0 0
原创粉丝点击