原文链接:
https://blog.csdn.net/Encore47/article/details/115127800
大佬的AC代码(供自己学习一下码风)
#include<stdio.h>
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<deque>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<stack>
#define VI vector<int>
#define eps 1e-6
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long LL;
typedef double db;
const int inf = 0x3f3f3f3f;
const LL INF = 1e18;
const int N = 1e5 + 10;
#define ls rt << 1
#define rs rt << 1 | 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
inline int readint() {int x; scanf("%d", &x); return x;}
int cnt[N], res[N];
set<int> day[N];
int n, m;
void init() {
for(int i = 1; i <= m; ++i) {
day[i].clear();
}
memset(cnt, 0, sizeof(cnt));
memset(res, 0, sizeof(res));
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t = readint();
while (t--) {
init();
n = readint(), m = readint();
for(int i = 1; i <= m; ++i) {
int k = readint();
while (k--) {
int p = readint();
day[i].insert(p);
cnt[p]++;
}
}
int ma = 0, lim = (m + 1) / 2;
for(int i = 1; i <= n; ++i) {
if (cnt[i] > lim) {
ma = i; break;
}
}
if (ma == 0) {
printf("YES\n");
for(int i = 1; i <= m; ++i) {
auto it = day[i].begin();
printf("%d%s", *it, i == m ? "\n" : " ");
}
continue;
}
int num = 0;
for(int i = 1; i <= m; ++i) {
if (res[i]) continue;
if (day[i].size() == 1 && day[i].find(ma) != day[i].end()) {
res[i] = ma;
num++;
}
}
if (num > lim) {
printf("NO\n"); continue;
}
for(int i = 1; i <= m; ++i) {
if (res[i]) continue;
if (day[i].find(ma) != day[i].end() && num < lim) {
res[i] = ma;
num++;
continue;
}
for(auto it = day[i].begin(); it != day[i].end(); ++it) {
int now = *it;
if (now != ma) {
res[i] = now;
break;
}
}
}
printf("YES\n");
for(int i = 1; i <= m; ++i) {
printf("%d%s", res[i], i == m ? "\n" : " ");
}
}
return 0;
}
我的代码
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, l, r) for (int i = l; i >= r; --i)
#define mset(s, _) memset(s, _, sizeof(s))
#define mcpy(s, _) memcpy(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define vi vector<int>
#define vpii vector<pii>
#define mp(a, b) make_pair(a, b)
#define debug system("pause")
#define pll pair <ll, ll>
#define fir first
#define sec second
#define inf 0x3f3f3f3f
#define pline cout << endl << endl
inline int lowbit(int x) {return x & -x;}
template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
inline int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> inline void print(T x, char let) {
print(x), putchar(let);
}
const int N = 2e5 + 10, mod = 100019;
int t, n, m;
int cnt[N], st[N], ans[N];
set<int> day[N];
void init() {
rep(i, 0, m) day[i].clear();
mset(cnt, 0); mset(st, 0); mset(ans, 0);
}
int main() {
cin >> t;
while(t -- ) {
init();
cin >> n >> m;
rep(d, 1, m) {
int num; cin >> num;
int p;
rep(j, 1, num) {
cin >> p; day[d].insert(p); cnt[p] ++ ;
}
}
int rnd = (m + 1) >> 1, mk = 0;
rep(i, 1, n) {
if(cnt[i] > rnd) {
mk = i; break;
}
}
if(!mk) {
puts("YES");
rep(i, 1, m) {
auto it = day[i].begin(); cout << *it << ' ';
}
cout << endl;
continue;
}
int num = 0;
rep(i, 1, m) {
if(day[i].size() == 1 && day[i].find(mk) != day[i].end()) {
st[i] = 1; num ++ ; ans[i] = mk;
}
}
if(num > rnd) {
puts("NO"); continue;
}
rep(i, 1, m) {
if(!st[i] && day[i].find(mk) != day[i].end() && num < rnd) {
st[i] = 1; ans[i] = mk; num ++ ;
continue;
}
if(!st[i]) {
for(auto j : day[i]) {
if(j != mk) {
ans[i] = j; break;
}
}
}
}
puts("YES");
rep(i, 1, m) cout << ans[i] << ' ';
cout << endl;
}
return 0;
}