zoukankan      html  css  js  c++  java
  • FZU2112 Tickets(欧拉通路)

    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
    0

    题解:

    每次得到一条边时,把两个城市的度都加一,最后得出有多少个度为奇数的城市,因为第一个城市和最后一个城市有接送所以减去二,剩下的城市数除以二得到的就是结果。注意当没有度为奇数的城市时直接输出0。(总结一句话,每个城市有进就地有出,而票都是双向的所以只要度为偶数就满足。)

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    int board[100005];
    
    int main()
    {
    	int N;
    	cin>>N;
    	while(N--)
    	{
    		int n,m;
    		memset(board,0,sizeof(board));
    		scanf("%d %d",&n,&m);
    		while(m--)
    		{
    			int a,b;
    			scanf("%d %d",&a,&b);
    			board[a]++;
    			board[b]++;
    		}
    		int sum = 0;
    		for(int i=1 ; i<=n ; i++)
    		{
    			if(board[i]&1)sum++;
    		}
    		if(sum == 0);
    		else
    		{
    			sum -= 2;
    			if(sum)sum /= 2;
    		}
    		printf("%d
    ",sum);
    	}
    
    	return 0;
    }
  • 相关阅读:
    Golang-字符串常用的系统函数
    35.HTML--网页自动跳转 5种方法
    34.js----JS 开发者必须知道的十个 ES6 新特性
    33. 禁止鼠标右键保存图片、禁止拖动图片
    32.js 判断当前页面是否被浏览
    31.JS实现控制HTML5背景音乐播放暂停
    30.get和post的区别
    29.html5 移动端开发总结
    28.json数组,select选择,input输出对应数据
    27.给input边框和背景颜色设置全透明
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514143.html
Copyright © 2011-2022 走看看