题目传送门
解题思路:
二分图的最大匹配,武器和攻击相匹配。
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int n,head[1000001],tot,ans,g[1000001]; 8 bool vis[1000001]; 9 struct kkk{ 10 int to,next; 11 }e[2000001]; 12 13 inline void add(int x,int y) { 14 e[++tot].to = y; 15 e[tot].next = head[x]; 16 head[x] = tot; 17 } 18 19 inline bool find(int x) { 20 for(int i = head[x];i; i = e[i].next) { 21 int u = e[i].to; 22 if(vis[u]) continue; 23 vis[u] = 1; 24 if(g[u] == 0 || find(g[u])) { 25 g[u] = x; 26 return true; 27 } 28 } 29 return false; 30 } 31 32 int main() { 33 scanf("%d",&n); 34 for(int i = 1;i <= n; i++) { 35 int x,y; 36 scanf("%d%d",&x,&y); 37 if(x <= n) add(x,i); 38 if(y <= n) add(y,i); 39 } 40 for(int i = 1;i <= n + 1; i++) { 41 memset(vis,0,sizeof(vis)); 42 if(!find(i)) { 43 printf("%d",i - 1); 44 return 0; 45 } 46 } 47 return 0; 48 }