码迷,mamicode.com
首页 > 其他好文 > 详细

FZU Problem 2112 Tickets (dfs 欧拉图啊)

时间:2014-12-05 21:21:54      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:fzu   数学   dfs   

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2112


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.

bubuko.com,布布扣 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.

bubuko.com,布布扣 Output

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

bubuko.com,布布扣 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

bubuko.com,布布扣 Sample Output

1
2
0

bubuko.com,布布扣 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:
给出n个城市和m张票,
求需要额外再买几张票可以用完已经给出的票,每张票只能用一次!

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100017;
vector <int >v[maxn];
int d[maxn];
int vis[maxn];
int k;
int dfs(int x)
{
    if(vis[x])
        return k;
    vis[x] = 1;
    if(d[x]%2 == 1)
        k++;
    for(int i = 0; i < v[x].size(); i++)
    {
        dfs(v[x][i]);
    }
    return k;
}
int main()
{
    int t;
    int n, m;
    scanf("%d",&t);
    while(t--)
    {
        memset(d,0,sizeof(d));
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++)
            v[i].clear();
        int a, b;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            v[b].push_back(a);
            d[a]++;
            d[b]++;
        }
        int tt, kk = 0;
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            k = 0;
            if(d[i] == 0 || vis[i])
                continue;
            kk++;//几个分块
            tt = dfs(i);
            //printf("tt::%d\n",tt);
            if(tt > 2)
                tt = (tt-2)/2;
            else
                tt = 0;
            ans += tt;
        }
        //printf("kk::%d\n",kk);
        if(kk > 1)
            ans += (kk-1);
        printf("%d\n",ans);
    }
    return 0;
}


FZU Problem 2112 Tickets (dfs 欧拉图啊)

标签:fzu   数学   dfs   

原文地址:http://blog.csdn.net/u012860063/article/details/41752787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!