Tickets(欧拉通路)

Problem 2112 Tickets

Accept: 238    Submit: 420
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

3
5 3
1 3
1 2
4 5
6 5
1 3
1 2
1 6
1 5
1 4
3 2
1 2
1 2

Sample Output

1
2

       题意:

       给出 T (<= 100)个 case,后每个 case 首先给出 N 和 M,代表有 N 个节点,M 条边,问最少要增加多少条边使能遍历给出的所有边。

 

       思路:

       欧拉通路。只有 0 个或者 2个 奇度数结点时存在一条欧拉通路。故判断有多少个奇度数结点即可。若不存在则说明已存在欧拉通路,输出0。若存在奇度数结点,则必然从一个奇度数结点出发到另外一个奇度数结点。每增加一条边就可以使两个奇数结点变为偶数,所以应该增加的边数是 (ans - 2)/ 2 条。

 

       AC:

#include <cstdio>
#include <cstring>

using namespace std;

const int NMAX = 100005;

int deg[NMAX];

int main() {
        int t;
        scanf("%d", &t);
        while(t--) {
                int n, m;
                scanf("%d%d", &n, &m);

                memset(deg, 0, sizeof(deg));

                while(m--) {
                        int a, b;
                        scanf("%d%d", &a, &b);
                        ++deg[a];
                        ++deg[b];
                }

                int ans = 0;
                for (int i = 1; i <= n; ++i) {
                        if (deg[i] % 2) ++ans;
                }

                if (!ans) printf("0\n");
                else printf("%d\n", (ans - 2) / 2);
        }
        return 0;
}

 

 

 

你可能感兴趣的:(c)