【SCOI2010】连续攻击游戏

题面

https://www.luogu.org/problem/P1640

题解

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define ri register int
#define N 10050
#define M 1000500

using namespace std;

int read() {
  int ret=0,f=0; char ch=getchar();
  while (ch<'0' || ch>'9') f|=(ch=='-'),ch=getchar();
  while (ch>='0' && ch<='9') ret=ret*10+(ch-'0'),ch=getchar();
  return f?-ret:ret;
}

int n,tim;
int vis[M],match[M];
vector<int> to[N];

bool dfs(int x) {
  for (ri i=0;i<to[x].size();i++) {
    int y=to[x][i];
    if (vis[y]==tim) continue;
    vis[y]=tim;
    if (!match[y] || dfs(match[y])) {
      match[y]=x;
      return 1;
    }
  }
  return 0;
}

int main(){
  n=read();
  for (ri i=1;i<=n;i++) {
    int a=read(),b=read();
    to[a].push_back(i); to[b].push_back(i);
  }
  tim=0;
  for (ri i=1;i<=10001;i++) {
    ++tim;
    if (!dfs(i)) {
      printf("%d
",i-1);
      return 0;
    }
  }
}
原文地址:https://www.cnblogs.com/shxnb666/p/11278610.html