[CF1491D] Zookeeper and The Infinite Zoo - 贪心

Description

有一个无限图,其中有无数个节点,从 \(1\) 开始标号。有一条从 \(u\)\(u+v\) 的单向边,当且仅当 \(u\ AND\ v = v\)。有 \(q\) 个询问,询问是否存在一条从 \(u\)\(v\) 的路径。

Solution

因为这里只要考虑连通性,显然我们会考虑所有加 2 的幂次的情形

这个过程相当于把若干个低位的 1 变成一个高位的 1

所以只需要将 u,v 的 1 贪心匹配,保证大于等于关系即可

#include <bits/stdc++.h>
using namespace std;
 
#define int long long
const int N = 1000005;
 
bool solve()
{
    vector<int> a;
    vector<int> b;
    int p, q;
    cin >> p >> q;
    if (p > q)
        return false;
    for (int i = 0; i < 30; i++)
    {
        if (p & (1 << i))
            a.push_back(i);
        if (q & (1 << i))
            b.push_back(i);
    }
    for (int i = 0; i < b.size(); i++)
    {
        if (i >= a.size() || a[i] > b[i])
            return false;
    }
    return true;
}
 
signed main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        if (solve())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
}

相关文章:

  • 2022-03-03
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2021-10-12
  • 2021-12-08
  • 2021-12-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案