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.
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.
If it's possible to turn on all m bulbs print "YES", otherwise print "NO".
3 4
2 1 4
3 1 3 1
1 2
YES
3 3
1 1
1 2
1 1
NO
1 #include <bits/stdc++.h> 2 3 int main() 4 { 5 int i,j,k; 6 int n,m,cnt=0; 7 int vis[105]={0}; 8 scanf("%d %d",&n,&m); 9 for(i=1;i<=n;i++) 10 { 11 int x,y; 12 scanf("%d",&x); 13 for(j=1;j<=x;j++) 14 { 15 scanf("%d",&y); 16 if(vis[y]==0) 17 { 18 vis[y]=1; 19 cnt++; 20 } 21 } 22 } 23 if(cnt==m) 24 printf("YES\n"); 25 else 26 printf("NO\n"); 27 }
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:
- Only segments already presented on the picture can be painted;
- The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;
- The numbers of points from the beginning of the tail to the end should strictly increase.
Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.
Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
First line of the input contains two integers n and m(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively.
Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
Print the maximum possible value of the hedgehog's beauty.
8 6
4 5
3 5
2 5
1 2
2 8
6 7
9
4 6
1 2
1 3
1 4
2 3
2 4
3 4
12
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int vis[100005],num[100005]; 5 vector <int> edg[100005]; 6 int n,m; 7 8 int main() 9 { 10 int i,j; 11 int x,y; 12 int u,v; 13 scanf("%d %d",&n,&m); 14 fill(vis,vis+(n+2),1); 15 fill(num,num+(n+2),0); 16 for(i=1;i<=m;i++) 17 { 18 scanf("%d %d",&x,&y); 19 if(y<x) 20 edg[x].push_back(y); 21 else 22 edg[y].push_back(x); 23 num[x]++,num[y]++; 24 } 25 for(i=1;i<=n;i++) 26 { 27 int u=i; 28 for(j=0;j<edg[u].size();j++) 29 { 30 v=edg[u][j]; 31 if(vis[v]+1>vis[u]) 32 { 33 vis[u]=vis[v]+1; 34 } 35 } 36 } 37 long long ans=0; 38 for(i=1;i<=n;i++) 39 { 40 if(1ll*vis[i]*num[i]>ans) 41 ans=1ll*vis[i]*num[i]; 42 } 43 printf("%I64d\n",ans); 44 }
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers xi and yi — numbers of ending blocks in the corresponding piece. If xi ≤ yi then this piece is used in the regular order, and if xi > yi piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
abc
cbaabc
2
3 1
1 3
aaabrytaaa
ayrat
3
1 1
6 5
8 7
ami
no
-1
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 char a[2108],b[2108]; 7 int i,j; 8 int ans=0,ansx[2105],ansy[2105]; 9 scanf("%s",b); 10 scanf("%s",a); 11 int la=strlen(a),lb=strlen(b); 12 bool flag=true; 13 for(i=0;i<la;i++) 14 { 15 int mxnum=0,mxj,flg; 16 for(j=0;j<lb;j++) 17 { 18 if(a[i]==b[j]) 19 { 20 int num=1; 21 while(a[i+num]==b[j+num] && i+num<la && j+num<lb) 22 { 23 num++; 24 } 25 if(num>mxnum) 26 mxnum=num,mxj=j,flg=1; 27 num=1; 28 while(a[i+num]==b[j-num] && i+num>=0 && j-num>=0) 29 { 30 num++; 31 } 32 if(num>mxnum) 33 mxnum=num,mxj=j,flg=2; 34 } 35 } 36 if(mxnum==0) 37 { 38 flag=false; 39 break; 40 } 41 else 42 { 43 ans++; 44 if(flg==1) 45 { 46 ansx[ans]=mxj,ansy[ans]=mxj+mxnum-1; 47 } 48 else 49 { 50 ansx[ans]=mxj,ansy[ans]=mxj-mxnum+1; 51 } 52 i=i+mxnum-1; 53 } 54 55 } 56 if(flag) 57 { 58 printf("%d\n",ans); 59 for(i=1;i<=ans;i++) 60 { 61 printf("%d %d\n",ansx[i]+1,ansy[i]+1); 62 } 63 } 64 else 65 { 66 printf("-1\n"); 67 } 68 }