零点工作室暑假集训(AtCoder--ABC287)

A - Majority 

问字符串For 是否比字符串 Against的数量多.

代码如下:

#include <iostream>

using namespace std;

int n, ans, res;
string s;

int main()
{
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        cin >> s;
        if(s == "Against") ans ++;
        else res ++;
    }
    
    if(ans > res) cout << "No";
    else cout << "Yes";
    
    return 0;
}

B - Postal Card 

给一个字符串长度为6的字符串数组s和一个字符串长度为3的字符串数组t,问si长度为3的字符串后缀是否在t中出现过.直接对长度为6的字符串数组取余就好了 %1000;然后两层for循环去找出答案

代码如下:

#include <iostream>

using namespace std;

const int N = 1010;

int n, m, cnt;
int a[N], b[N];

int main()
{

    cin >> n >> m;
    
    for(int i = 0; i < n; i ++)
    {
        cin >> a[i];
        a[i] = a[i] % 1000;
    }
    
    for(int j = 0; j < m; j ++)
    {
        cin >> b[j];
    }
    
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            if(a[i] == b[j])
            {
                cnt ++;
                break;
            }
        }
    }
    
    
    cout << cnt << endl;
    
    return 0;
}

C - Path Graph? 

题意:给我们一个无向图,有n个顶点,m条边,让我们判断一下当前的这个无向图是否为路径图

并查集:

我们可以一个Path Graph一定满足以下条件:

  1. 只有一个连通块
  2. 只有两个入度为1,其余均是入度为2
  3. 点数-边数=1

用cnt数组记录各点的入度情况,并查集判断连通块个数即可。此外还要考虑是否有闭环的特殊情况。

#include <iostream>

using namespace std;

const int N = 200010;

int n, m, a, b, fu, fv, cnt1, cnt2;
int p[N], d[N];

int find(int x)
    {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        p[i] = i;
    }
    
    for(int i = 1; i <= m; i ++)
    {
        cin >> a >> b;
        d[a] ++, d[b] ++;
        fu = find(a), fv = find(b);
        p[fv] = fu;
    }
    
    for(int i = 1; i <= n; i ++)
    {
        if(d[i] == 1) cnt1 ++;
        else if(d[i] != 2)
        {
            cout << "No" << endl;
            return 0;
        }
        
        if(p[i] == i) cnt2 ++;
    }
    
    if(cnt1 == 2 && cnt2 == 1) cout << "Yes" << endl;
    else cout << "No" << endl;
    
    
    return 0;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值