A12792 | Cow and Snacks
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are $n$ snacks flavors, numbered with integers $1, 2, \ldots, n$ . Bessie has $n$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
- First, Bessie will line up the guests in some way.
- Then in this order, guests will approach the snacks one by one.
- Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
There are $n$ snacks flavors, numbered with integers $1, 2, \ldots, n$ . Bessie has $n$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
- First, Bessie will line up the guests in some way.
- Then in this order, guests will approach the snacks one by one.
- Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
输入格式
The first line contains integers $n$ and $k$ ( $2 \le n \le 10^5$ , $1 \le k \le 10^5$ ), the number of snacks and the number of guests.
The $i$ -th of the following $k$ lines contains two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ , $x_i \ne y_i$ ), favorite snack flavors of the $i$ -th guest.
The $i$ -th of the following $k$ lines contains two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ , $x_i \ne y_i$ ), favorite snack flavors of the $i$ -th guest.
输出格式
Output one integer, the smallest possible number of sad guests.
输入输出样例
输入 #1
5 4 1 2 4 3 1 4 3 4
输出 #1
1
输入 #2
6 5 2 3 2 1 3 4 6 5 4 5
输出 #2
0
In the first example, Bessie can order the guests like this: $3, 1, 2, 4$ . Guest $3$ goes first and eats snacks $1$ and $4$ . Then the guest $1$ goes and eats the snack $2$ only, because the snack $1$ has already been eaten. Similarly, the guest $2$ goes up and eats the snack $3$ only. All the snacks are gone, so the guest $4$ will be sad.
In the second example, one optimal ordering is $2, 1, 3, 5, 4$ . All the guests will be satisfied.
In the second example, one optimal ordering is $2, 1, 3, 5, 4$ . All the guests will be satisfied.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted