# 2.7日学习总结

50 篇文章 0 订阅

Problem Statement

You are given a simple undirected graph with �N vertices numbered 11 to �N and �M edges numbered 11 to �M. Edge �i connects vertex ��ui​ and vertex ��vi​.
Find the number of connected components in this graph.

Notes

simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.

subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

• 1≤�≤1001≤N≤100
• 0≤�≤�(�−1)20≤M≤2N(N−1)​
• 1≤��,��≤�1≤ui​,vi​≤N
• The given graph is simple.
• All values in the input are integers.

Input

The input is given from Standard Input in the following format:

�N �M
�1u1​ �1v1​
�2u2​ �2v2​
⋮⋮
��uM​ ��vM​


Output

Sample 1

InputcopyOutputcopy
5 3
1 2
1 3
4 5

2


The given graph contains the following two connected components:

• a subgraph formed from vertices 11, 22, 33, and edges 11, 22;
• a subgraph formed from vertices 44, 55, and edge 33.

Sample 2

InputcopyOutputcopy
5 0

5


Sample 3

InputcopyOutputcopy
4 6
1 2
1 3
1 4
2 3
2 4
3 4

#include<stdio.h>
int f[101]; int n,m;
void init()
{
for(int i=1;i<=n;i++)
f[i]=i;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}

void merge(int v,int u)
{
int t1=getf(u);
int t2=getf(v);
if(t1!=t2)
{
f[t2]=t1;

}

}
int main()
{

int g,h;
scanf("%d %d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
scanf("%d %d",&g,&h);
merge(g,h);
}
int ans=0;//我多花的二十分钟
for(int i=1;i<=n;i++)
{
if(f[i]==i)ans++;
}
printf("%d",ans);
return 0;

}

事实告诉我们，必须要重视小细节啊啊！！血泪史！！

Define the score of some binary string �T as the absolute difference between the number of zeroes and ones in it. (for example, �=T= 010001 contains 44 zeroes and 22 ones, so the score of �T is ∣4−2∣=2∣4−2∣=2).

Define the creepiness of some binary string �S as the maximum score among all of its prefixes (for example, the creepiness of �=S= 01001 is equal to 22 because the score of the prefix �[1…4]S[1…4] is 22 and the rest of the prefixes have a score of 22 or less).

Given two integers �a and �b, construct a binary string consisting of �a zeroes and �b ones with the minimum possible creepiness.

Input

The first line contains a single integer �t (1≤�≤1000)(1≤t≤1000)  — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers �a and �b (1≤�,�≤1001≤a,b≤100)  — the numbers of zeroes and ones correspondingly.

Output

For each test case, print a binary string consisting of �a zeroes and �b ones with the minimum possible creepiness. If there are multiple answers, print any of them.

Sample 1

InputcopyOutputcopy
5
1 1
1 2
5 2
4 5
3 7

10
011
0011000
101010101
0001111111


Note

In the first test case, the score of �[1…1]S[1…1] is 11, and the score of �[1…2]S[1…2] is 00.

In the second test case, the minimum possible creepiness is 11 and one of the other answers is 101.

In the third test case, the minimum possible creepiness is 33 and one of the other answers is 0001100.

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b;
scanf("%d %d",&a,&b);

int t=(a>b)?b:a;

for(int i=1;i<=t;i++)
{
printf("1");
b--;

printf("0");
a--;

}

if(a==0)
{
for(int i=1;i<=b;i++)
printf("1");
}

if(b==0)
{
for(int i=1;i<=a;i++)
printf("0");
}

printf("\n");

}
return 0;
}


10交替：

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b;
scanf("%d %d",&a,&b);

int t=(a>b)?b:a;

for(int i=1;i<=t;i++)
{
printf("0");
a--;

printf("1");
b--;

}

if(a==0)
{
for(int i=1;i<=b;i++)
printf("1");
}

if(b==0)
{
for(int i=1;i<=a;i++)
printf("0");
}

printf("\n");

}
return 0;
}


#include<stdio.h>
int main()
{
int g=1,m=0;
int n;
scanf("%d",&n);
while(n--)
{
int a,b;
scanf("%d %d",&a,&b);

int t=(a>b)?b:a;

for(int i=1;i<=t;i++)
{
printf("%d",m);
a--;

printf("%d",g);
b--;

}

if(a==0)
{
for(int i=1;i<=b;i++)
printf("%d",g);
}

if(b==0)
{
for(int i=1;i<=a;i++)
printf("%d",m);
}

printf("\n");

}
return 0;
}


看，时间还是有差距的，虽然是毫秒（上面是打印变量）；

Let's call a binary string �T of length �m indexed from 11 to �m paranoid if we can obtain a string of length 11 by performing the following two kinds of operations �−1m−1 times in any order :

• Select any substring of �T that is equal to 01, and then replace it with 1.
• Select any substring of �T that is equal to 10, and then replace it with 0.

For example, if �=T= 001, we can select the substring [�2�3][T2​T3​] and perform the first operation. So we obtain �=T= 01.

You are given a binary string �S of length �n indexed from 11 to �n. Find the number of pairs of integers (�,�)(l,r) 1≤�≤�≤�1≤l≤r≤n such that �[�…�]S[l…r] (the substring of �S from �l to �r) is a paranoid string.

Input

The first line contains an integer �t (1≤�≤10001≤t≤1000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer �n (1≤�≤2⋅1051≤n≤2⋅105) — the size of �S.

The second line of each test case contains a binary string �S of �n characters �1�2…��S1​S2​…Sn​. (��=Si​= 0 or ��=Si​= 1 for each 1≤�≤�1≤i≤n)

It is guaranteed that the sum of �n over all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, output the number of pairs of integers (�,�)(l,r) 1≤�≤�≤�1≤l≤r≤n such that �[�…�]S[l…r] (the substring of �S from �l to �r) is a paranoid string.

Sample 1

InputcopyOutputcopy
5
1
1
2
01
3
100
4
1001
5
11111
1
3
4
8
5


Note

In the first sample, �S already has length 11 and doesn't need any operations.

In the second sample, all substrings of �S are paranoid. For the entire string, it's enough to perform the first operation.

In the third sample, all substrings of �S are paranoid except [�2�3][S2​S3​], because we can't perform any operations on it, and [�1�2�3][S1​S2​S3​] (the entire string).

#include<stdio.h>
char a[200001];//刚开始狂输整数，一直出不来，才发现连在一起的要用字符TAT
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
long long ans=1;//别忘了每次用前初始化
scanf("%d",&n);//一定要记得先输入再使用,我之前经常先用再输入，时常出不来
scanf("%s",a+1);//如果没加一就是从a[0]开始
for(int i=1;i<n;i++)
{
if(a[i]!=a[i+1])ans+=i+1;
else ans+=1;
}
printf("%lld\n",ans);

}

return 0;//记得放在循环外
}

(16条消息) 复习题&&第五次测试E_m0_图灵灵的博客-CSDN博客

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
07-18
07-09

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

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