问题 F: Investigating Imposters

题目描述

You have stumbled upon a village. In this village, some of the people are “imposters”, while the rest are not. Fortunately, you know that the number of possible imposters is limited!
You would like to determine who is not an imposter in this village. To do so, you ask each villager to submit a list of some villagers who are not imposters.
Non-imposters will only submit lists that contain other non-imposter names, while there is no such restriction for imposters. Imposters’ lists could contain imposters or non-imposters.
Given the lists of each villager, determine whether they could possibly be an imposter, or are definitely not an imposter.

输入

The first line of input contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 500), where n is the number of villagers and k is the maximum possible number of imposters. The villagers are
numbered from 1 to n.
Each of the next n lines describes a villager, where the ith line represents the list of villager i. The ith line starts with an integer s (0 ≤ s ≤ n), which is the number of people on villager i’s list of non-imposters. Then there will follow s distinct integers denoting the villagers on villager i’s list of non-imposters. It is possible for a villager to appear on their own list. All of the villagers on any given list will be distinct.

 

输出

Output n lines, each with a single integer. The ith line should contain 0 if the villager represented by the ith list in the input could possibly be an imposter, and 1 if that villager is definitely not an imposter.

样例输入 Copy

4 1
1 2
1 1
1 3
1 4

样例输出 Copy

1
1
0
0

 

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef double db;
const int N=250010;
int e[N],ne[N],h[N];
bool vis[510];
int n,k,cnt,idx;
void add(int x,int y)
{
    e[idx]=y;
    ne[idx]=h[x];
    h[x]=idx++;
}
void dfs(int x)
{
    vis[x]=true;
    cnt++;
    for(int i=h[x];~i; i=ne[i])
    {
        int j=e[i];
        if(vis[j])continue;
        dfs(j);
    }
}
signed main()
{
    memset(h,-1,sizeof h);
    cin>>n>>k;
    for(int i=1; i<=n; i++)
    {
        int s,x;
        cin>>s;
        for(int j=1; j<=s; j++)
        {
            cin>>x;
            add(x,i);
        }
    }
    for(int i=1; i<=n; i++)
    {
        memset(vis,0,sizeof vis);
        cnt=0;
        dfs(i);
        if(cnt>k)
        {
            cout<<1<<"\n";
        }
        else
        {
            cout<<0<<"\n";
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值