【cf1199E】E. Matching vs Independent Set(构造)

传送门

题意:
给出一张含有(3n)个点和(m)条边的图,现要从中找到一个(n)个点的独立集,或者(n)条边的匹配。
边的匹配定义为任意两条边不存在公共结点。

思路:
一条边会占用(2)个结点,如果能够找到(n)条边的匹配那么直接输出,否则就会剩下多于(n)个结点,将他们构成独立集即可。
核心思想在于"2+1=3"。
代码如下:

/*
 * Author:  heyuhhh
 * Created Time:  2020/3/15 21:59:25
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << '
'; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5, M = 5e5 + 5;

int n, m;
bool vis[N << 2];

void run() {
    cin >> n >> m;
    for(int i = 1; i <= 3 * n; i++) vis[i] = false;
    vector <int> edges;
    for(int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        if(!vis[u] && !vis[v]) {
            vis[u] = vis[v] = true;
            edges.push_back(i);   
        }
    }
    if(sz(edges) >= n) {
        edges.resize(n);
        cout << "Matching" << '
';
        for(auto it : edges) cout << it << ' ';
        cout << '
';   
    } else {
        cout << "IndSet" << '
';
        int cnt = n;
        for(int i = 1; i <= 3 * n; i++) if(!vis[i] && cnt) {
            --cnt; cout << i << ' ';
        }   
        cout << '
';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int T; cin >> T;
    while(T--) run();
    return 0;
}
原文地址:https://www.cnblogs.com/heyuhhh/p/12507072.html