【 CodeForces 615A 】 Bulbs

来源:互联网 发布:阿里旅行软件 编辑:程序博客网 时间:2024/05/21 11:12

Bulbs

Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it’s connected to. Can Vasya light up all the bulbs?

If Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on.

Input
The first line of the input contains integers n and m (1 ≤ n, m ≤ 100) — the number of buttons and the number of bulbs respectively.

Each of the next n lines contains xi (0 ≤ xi ≤ m) — the number of bulbs that are turned on by the i-th button, and then xi numbers yij (1 ≤ yij ≤ m) — the numbers of these bulbs.

Output
If it’s possible to turn on all m bulbs print “YES”, otherwise print “NO”.

Sample Input
Input
3 4
2 1 4
3 1 3 1
1 2
Output
YES
Input
3 3
1 1
1 2
1 1
Output
NO

————————————————————————————————————————
思路有点绕,想通了简单。
刚开始绕进去,做了好一会。

开关开灯泡,一个开关控制数量不定的灯泡,判断一下按下列bu个开关能否把li个灯泡开启,开启YES,否则NO。

关键是在如何判断现在有的开关控制的灯泡是不是包括了所有的灯泡。
就是一个搜索比较,因为数组不大 ,两重for循环直接暴力求解。

代码如下:

————————————————————————————————————————

#include<cstdio>int main(){    int bu,li,i,a[105],b[105][105],c[10000],j,count,flag,k;    while( ~scanf("%d%d",&bu,&li) )    {        for( i=0; i<bu; i++)        {            scanf("%d",&a[i]);            for( j=0; j<a[i]; j++)            {                scanf("%d",&b[i][j]);            }        }   //根据题意先两个循环接受到二维素组b[]里面,在转到c[]里面        k=0;        for( i=0; i<bu; i++)          {            for( j=0; j<a[i]; j++)            {                c[k]=b[i][j];                k++;            }        }           count=1;        for( i=1; i<=li; i++ )  //把灯泡放在外面        {            flag=0;            for( j=0; j<k; j++)            {             //每一个灯泡和所有的开关比对,看看能否被控制。                if( i == c[j] )                {                    flag = 1; //任何一个开关能控制,输出 flag=1;                }          //否则为 0            }            if( flag == 0  )                {                    count=0;                    break;                }           }        if( count == 1)        {            printf("YES\n");        }        else        {            printf("NO\n");        }    }}
0 0
原创粉丝点击