B - Neither shaken nor stirred (dfs)

The ACM ICPC regional contest in St. Petersburg is a stressful event even for veterans of competetive programming. That’s why for the last four years programmer Denchik and his coach Vova go to their favorite bars to relax after the event. Having entered a bar, Denchik immediately orders cocktail “B-52”. If there is no such cocktail on the menu he drinks nothing. On the other hand, in places, where the cocktail is good, Denchik can repeat his order several times.

Vova, as an elder friend, tries to control his trainee drunkenness degree. When entering and leaving bars, Vova asks Denchik how many cocktails he has drunk in the last bar where B-52 was served. If Denchik is not sure about the answer, Vova considers Denchik’s drinking enough for this day and takes him to the hotel.

This year the story repeats again. Denchik has the experience of four previous regional contests and knows which bars serve B-52 and how many cocktails he’s going to drink in each bar at one visit. He also knows where they can go after leaving every bar on their route. For which bars Denchik may prepare right answers to Vova’s questions in advance, no matter what route they choose?

Input

The first line contains an integer n which is the number of bars (1 ≤ n ≤ 100 000). Next n lines describe these bars. The i-th line contains integers k imin i1, n i2, …,  n imi (0 ≤ k i ≤ 100 000; 0 ≤ m i ≤ n). If k i equals zero, then in bar i B-52 is not served, and if k i is positive, it means that Denchik will drink k i cocktails at one visit to bar in i1, n i2, …, n imi are the numbers of the bars friends can go to right after leaving bar i (1 ≤ n ij ≤ nn ij < ni,j+1). There can be number i among the numbers n ij, and it means that after leaving bar i friends can hang around and enter the same bar again. The sum of all numbers mi does not exceed 100 000.

The bars are numbered in the order they are in the input data. Bar with number 1 is the bar from which Vova and Denchik begin their journey. It is guaranteed that during the night friends can reach every bar listed in the input.

Output

In the i-th of n lines output Denchik’s answers to Vova’s question on entering bari and leaving it. Every answer should have one of the following forms:

  • sober, if Denchik hasn’t drunk any B-52 yet
  • X, if during the last visit to the bar where B-52 was served Denchik drunk Xcocktails (X is an integer from 1 to 100 000)
  • unknown, if with different routes to the i-th bar different situations are possible

Example

inputoutput
5
0 2 2 3
6 1 4
5 2 4 5
5 1 5
0 0
sober sober
sober 6
sober 5
unknown 5
5 5
2
0 2 1 2
0 2 1 2
sober sober
sober sober

题目.....不想解释了,自己翻译吧,嘻嘻

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 50;
int a[maxn];
vector<int> ve[maxn];
int zt[maxn];
int n;
void dfs(int pos, int y)
{
    if(zt[pos] == -2) return ;
    if(!zt[pos]) zt[pos] = y;
    else zt[pos] = -2;
    if(a[pos]) y = a[pos];
    for(auto it : ve[pos])
    {
        if(zt[it] != y) dfs(it, y);
    }
}
int main()
{
    memset(zt, 0, sizeof(zt));
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        int x;
        cin >> x;
        for(int j=0; j<x; j++)
        {
            int y;
            cin >> y;
            ve[i].push_back(y);
        }
    }
    dfs(1, -1);
    for(int i=1; i<=n; i++)
    {
        if(zt[i] == -1) cout << "sober ";
        else if(zt[i] == -2) cout << "unknown ";
        else cout << zt[i] << ' ';
        if(a[i] > 0) cout << a[i] << endl;
        else
        {
            if(zt[i] == -1) cout << "sober" << endl;
            else if(zt[i] == -2) cout << "unknown" << endl;
            else cout << zt[i] << endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值