uva 12118 Inspector's Dilemma

In a country, there are a number of cities. Each pair of city is connected by a highway, bi-directional ofcourse. A road-inspector’s task is to travel through the highways (in either direction) and to check ifeverything is in order. Now, a road-inspector has a list of highways he must inspect. However, it mightnot be possible for him to travel through all the highways on his list without using other highways. Heneeds a constant amount of time to traverse any single highway. As you can understand, the inspectoris a busy fellow does not want to waste his precious time. He needs to know the minimum possibletime to complete his task. He has the liberty to start from and end with any city he likes. Please helphim out.InputThe input file has several test cases. First line of each case has three integers: V (1 ≤ V ≤ 1000), thenumber of cities, E (0 ≤ E ≤ V ∗ (V − 1)/2), the number of highways the inspector needs to check andT (1 ≤ T ≤ 10), time needed to pass a single highway. Each of the next E lines contains two integersa and b (1 ≤ a, b ≤ V , a ̸= b) meaning the inspector has to check the highway between cities a and b.The input is terminated by a case with V = E = T = 0. This case should not be processed.OutputFor each test case, print the serial of output followed by the minimum possible time the inspector needsto inspect all the highways on his list. Look at the output for sample input for details.Sample Input5 3 11 21 34 54 4 11 21 42 33 40 0 0Sample OutputCase 1: 4Case 2: 4

点击打开链接

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

int n,E,T;
int deg[1100],par[1100],rk[1100];
bool used[1100];
void init()
{
    memset(deg,0,sizeof(deg));
    memset(used,0,sizeof(used));
    for(int i=0;i<=n;i++)par[i]=i,rk[i]=0;
}
int f(int x)
{
    if(par[x]==x)return x;
    else return par[x]=f(par[x]);
}
void unite(int x,int y)
{
    x=f(x);y=f(y);
    if(x==y) return;
    if(rk[x]<rk[y]) par[x]=y;
    else
    {
        par[y]=x;
        if(rk[x]==rk[y]) rk[x]++;
    }
}
int main()
{
    int Cases=0;
    while(scanf("%d%d%d",&n,&E,&T),n||E||T)
    {
        init();
        for(int i=0;i<E;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            deg[u]++;deg[v]++;used[u]=used[v]=true;
            unite(u,v);
        }
        vector<int> ans;
        for(int i=1;i<=n;i++)
        {
            if(deg[i]%2==1)
            {
                 ans.push_back(i);
            }
        }
       for(int i=0;i<ans.size();i++)
        for(int j=i+1;j<ans.size();j++) unite(ans[i],ans[j]);
       set<int> s;
       for(int i=1;i<=n;i++) if(used[i]) s.insert(f(i));
       int res=E;
       if(ans.size()>=2) res+=ans.size()/2-1;
       if(s.size()>0) res+=s.size()-1;
        printf("Case %d: %d\n",++Cases,res*T);
    }
    return 0;
}
刚开始想dfs时,发现挺烦的,想想还是用并查集,后来看别人的题解发现还是dfs简单,但是我没有发现用并查集,所以贴个代码。

首先把所有奇数点首尾相连,然后用set判断整张图的分块。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值