简略题意:
有n张牌 每张牌2面, 你每张牌只能选一个面, 把这n张牌排序后从 最大能从1开始连续到多少会断掉?
想法很棒棒啊。
首先把牌面看做一条边,考虑一棵n大小的树的情况,那么可以使得其中n-1个点被选出来。如果这不是一个树,那么这个图里的任意节点都可以满足。
为了让答案更优,对于每个联通块,我们必然选择每个联通块序号最小的n-1个点。
用并查集来维护,每次启发式合并两个联通块。
#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
#define eps 1e-8
const double pi = acos(-1.0);
typedef long long LL;
typedef unsigned long long ULL;
void umax(int &a, int b) {
a = max(a, b);
}
void umin(int &a, int b) {
a = min(a, b);
}
int dcmp(double x) {
return fabs(x) <= eps?0:(x > 0?1:-1);
}
void file() {
freopen("data_in.txt", "r", stdin);
freopen("data_out.txt", "w", stdout);
}
namespace solver {
const int maxn = 1100000;
int n;
int vis[maxn], col[maxn];
int sz[maxn];
int fa[maxn];
vector<int> G[1100000];
void init() {
memset(vis, 0, sizeof vis);
for(int i = 0; i < maxn; i++)
fa[i] = i, G[i].push_back(i);
}
int find(int x) {
return x == fa[x]?fa[x] : fa[x] = find(fa[x]);
}
void solve() {
init();
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
int f1 = find(x), f2 = find(y);
if(f1 == f2) {
vis[f1] = 1;
} else {
if(G[f1].size() > G[f2].size())
swap(f1, f2);
for(auto it:G[f1])
G[f2].push_back(it);
fa[f1] = f2;
}
}
for(int i = 1; i <= 10000; i++) {
if(i == find(i)) {
sort(all(G[i]));
if(vis[i])
vis[*G[i].rbegin()] = 1;
for(int j = 0; j < G[i].size() - 1; j++)
vis[G[i][j]] = 1;
}
}
int i = 0;
for(i = 1; i <= 10001; i++) {
if(!vis[i]) break;
}
cout<<i-1;
}
}
/*
2 0 2
1 2
3 1
*/
int main() {
// file();
solver::solve();
return 0;
}