2017湖南多校第二场-20170312

通过分析一组房屋间的油迹路线图,使用并查集算法判断是否仅由一位驾车者完成所有移动。该问题涉及一笔画原理及图论知识,旨在判断特定情况下路径的可行性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1834: Oil
Submit Status Time Limit: 2 Sec Memory Limit: 128 Mb Submitted: 45 Solved: 15
Description
Mr. Mike owns many houses. Every day wakes up in one of his houses and drives around between this house and the other houses he owns till he gets tired. Then he goes to bed in whatever house he is in when he decides to stop. Mr. Mike’s car leaks oil, so by visiting houses he leaves a trail of oil that could be used to track his journey. However, Mr. Mike also has servants who stay in his houses, and they also sometimes drive between his houses in their cars. By coincidence, their cars also leak oil, so at the end of the day there may be many oil trails between houses. Mr. Mike and his servants will never leave one house and return to the same house without first having visited a different house. The question is, given a map of the houses and the oil trails between them at the end of a particular day, can you say whether it is possible that Mr. Mike was the only person driving that day?

Input
The first line of input is an integer between 500 and 1000 (inclusive). This tells you how many test cases there are. After this line the lines come in pairs (one for every test case). The first line for each test case also contains a single integer between 2 and 100 (inclusive). This is how many houses there are in this test case. The next line is also composed of integers. The first tells you how many triples the line contains. After this the line consists of triples of integers. A triple x y z tells you there are z oil trails between house x and house y (house numbering begins at 0, and there are at most 5 trails between each pair of houses). Trails have no direction so if the triple x y z occurs then the triple y x z is assumed to exist but will not appear on the input line. If the house x and house y do not appear in a triple then there are no oil trails between them.

Output
For each test case output the string “yes” if it is possible (based only on this information) that Mr. Mike created all the oil trails himself. Otherwise output “no”. Use one line for each test case.

Sample Input
5
3
3 0 1 1 0 2 1 1 2 1
3
2 0 1 1 0 2 2
4
2 0 1 2 2 3 2
3
1 0 2 3
2
0
Sample Output
yes
yes
no
yes
yes

解题思路:并查集判断连通分量,一笔画条件,0或2个奇数结点

#include<bits/stdc++.h>
using namespace std;
const int MAXN=110;
int d[MAXN],fa[MAXN];
int G[110][110];

void init(int n)
{
    memset(d,0,sizeof(d));
    memset(G,-1,sizeof(G));
    for(int i=0;i<=n;i++)
        fa[i]=i;
}

int findfa(int x)
{
    if(fa[x]==x) return fa[x]=x;
    return fa[x]=findfa(fa[x]);
}

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int n,m;
        scanf("%d",&n);
        scanf("%d",&m);
        init(n);
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(G[u][v]<0)
                G[u][v]=G[v][u]=1;
            d[u]+=w; d[v]+=w;
            int uu=findfa(u),vv=findfa(v);
            if(uu!=vv)
            {
                fa[uu]=vv;
            }
        }
        int cnt=0,flag=0,rt=-1;
        int tmp;
        for(int i=0;i<n;i++)
        {
            if(d[i]==0) continue;
            if(d[i]&1) cnt++;//奇数
            tmp=findfa(i);
            if(rt<0) rt=tmp;
            else if(tmp!=rt)
            {
                flag=1;//不连通
                break;
            }
        }
        if(flag) printf("no\n");
        else if(cnt==0||cnt==2) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值