题目描述
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;
}