FZU 2112 Tickets

这个问题可以转变一下,先要知道有几个连通块,连通块之间肯定需要添加一条边,

还需要知道每个连通块内部需要添加几条边,这个问题等价于求一张图至少需要几笔画成,这个问题的答案是度为奇数的点的个数/2

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
int T;
int n,m;
vector<int>G[maxn];
int tot[maxn];
bool flag[maxn];
bool flag2[maxn];
int q[maxn],sum;
int ans;

void init()
{
    ans=0;
    memset(tot,0,sizeof tot);
    memset(flag,0,sizeof flag);
    memset(flag2,0,sizeof flag2);
}

void read()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) G[i].clear();
    for(int i=1;i<=m;i++)
    {
        int u,v; scanf("%d%d",&u,&v);
        tot[u]++;tot[v]++;
        flag2[u]=1;flag2[v]=1;
        G[u].push_back(v);
        G[v].push_back(u);
    }
}

void dfs(int now)
{
    flag[now]=1; q[sum++]=now;
    for(int i=0;i<G[now].size();i++)
        if(!flag[G[now][i]])
            dfs(G[now][i]);
}

void work()
{
    for(int i=1;i<=n;i++)
    {
        if(!flag[i]&&flag2[i])
        {
            sum=0; ans++; dfs(i);
            int tmp=0;
            for(int j=0;j<sum;j++) if(tot[q[j]]%2==1) tmp++;
            ans=ans+(tmp-1)/2;
        }
    }
    ans--;
    printf("%d\n",ans);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        init();
        read();
        work();
    }
    return 0;
}
时间: 2024-07-31 06:47:16

FZU 2112 Tickets的相关文章

ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Practice 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

FZU Problem 2112 Tickets (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.

FZU 2112 并查集、欧拉通路

原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个联通分量,我们可以用一个并查集来维护,假设有n个联通分量,我们就需要n-1条边把他们连起来. 最后对于每个联通分量来说,我们要使它能一次走完,就是要求他是否满足欧拉通路,也就是这个联通分量中至多有2个度为奇数的点,每多出2个度为奇数的点,就多需要一条边(因为单个连通分量的所有点的度数之和为偶数,所以不可能存在奇数个奇数度数的点). 1 #i

建图 FZU 2112

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 y

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

Problem 2112 Tickets Accept: 309    Submit: 526 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 betwe

&quot;高教社杯&quot;第三届福建省大学生程序设计竞赛

 A.Problem 2102 Solve equation Accept: 1032    Submit: 2471 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

FZU 1096 QS Network

QS Network Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 1096 64-bit integer IO format: %I64d      Java class name: Main In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS.

FZU 1759 欧拉函数 降幂公式

Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a singl