Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya.
For each opponent Arya knows his schedule — whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents.
Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents.
The first line of the input contains two integers n and d (1 ≤ n, d ≤ 100) — the number of opponents and the number of days, respectively.
The i-th of the following d lines contains a string of length n consisting of characters '0' and '1'. The j-th character of this string is '0' if the j-th opponent is going to be absent on the i-th day.
Print the only integer — the maximum number of consecutive days that Arya will beat all present opponents.
2 2 10 00
2
4 1 0100
1
4 5 1101 1111 0110 1011 1111
2
In the first and the second samples, Arya will beat all present opponents each of the d days.
In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and4.
题目大意:有d天,每天有n次挑战,如果有一次挑战中有结果0,就算今天胜利了,问最大连续胜利天数为多少。
思路:简单模拟即可。
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char a[105][105];
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
int ans=0;
int output=0;
for(int i=0;i<n;i++)
{
int flag=1;
for(int j=0;j<m;j++)
{
if(a[i][j]=='0')
{
flag=0;
output++;
break;
}
}
if(flag==0)
{
ans=max(ans,output);
}
else output=0;
}
printf("%d\n",ans);
}
}
思路:枚举找规律,发现第n个这样的数就是其本身后边加一个反着的本身,比如第10个数就是1001,第15个数就是1551.
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
char a[10000000];
int main()
{
while(~scanf("%s",a))
{
printf("%s",a);
int n=strlen(a);
for(int i=n-1;i>=0;i--)
{
printf("%c",a[i]);
}
printf("\n");
}
}
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.
Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).
Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.
They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A andB, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.
Each of the next m lines contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).
If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.
4 2 1 2 2 3
1 2 2 1 3
3 3 1 2 2 3 1 3
-1
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).
In the second sample, there is no way to satisfy both Pari and Arya.
题目大意:
给你n个点,m条边,将其分成两个集合,集合A是图的一个点覆盖,集合B也是图的一个点覆盖,要求集合A和集合B没有交集,如果有这样的两个集合,在spj的情况下输出合理解,如果没有这样的分配,输出-1.
思路:
1、首先我们知道,如果图是一个二分图,那么其必然有点覆盖集,那么点覆盖集的补集,也一定是一个点覆盖集。那么问题就转化到二分图的判定上来。
2、二分图的判定直接Bfs/Dfs,二分染色,如果遇到矛盾,输出-1,如果没遇到矛盾,那么一直染色下去,注意这个图是一个森林图,所以我们要将所有点都进行染色分配集合。
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int color[100030];
vector<int >mp[100030];
int n,m;
int Bfs(int ss)
{
color[ss]=1;
queue<int >s;
s.push(ss);
while(!s.empty())
{
int u=s.front();
s.pop();
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(u==v)continue;
if(color[v]==-1)
{
color[v]=3-color[u];
s.push(v);
}
else
{
if(color[v]==color[u])return 0;
}
}
}
return 1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(color,-1,sizeof(color));
for(int i=1;i<=n;i++)mp[i].clear();
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
int tt=1;
for(int i=1;i<=n;i++)
{
if(color[i]==-1)
{
int ttt=Bfs(i);
if(ttt==0)
{
tt=0;break;
}
}
}
if(tt==0)
{
printf("-1\n");
}
else
{
int tot=0;
for(int i=1;i<=n;i++)
{
if(color[i]==1)tot++;
}
printf("%d\n",tot);
int f=0;
for(int i=1;i<=n;i++)
{
if(color[i]==1)
{
if(f==0)printf("%d",i),f=1;
else printf(" %d",i);
}
}
printf("\n");
tot=0;
for(int i=1;i<=n;i++)
{
if(color[i]==2)tot++;
}
printf("%d\n",tot);
f=0;
for(int i=1;i<=n;i++)
{
if(color[i]==2)
{
if(f==0)printf("%d",i),f=1;
else printf(" %d",i);
}
}
printf("\n");
}
}
}