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

FZU2112 Tickets (并查集+欧拉通路)经典

时间:2014-12-09 15:44:43      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:图论   并查集   欧拉通路   

Problem 2112 Tickets

Accept: 309    Submit: 526
Time Limit: 3000 mSec    Memory Limit : 32768 KB

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

“高教社杯”第三届福建省大学生程序设计竞赛
#include<stdio.h>
#include<string.h>

const int N = 100005;

int edgN,fath[N];

int findObj(int x)
{
    if(x!=fath[x])
        fath[x]=findObj(fath[x]);
    return fath[x];
}
void setNode(int u,int v)
{
    u=findObj(u);
    v=findObj(v);
    if(u!=v)
    {
        edgN--;
        fath[u]=v;
    }
}
int main()
{
    int t,linkEdgN[N],ans,n,m,u,v;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n; i++)
        {
            fath[i]=i; linkEdgN[i]=0;
        }
        edgN=0;//记录点与边的差值
        ans=0;//记录顶点度数为奇数的顶点个数
        while(m--)
        {
            scanf("%d%d",&u,&v);
            if(linkEdgN[u]==0)
                edgN++;
            if(linkEdgN[v]==0)
                edgN++;
            setNode(u,v);
            
            linkEdgN[u]++;
            linkEdgN[v]++;
            if(linkEdgN[u]&1)ans++; else ans--;
            if(linkEdgN[v]&1)ans++; else ans--;
        }
        edgN--;//最少加的边数使出现的点成为一个连通图
        ans-=(edgN+1)*2;//加了边之后图中还剩ans个点的度数为奇度顶点(除去两个起始点和终点为奇度)
        ans/=2; //剩下的点的度数为奇度数的顶点个数两两相连
        if(ans<0)
            printf("%d\n",edgN);
        else printf("%d\n",edgN+ans);
    }
}


FZU2112 Tickets (并查集+欧拉通路)经典

标签:图论   并查集   欧拉通路   

原文地址:http://blog.csdn.net/u010372095/article/details/41823761

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