AtCoder Grand Contest 014 B - Unplanned Queries

题目描述

Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.

First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.

Then, Aoki gave him M queries. The i-th of them is as follows:

Increment the number written at each edge along the path connecting vertices ai and bi, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.

Determine whether there exists a tree that has the property mentioned by Takahashi.

Constraints
2≤N≤105
1≤M≤105
1≤ai,bi≤N
ai≠bi

输入

Input is given from Standard Input in the following format:

N M
a1 b1
:
aM bM

输出

Print YES if there exists a tree that has the property mentioned by Takahashi; print NO otherwise.

样例输入
4 4
1 2
2 4
1 3
3 4
样例输出

YES
题目大意:给出n个点和m条路径,每条路径上所有的边的权值都+1,问你能不能构建一个树使得最后每条边的权值都是偶数

解法:我们假设有这么一棵树,再往树上加上一个0号节点作为根,每次读进来u,v两个节点,设p为lca(u,v),我们每次连接u,v就相当于连接u,p和连接v,p,如果我们再把0,p连接两次,很明显对答案并没有影响,这时候连接u,p和连接v,p就变成了连接0,u和连接0,v。很明显,我们每次连接 u i u_i ui,0和连接 p i p_i pi,0。我们假设某一个 u i u_i ui出现了奇数次,那么 u i u_i ui,0我们必然是连接了奇数次,那么与 u i u_i ui相连接的某一条边一定是出现了奇数次,所以我们只需要统计每个点出现的次数,一但所有点出现的次数都是偶数,那么肯定就存在这样一棵树。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int n,m;
int z[maxn];
int main()
{
    scanf("%d%d", &n, &m);
    while (m--)
    {
        int tmp1,tmp2;
        scanf("%d%d", &tmp1, &tmp2);
        ++z[tmp1];
        ++z[tmp2];
    }
    int flag = 1;
    for (int i = 1; i <= n; ++i)
        if (z[i] % 2)
            flag = 0;
    if (flag)
        puts("YES");
    else
        puts("NO");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值