Codeforces Div.2 1798B Three Sevens题解

题目:

传送门

B. Three Sevens

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Lottery "Three Sevens" was held for m days. On day i, ni people with the numbers ai,1,…,ai,ni,participated in the lottery.

It is known that in each of the m days, only one winner was selected from the lottery participants. The lottery winner on day i was not allowed to participate in the lottery in the days from i+1 to m.

Unfortunately, the information about the lottery winners has been lost. You need to find any possible list of lottery winners on days from 11 to m or determine that no solution exists.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤50000). The description of the test cases follows.

The first line of each test case contains a single integer m (1≤m≤50000) — the number of days in which the lottery was held.

Next, for each i from 11 to m, follows a two-line block of data.

The first line of each block contains a single integer ni (1≤ni≤50000) — the number of lottery participants on day i.

The second line of the block contains integers ai,1,…,ai,ni (1≤ai,j≤50000) — lottery participants on day i. It is guaranteed that all the numbers ai,1,…,ai,ni are pairwise distinct.

It is guaranteed that the sum of ni over all blocks of all test cases does not exceed 5000050000.

Output

For each test case, if there is no solution, print a single integer −1−1.

Otherwise, print mintegers p1,p2,…,pm (1≤pi≤50000) — lottery winners on days from 11 to m. If there are multiple solutions, print any of them.

Sample 1

InputcopyOutputcopy
 

3

3

4

1 2 4 8

3

2 9 1

2

1 4

2

2

1 2

2

2 1

4

4

1 2 3 4

1

1

1

4

1

3

8 2 1 
-1
2 1 4 3 

 Note

In the first test case, one of the answers is [8,2,1][8,2,1] since the participant with the number 88 participated on day 11, but did not participate on days 22 and 33; the participant with the number 22 participated on day 22, but did not participate on day 33; and the participant with the number 11 participated on day 33. Note that this is not the only possible answer, for example, [8,9,4][8,9,4] is also a correct answer.

In the second test case, both lottery participants participated on both days, so any possible lottery winner on the day 11 must have participated on the day 22, which is not allowed. Thus, there is no correct answer.

In the third test case, only one participant participated on days 22, 33, 44, and on day 11 there is only one participant who did not participate in the lottery on days 2,3,42,3,4 — participant 22, which means [2,1,4,3][2,1,4,3] is the only correct answer to this test case.


分析:

就是给几个数组,分别对应天数,如果一个数在第一天出现了,第二天没出现,可能就是这小子中奖了,然后答案就是输出可能的中奖名单。


code

#include<bits/stdc++.h>
using namespace std;
int m, n;
set<int> st[50005];
int ans[50005];

void sol() 
{
    cin >> m;
    for (int i = 1; i <= m; i++) 
    {
        st[i].clear();
        cin >> n;
        for (int j = 1; j <= n; j++) 
        {
            int x;
            cin >> x;
            st[i].insert(x);
        }
    }

    set<int> tot;
    for (int i = m; i >= 1; i--) 
    {
        bool flag = false;
        for (int x : st[i]) 
        {
            if (tot.find(x) == tot.end()) 
            {
                flag = true;
                ans[i] = x;
            }
            tot.insert(x);
        }
        if (!flag) 
        {
            printf("-1\n");
            return;
        }
    }

    for (int i = 1; i <= m; i++) 
    {
        printf("%d ", ans[i]);
    }
    printf("\n");
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        sol();
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值