思路:二分图匹配

做法:先把边用邻接表存(链式前向星也可)(这里注意把是把装备和它的两个属性相连),然后套用二分图模板即可

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);1i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=10010,M=1000010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;

vector<int> g[N]; //图的邻接表 
int match[M]; //所匹配的顶点
bool st[M]; //(临时)预定标记 
int n;

bool dfs(int v){
    for(int i=0;i<g[v].size();i++){
        int u=g[v][i],w=match[u];
        if(!st[u]){
            st[u]=1;
            if(!w||dfs(w)){
//                match[v]=u;
                match[u]=v;
                return true;
            }
        }
    }
    return false;
}

int bipartite_matching(){
    for(int v=1;v<=10001;v++){
        mst(st,0);
        if(!dfs(v)) return v-1;
    }
}

void solve(){
    cin>>n;
    rep(i,1,n){
        int u,v;
        cin>>u>>v;
        g[u].push_back(i);
        g[v].push_back(i);
    }
    cout<<bipartite_matching()<<'\n';
}


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
#ifdef DEBUG
    freopen("F:/laji/1.in", "r", stdin);
//    freopen("F:/laji/2.out", "w", stdout);
#endif
//    int t;cin>>t;while(t--)
    solve(); 
    return 0;
}