2025.2.19——1500

2025.2.19——1500


A 1500

B 1500

C 1500

D 1500

------------------------------------------------

  • 思维/图论+位运算/思维+数学/思维+构造/思维


A

  1. 存在路径即在一个连通块。
  2. 加上必须加的边,删去必须要删去的边。并查集维护查询,考虑一下删边和加边的先后顺序。

B

  1. 位运算入手点当然是单独考虑每一位。发现三个数中在同一位中有1个/2个1才会有贡献。
  2. 注意区间范围。

C

  1. 分奇偶模拟下过程发现结论。

D

  1. 尝试0101进行构造。
  2. 再考虑奇偶和 x 、 y x、y xy 关系,找通解。

------------------------代码------------------------

A

#include <bits/stdc++.h>
#define int long long
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        cout << '\n';           \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

// 带权并查集
struct DSU
{
    vector<int> p, vs, es; // 集合数 点数 边数 (对一个连通块而言)
    DSU(int n1)            // p[x]不一定为根节点  find(x)一定是根节点
    {
        int n = n1 + 2;
        p.assign(n, 0);
        vs.assign(n, 0);
        es.assign(n, 0);
        for (int i = 1; i <= n1; i++)
            p[i] = i, vs[i] = 1, es[i] = 0;
    }
    int find(int x) // 找到根节点
    {
        if (p[x] == x)
            return x;
        int px = find(p[x]);
        return p[x] = px;
    }
    bool same(int a, int b)
    {
        return find(a) == find(b);
    }
    void merge(int a, int b) // 合并集合
    {
        int pa = find(a);
        int pb = find(b);
        if (pa == pb) // pa pb 均为根节点 p[pa]==pa
        {
            es[pa]++; // 1个集合 边+1
            return;
        }
        p[pb] = p[pa];        // 改变b的根节点
        vs[pa] += vs[pb];     // 将b合并进a
        es[pa] += es[pb] + 1; // 2个集合
    }
    int size(int a) //  集合内的元素的个数
    {
        return vs[find(a)];
    }
};
//  DSU(n);

void _()
{
    int n, m1, m2;
    cin >> n >> m1 >> m2;
    DSU F(n), G(n);
    vector<pair<int, int>> ef(m1), eg(m2);
    for (auto &[x, y] : ef)
        cin >> x >> y;

    for (auto &[x, y] : eg)
    {
        cin >> x >> y;
        G.merge(x, y);
    }
    int res = 0;
    for (auto [x, y] : ef)
    {
        if (!G.same(x, y))
            res++;
        else
            F.merge(x, y);
    }
    for (auto [x, y] : eg)
    {
        if (!F.same(x, y))
        {
            res++;
            F.merge(x, y);
        }
    }

    cout << res << '\n';
}

B

#include <bits/stdc++.h>
#define int long long
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        cout << '\n';           \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int l, r;
    cin >> l >> r;
    int a = 0, b = 0, c = 0;
    int hi = 32;
    for (;; hi--)
    {
        int r_bit = r >> hi & 1, l_bit = l >> hi & 1;
        if (r_bit - l_bit)
            break;
        else if (l_bit)
        {
            a |= 1 << hi, b |= 1 << hi, c |= 1 << hi;
        }
    }
    c |= 1 << hi;
    b = c - 1;
    a = b - 1 < l ? c + 1 : b - 1;
    cout << a << ' ' << b << ' ' << c << '\n';
}
// void _()
// {
//     int a, b, c;
//     cin >> a >> b >> c;
//     cout << (a ^ b) + (b ^ c) + (a ^ c) << '\n';
// }

C

#include <bits/stdc++.h>
#define int long long
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        cout << '\n';           \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, k;
    cin >> n >> k;
    int st = 1, cnt = 0;
    int l = 1, r = n;
    while (r - l + 1 >= k)
    {
        int mid = l + r >> 1;
        if (r - l + 1 & 1)
        {
            cnt += st;
            r = mid - 1;
        }
        else
            r = mid;
        st <<= 1;
    }
    // int res = (1 + n >> 1) * cnt;
    // if (n % 2 == 0)
    //     res = (1 + n) * cnt >> 1;
    int res = (1 + n) * cnt >> 1;
    cout << res << '\n';
}

D

#include <bits/stdc++.h>
#define int long long
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        cout << '\n';           \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n, x, y;
    cin >> n >> x >> y;
    vector<int> f(n + 1);
    if (n & 1)
    {
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            if (i == x)
                f[i] = 2;
            else
            {
                f[i] = ans;
                ans ^= 1;
            }
        }
    }
    else
    {
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            f[i] = ans;
            ans ^= 1;
        }
        if (abs(x - y) % 2 == 0)
            f[x] = 2;
    }
    for (int i = 1; i <= n; i++)
        cout << f[i] << ' ';
    cout << '\n';
}
// void _()
// {
//     int n, x, y;
//     cin >> n >> x >> y;
//     vector<set<int>> a(n + 1, set<int>());
//     for (int i = 1; i <= n; i++)
//     {
//         int l = i - 1, r = i + 1;
//         if (!l)
//             l = n;
//         if (r > n)
//             r = 1;
//         a[i].insert(l);
//         a[i].insert(r);
//     }
//     a[x].insert(y);
//     a[y].insert(x);
//     auto cal = [](set<int> s)
//     {
//         int st = 0;
//         for (auto v : s)
//         {
//             if (v - st)
//                 return st;
//             st++;
//         }
//         return st;
//     };
//     for (int i = 1; i <= n; i++)
//         cout << cal(a[i]) << ' ';
//     cout << '\n';
// }

### 解决方案分析 当尝试建立与对等服务器 `server2` 的网络级连接时遇到错误,可能的原因在于端口或地址冲突以及NAT穿透问题。 #### 地址重用冲突 如果之前的 `outboundconnect()` 尝试使用了一组源和目标端点组合,而这些端点现在已被另一个套接字占用,则异步 `connect()` 请求通常会在某个时刻失败,并返回“地址已在使用”的错误消息[^1]。尽管如此,应用程序仍然拥有用于通信的工作P2P流套接字,因此可以忽略此失败并继续操作。 #### NAT兼容性和洞穿支持 对于TCP协议,在286个数据样本中,有184个(占总数的64%)展示了与TCP打孔技术的良好兼容性:NAT设备会一致地转换客户端私有的TCP端点,并不会因未请求的入站连接尝试发送RST包作为响应。然而,回环翻译的支持率要低得多——仅37份报告(占比约13%)显示了针对TCP的此类特性支持[^4]。 为了有效解决问题: - **确认本地资源释放**:确保之前创建的所有套接字已正确关闭,防止新连接由于端口被占用而导致失败。 - **调整防火墙/NAT设置**:检查是否有任何安全策略阻止来自特定IP范围的数据传输;考虑配置静态映射或者启用UPnP来简化外部访问过程。 - **实施TCP Hole Punching机制**:利用该方法绕过某些类型的NAT限制,提高成功建立直接P2P连接的可能性。 ```python import socket def establish_connection(server_address, port): sock = None try: # 创建一个新的socket对象 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 设置SO_REUSEADDR选项允许快速重启服务而不必等待TIME_WAIT状态结束 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 进行连接前先绑定到指定接口上的随机可用端口 sock.bind(('0.0.0.0', 0)) # 发起连接请求 sock.connect((server_address, port)) print(f"Connection established successfully.") return sock except Exception as e: print(f"Failed to connect: {e}") if sock is not None: sock.close() raise finally: pass # 可在此处添加清理逻辑 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值