You are given a simple undirected graph with �N vertices and �M edges. The vertices are numbered 11 to �N, and the �i-th edge connects vertex ��Ai and vertex ��Bi. Let us delete zero or more edges to remove cycles from the graph. Find the minimum number of edges that must be deleted for this purpose.
What is a simple undirected graph?
What is a cycle?
Constraints
- 1≤�≤2×1051≤N≤2×105
- 0≤�≤2×1050≤M≤2×105
- 1≤��,��≤�1≤Ai,Bi≤N
- The given graph is simple.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
�N �M �1A1 �1B1 �2A2 �2B2 ⋮⋮ ��AM ��BM
Output
Print the answer.
Sample 1
Inputcopy | Outputcopy |
---|---|
6 7 1 2 1 3 2 3 4 2 6 5 4 6 4 5 | 2 |
One way to remove cycles from the graph is to delete the two edges between vertex 11 and vertex 22 and between vertex 44 and vertex 55.
There is no way to remove cycles from the graph by deleting one or fewer edges, so you should print 22.
Sample 2
Inputcopy | Outputcopy |
---|---|
4 2 1 2 3 4 | 0 |
Sample 3
Inputcopy | Outputcopy |
---|---|
5 3 1 2 1 3 2 3 | 1 |
题目的大概意思就是要求将一个带有自循环的图拆成一个各边均不相连的一个图那么我们可以使用并查集和最小生成树的相关知识来解决这个问题,我们只需要将这题的每个点相连计算出其中所需要的节点数,然后再用m的值减去这个值就可以解决这个问题了
#include<stdio.h>
int f[200005];
int find(int x)
{
if (f[x] == x)
return x;
else {
f[x] = find(f[x]);
return f[x];
}
}
int merge(int x, int y)
{
int t1,t2,flag=0;
t1 = find(x);
t2 = find(y);
if (t1 != t2)
{
f[t2] = t1;
flag = 1;
}
return flag;
}
int main()
{
int n,m,count=0;
int x,y;
scanf("%d %d", &n,&m);
for (int i=1;i<=n;i++)
f[i] = i;
for (int i=1;i<=m;i++)
{
scanf("%d %d", &x,&y);
if (merge(x,y))
{
count++;
}
if (count==n-1)
break;
}
printf("%d", m-count);
return 0;
}
题目描述
设 �A 和 �B 是两个字符串。我们要用最少的字符操作次数,将字符串 �A 转换为字符串 �B。这里所说的字符操作共有三种:
- 删除一个字符;
- 插入一个字符;
- 将一个字符改为另一个字符。
�,�A,B 均只包含小写字母。
输入格式
第一行为字符串 �A;第二行为字符串 �B;字符串 �,�A,B 的长度均小于 20002000。
输出格式
只有一个正整数,为最少字符操作次数。
输入输出样例
输入 #1复制
sfdqxbw gfdgw
输出 #1复制
4
说明/提示
对于 100%100% 的数据,1≤∣�∣,∣�∣≤20001≤∣A∣,∣B∣≤2000。
写这个题的话我们就需要判断三种情况分别是删字符,改字符和添字符那我们只需要将其中的最下的值取出来然后赋值给dp【i】【j】就好了。然后我们需要知道i和j就是代表字符串a和字符串b的最小编辑距离
#include<bits/stdc++.h>
using namespace std;
char s1[2005],s2[2005];
int l1,l2,dp[2005][2005];
int main(){
scanf("%s%s",s1+1,s2+1);
l1=strlen(s1+1);
l2=strlen(s2+1);
for(int i=1;i<=l1;i++){
dp[i][0]=i;
}
for(int j=1;j<=l2;j++){
dp[0][j]=j;
}
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(s1[i]==s2[j]){
dp[i][j]=dp[i-1][j-1];
}
else
dp[i][j]=1+min(min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1]);
}
}
cout<<dp[l1][l2]<<endl;
return 0;
}