题目链接:https://www.luogu.org/problemnew/show/P1640
数据有点水吧,从属性值连向对应武器编号。
枚举属性值匹配,遇到第一个无法匹配的直接跳出就好惹~。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
inline int read()
{
int ret=0;
char c=getchar();
while (c<'0' || c>'9') c=getchar();
while (c>='0' && c<='9'){
ret=((ret<<3)+(ret<<1))+c-'0';
c=getchar();
}
return ret;
}
const int maxn = 500000+1;
int n, ans, cnt;
int link[maxn], head[maxn];
bool vis[maxn];
struct edge{
int v,next;
}e[maxn<<2];
void add(int u, int v)
{
e[++cnt].v = v;
e[cnt].next = head[u];
head[u] = cnt;
}
bool dfs(int u)
{
int v = head[u];
for(int i = v; i != 0; i = e[i].next)
{
if(!vis[e[i].v])
{
vis[e[i].v] = 1;
if(!link[e[i].v] || dfs(link[e[i].v]))
{
link[e[i].v] = u;
return 1;
}
}
}
return 0;
}
int main()
{
int u,v;
n = read();
for(int i = 1; i <= n; i++)
{
u = read(); v = read();
add(u,i);
add(v,i);
}
for(int i = 1; i <= n; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
else break;
}
printf("%d\n",ans);
return 0;
}