D. Cow and Snacks
D. Cow and Snacks 传送门
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,…,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.
Input
The first line contains integers n and k (2≤n≤1e5, 1≤k≤1e5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers xi and yi (1≤xi,yi≤n, xi≠yi), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4 1 2 4 3 1 4 3 4
Output
1
Input
6 5 2 3 2 1 3 4 6 5 4 5
Output
0
Note
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.
思路: 根据每个客人爱吃的零食的种类编号,构建一条边。每条边的端点是每种口味零食的编号。我们把图建好之后, 枚举每个客人爱吃的零食的口味编号, 以两种口味为起点进行dfs每遍历到一个端点(没有被标记过的)就会满足一个客人。
时间复杂度O(n+k).
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mset(a, n) memset(a, n, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<int,PII> PPI;
typedef pair<ll, ll> PLL;
const int maxn = 1e5+5;
const ll Mod = 998244353;
int n,k;
bool vis[maxn];
int ans;
int u[maxn], v[maxn];
vector<vector<int> > G;
void dfs(int s){
int len = G[s].size();
for(int i=0;i<len;i++){
int to = G[s][i];
if(vis[to]) continue;
vis[to] = true; ans -= 1;
dfs(to);
}
}
int main(){
scanf("%d%d", &n, &k);
G.clear();
G.resize(n+1);
for(int i=1;i<=k;i++){
scanf("%d%d", &u[i], &v[i]);
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
}
ans = k;
mset(vis, false);
for(int i=1;i<=k;i++){
if(!vis[u[i]]){
vis[u[i]] = true; vis[v[i]] = true; ans -= 1;
dfs(u[i]);
dfs(v[i]);
}
}
printf("%dn", ans);
return 0;
}
相关知识
殷花
大多数温室花卉()进入休眠期。A.春季B.夏季C.秋季D.冬...
夏季常见花卉有( )。 A.菊花 B.荷花 C.百合 D.海棠
A.鸢尾科B.石蒜科C.美人蕉科D.虎耳草科中国水仙属于()
角磨机改装电链锯最详细的安装使用教程,喜欢的可以收藏下!#教学 #角磨机电链锯 #角磨机改装
现存两栖动物大多生活在( ) A.热带 B.亚热带 C.温带 D.寒带
A.母爱B.高洁C.百年好合D.热恋,真心实意百合花的花语是()。
以干燥花入药的是( )A.丁香B.菊花C.辛夷D.红花...
开关:灯A.钥匙:锁B.插头:电脑C.阳光:草地D.线:风筝
白天:黑夜A.男人:女人B.高山:大海C.白色:黑色D.老人:小孩
网址: D. Cow and Snacks https://www.huajiangbk.com/newsview561924.html
上一篇: 花店物语破解版下载 |
下一篇: 花卉库存如何管理 |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039