【CF #435】A B C

来源:互联网 发布:售后服务网络体系 编辑:程序博客网 时间:2024/05/16 16:18

A.
Mahmoud and Ehab and the MEX
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.

Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn’t exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .

Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?

Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.

The second line contains n distinct non-negative integers not exceeding 100 that represent the set.

Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.

Examples
input
5 3
0 4 5 6 7
output
2
input
1 0
0
output
1
input
5 0
1 2 3 4 5
output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.

For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.

In the third test case the set is already evil.
水题

#include<bits/stdc++.h>using namespace std;#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 1000+10;const int MAXM = 100000+100;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int a[MAXN];int main(){    CLOSE();//  fread();//  fwrite();    int n,x;scanf("%d%d",&n,&x);    int maxx=-1;    for(int i=1;i<=n;i++) {        int val;scanf("%d",&val);        a[val]=1;        maxx=max(maxx,val);    }    int cnt=0;    if(a[x]) cnt++;    for(int i=0;i<x;i++) if(!a[i]) cnt++;    printf("%d\n",cnt);    return 0;}

B
. Mahmoud and Ehab and the bipartiteness
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.

A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.

Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn’t contain loops or multiple edges). What is the maximum number of edges they can add?

A loop is an edge, which connects a node with itself. Graph doesn’t contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren’t the same .

Input
The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105).

The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ n, u ≠ v) — the description of the edges of the tree.

It’s guaranteed that the given graph is a tree.

Output
Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

Examples
input
3
1 2
1 3
output
0
input
5
1 2
2 3
3 4
4 5
output
2
Note
Tree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)

Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graph

In the first test case the only edge that can be added in such a way, that graph won’t contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.

In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
我们只要找到这个树中两个集合的个数,就行了。
相连的两个点肯定不是一个集合的。
我们可以用DFS来染色,黑白两种颜色,分属两个集合。

#include<bits/stdc++.h>using namespace std;#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 1e5+10;const int MAXM = 100000+100;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;vector<int>ve[MAXN];int color[MAXN];void dfs(int now,int col){    color[now]=col;    for(int i=0;i<ve[now].size();i++){        int v=ve[now][i];        if(color[v]) continue;        dfs(v,-col);    }}int main(){    CLOSE();//  fread();//  fwrite();     LL n;scanf("%lld",&n);     for(int i=1;i<=n-1;i++) {        int a,b;        scanf("%d%d",&a,&b);        ve[a].push_back(b);        ve[b].push_back(a);     }     memset(color,0,sizeof(color));     dfs(1,1);     LL a,b;     a=b=0;     for(int i=1;i<=n;i++ ){        if(color[i]>0) a++;        else b++;     }     printf("%lld\n",a*b-(n-1));    return 0;}

C题的题解链接

原创粉丝点击