给定m天,每天n个人可以中奖,当前中奖的人不能参加后面的比赛,输出可能的方案
题目让我输出一个 没有冲突的方案,如果一个人在多天都能得奖,那么我们任选其中一天让他得奖,这种人可能存在多个,我们把同一天的这类人看成一组,这一天我们选择了这组中的其中一个人相当于选择了这个组,在之后就不能再选择属于这组中的人了
然后题目等价与在每天的那个组中选一个人,总的集合 向 当前天的选择的那个人(总的集合没有的人) 连边,然后这一天的这一组也属于 总的集合
组1 ---组2 -- 组3 ----...
== 组12 -- 组3 ---...
最后 每天 的 单独的一个集合 合并为一个大的集合 ,就说明有解,如果其中一个集合无法合并就说明无解
要保证前面选择的 不会和 后面的有冲突,我们从后面开始选,选完后把这组人全部标记
#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#define x first
#define y second
#define pb emplace_back
#define fu(i,a,b) for(int i=a;i<=b; ++ i)
#define fd(i,a,b) for(int i=a;i>=b; -- i)
#define endl '\n'
#define ms(x,y) memset(x,y,sizeof x)
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<vector<LL>> VVL;
typedef vector<vector<int>> VVI;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef pair<PII,int> PIII;
typedef pair<double,double> PDD;
typedef pair<double,int> PDI;
typedef pair<char,int> PCI;
typedef pair<string,int> PSI;
typedef pair<int,string> PIS;
typedef pair<int,char> PIC;
typedef pair<LL,LL> PLL;
typedef __int128 i128;
typedef unsigned long long ULL;
const int N =20+ 10,M = N * 2,base =400 ,INF = 0x3f3f3f3f,P = 131;
const double eps = 1e-8;
const int mod = 998244353;
const LL LNF=(LL) INF * INF;
inline void solve()
{
int m;cin >> m;
VVI a(m);
fu(i,0,m-1)
{
int n;cin >> n ;
a[i].resize(n);
fu(j,0,n-1) cin >> a[i][j];
}
set<int> s;
VI ans(m);
fd(i,m-1,0) // 从后往前找 ,要保证每天得奖的人不同,就把后面开始每天可能得奖的人标记
{
int x = -1;
for(auto t : a[i])
if(!s.count(t))// 任选一个人在后面 还没有得奖的人
{
x = t;s.insert(t);
}
if(x == -1) // 这天找不到人得奖
{
cout << -1 << endl;return;
}
ans[i] = x;
}
fu(i,0,m-1)
cout << ans[i] << " \n"[i == m- 1];
}
signed main()
{
// freopen("1.txt","r",stdin);
// #define int long long
ios
// cout << fixed<<setprecision(2);
int t=1;
cin>>t;
int now = 1;
while(t -- )
{
// cout<<"Case ";
// cout<<"Case #";
// cout<<"Scenario #";
// cout<< now ++ <<": ";
// cout<< now ++ <<": \n";
solve();
}
return 0;
}