2019浙大计算机考研机试模拟赛(2)——概念专题

题目链接   引用自晴神OJ

  • A - 边覆盖
  • B - 极大独立集
  • C - 稳定婚姻问题
  • D - 笛卡尔树

没赶得上全程的比赛,就做了两道,后面两道以后有时间再补。两道都是概念题,比较基础~ 以下是题解


A - 边覆盖

Case Time Limit: 200 MS (Others) / 400 MS (Java)       Case Memory Limit: 256 MB (Others) / 512 MB (Java)

Accepted: 199      Total Submission: 362

Problem Description

对一个给定的无向图G(V,E),边集E‘是E的子集。如果V中的所有顶点都在E‘中出现过,那么称边集E‘是图G的一个边覆盖(Edge Cover)。
(以上定义引自https://en.wikipedia.org/wiki/Edge_cover

根据上面的定义,请判断一些给定的边集是否是给定的无向图的边覆盖。

Input

每个输入文件一组数据。

第一行为两个整数N、M(1<=N<=500, 1<=M<=N*(N-1)/2),分别表示无向图的顶点数和边数。假设图上的顶点编号为从1到N。

接下来M行,每行两个正整数u、v(1<=u,v<=N, u!=v),分别表示一条无向边的两个端点。数据保证没有重边。

接着一个正整数K(K<=10),表示查询的个数。

然后是K个查询,每个查询第一行为一个正整数L(L<=M),表示欲查询边集E‘中的边数;接下来L行,每行两个整数,表示边集E‘中的一条边。数据保证E‘一定是E的子集。

Output

每个查询一行,如果欲查询边集E‘不是图G的边覆盖,那么输出No;否则输出Yes

Sample Input

6 7
1 2
1 3
2 3
2 4
3 5
4 5
4 6
3
3
1 2
3 5
4 6
4
1 2
2 3
4 5
4 6
3
1 2
2 3
4 6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Output

Yes
Yes
No

Author

Shoutmon

Source

19浙大考研机试模拟赛


分析:题目是中文题,意思是输入一堆边,看这些边是否将所有顶点都覆盖到了。只需要在每次查询输入后,将边所连的顶点置为已访问,再遍历一次访问数组即可。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=510;
int G[maxn][maxn];
bool vis[maxn];
int main()
{
    //freopen("1.txt","r",stdin);
    int n,m;
    cin>>n>>m;
    int u,v;
    for(int i=0;i<m;i++){
        scanf("%d%d",&u,&v);
        G[u][v]=1;
        G[v][u]=1;
    }
    int k;
    cin>>k;
    while(k--){
        int L;
        scanf("%d",&L);
        memset(vis,0,sizeof(vis));
        for(int i=0;i<L;i++){
            scanf("%d%d",&u,&v);
            vis[u]=true;
            vis[v]=true;
        }
        int j;
        for(j=1;j<=n;j++){
            if(vis[j]==false){
                printf("No\n");
                break;
            }

        }
        if(j==n+1) printf("Yes\n");
    }
    return 0;
}


B - 极大独立集

Case Time Limit: 100 MS (Others) / 200 MS (Java)       Case Memory Limit: 256 MB (Others) / 512 MB (Java)

Accepted: 140      Total Submission: 303

Problem Description

对一个给定的无向图G(V,E),点集V‘是V的子集。如果V‘中的任意两个顶点之间都没有边,就称点集V‘是图G的独立集(Independent Set)。在此基础上,如果往V‘中添加任何一个在V中但不在V‘中的顶点,都会使V‘变成非独立集,那么就称V‘是图G的极大独立集(Maximal Independent Set)。
(以上定义引自https://en.wikipedia.org/wiki/Independent_set_(graph_theory)

根据上面的定义,请判断一些给定的点集是否是给定的无向图的极大独立集。

Input

每个输入文件一组数据。

第一行为两个整数N、M(1<=N<=500, 1<=M<=N*(N-1)/2),分别表示无向图的顶点数和边数。假设图上的顶点编号为从1到N。

接下来M行,每行两个正整数u、v(1<=u,v<=N, u!=v),分别表示一条无向边的两个端点。数据保证没有重边。

接着一个正整数K(K<=10),表示查询的个数。

然后是K个查询,每个查询第一行为一个正整数L(L<=N),表示欲查询点集V‘的顶点个数;第二行为用空格隔开的L个正整数,表示V‘中的顶点编号。数据保证V‘一定是V的子集。

Output

每个查询一行,如果欲查询的点集不是图G的独立集,那么输出Not an Independent Set;如果欲查询的点集是图G的独立集但不是极大独立集,那么输出Not Maximal;如果欲查询的点集是图G的极大独立集,输出Yes

Sample Input

6 5
1 2
2 3
2 4
4 5
4 6
3
2
1 4
3
1 3 4
3
1 2 4

Sample Output

Not Maximal
Yes
Not an Independent Set

Author

Shoutmon

Source

19浙大考研机试模拟赛


分析:判断是否是极大独立集,根据定义一个独立集是指任意两个顶点之间都没有边的点集,所谓最大就是加入任意一个顶点都会“破坏”独立集。先判断是否是独立集,然后再枚举每一个未在点集中的点,判断是否在加入后会“破坏”独立集。注意到样例中已经给出了坑点,即1和4仅是独立集不是最大独立集,因为加入3后仍然是一个独立集,知道这点以后就可以轻松解决了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=510;
int G[maxn][maxn]={0};
bool vis[maxn];
int main()
{
    //freopen("1.txt","r",stdin);
    int n,m;
    cin>>n>>m;
    int u,v;
    for(int i=0;i<m;i++){
        scanf("%d%d",&u,&v);
        G[u][v]=G[v][u]=1;
    }
    int K;
    scanf("%d",&K);
loop:   while(K--){
        int L;
        scanf("%d",&L);
        vector<int> vec;
        int temp;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<L;i++){
            scanf("%d",&temp);
            vec.push_back(temp);
            vis[temp]=true;
        }
        for(int i=0;i<vec.size();i++){
            for(int j=i+1;j<vec.size();j++){
                if(G[vec[i]][vec[j]]==1){
                    cout<<"Not an Independent Set"<<endl;
                    goto loop;
                }
            }
        }
        bool flag=false;
        for(int i=1;i<=n;i++){
            if(vis[i]==false){
                int j;
                for(j=0;j<vec.size();j++){
                    if(G[i][vec[j]]==1){
                        break;
                    }
                }
                if(j==vec.size()){
                    cout<<"Not Maximal"<<endl;
                    flag=true;
                    goto loop;
                }
            }
        }
        if(!flag) cout<<"Yes"<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Mered1th/p/10498757.html

时间: 2024-08-04 18:01:13

2019浙大计算机考研机试模拟赛(2)——概念专题的相关文章

2015年天勤考研机试模拟赛 A 判断三角形

[思路]:采用atoi转换长度,两边只和大于第三边,两边之差小于第三边. [AC代码]: #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; #define MAX 100+10 int main() { //freopen("in.txt", "r", stdin); //

计算机考研机试指南(六) ——栈

机试指南 cha 3 栈的应用 括号匹配问题 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <queue> 5 #include <stack> 6 #include <math.h> 7 #include <string> 8 #include <string.h> 9 #include <std

考研机试真题(一)之排序

转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 题目1202:排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:10071 解决:3549 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). 接下来的一行包括n个整数. 输出: 可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格. 每组测试数据的结果占一行. 样例输入: 4 1 4 3 2 样例输出: 1

西北工业大学2015年计算机学院考研机试 题目+标程(完整版)

A 求最小数(Output the minimum) 时限:1000ms 内存限制:10000K  总时限:3000ms 描述 每次给定3个数(均可用int表示),要求找出3个数里的最小的一个,并输出最小的数.Input three integers and output the minimum 输入 a b c 三个数用空格隔开Input three integers . 输出 a b c中最小的一个数Output the minimum . 输入样例 5 3 98 输出样例 3 1 #inc

NOIP 2012 提高组第二试模拟赛 Solution

第一题 题意 数据范围 Solution 三分求下凹函数最值 1 #include <cstdio> 2 #include <queue> 3 #include <iostream> 4 using namespace std; 5 inline void read(int &k) 6 { 7 k=0;int f=1;char c=getchar(); 8 while (c<'0'||c>'9')c=='-'&&(f=-1),c=ge

《考研机试》(二)机试题精讲

(一)题1: 解题思路:通过二维数组存取输入数据,之后通过一个函数判断是否存在'E''A''S''Y'四个字母,最后根据返回值打印difficult/easy 如何判断:传入二维数组的每一行(一行等于一个输入数据),定义2个指针,然后while循环读取 代码: #include <iostream> #include <iomanip> #include <math.h> #include <string.h> using namespace std; bo

《考研机试》(四)机试题精讲

1.题(一) 解析: 只需要知道一个三位数k:个位 = k%10   十位 = k/10%10   百位 = k/100  代码: #include<iostream> using namespace std; int main() { for(int i=100; i<1000; i++){ int gewei = i%10; int shiwei = i/10%10; int baiwei = i/100; int sum = (gewei*gewei*gewei)+(shiwei*

九度机试 题目1165:字符串匹配 2008年北京航空航天大学计算机研究生机试真题

题目1165:字符串匹配 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2497 解决:858 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中和短字符串的所有匹配,输出行号.匹配字符串.匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配.如"aa[123]bb",就是说aa1bb.aa2bb.aa3bb都算匹配. 输入: 输入有多组数据. 每组数据第一行输入n(1<=n<=1000),从第二行开始输入n个字符串(

2005年华中科技大学计算机保研机试真题 找位置

题目1199: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <vector> 5 using namespace std; 6 struct node{ 7 vector<int> v; 8 }; 9 node map['z'+1]; 10 int main(){ 11 string s; 12 while(cin>>s){ 13