无法匹配的时候break就好了。
/* Pigonometry */
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int n, m, head[maxn], cnt, from[maxn], vis[maxn], clo;
struct _edge {
int v, next;
} g[maxn << 1];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void add(int u, int v) {
g[cnt] = (_edge){v, head[u]};
head[u] = cnt++;
}
inline bool hungary(int x) {
for(int i = head[x]; ~i; i = g[i].next) if(vis[g[i].v] != clo) {
vis[g[i].v] = clo;
if(!from[g[i].v] || hungary(from[g[i].v])) {
from[g[i].v] = x;
return 1;
}
}
return 0;
}
int main() {
n = iread(); m = iread();
for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;
for(int i = 1; i <= m; i++) {
int a = iread(), b = iread(); a++; b++;
add(i, a);
if(a ^ b) add(i, b);
}
int ans = 0;
for(int i = 1; i <= m; i++) {
clo++;
if(hungary(i)) ans++;
else break;
}
printf("%d\n", ans);
return 0;
}