题目 › usaco-1499

Friendship Editing

USACO 2025 February · 金组 · 2025-02 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Farmer John's $N$ cows are labeled $1$ to $N$ ($2\le N\le 16$). The friendship relationships between the cows can be modeled as an undirected graph with $M$ ($0\le M\le N(N-1)/2$) edges. Two cows are friends if and only if there is an edge between them in the graph.

In one operation, you can add or remove a single edge from the graph. Count the minimum number of operations required to ensure that the following property holds: If cows $a$ and $b$ are friends, then for every other cow $c$, at least one of $a$ and $b$ is friends with $c$.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$ and $M$.

The next $M$ lines each contain a pair of friends $a$ and $b$ ($1\le a<b\le N$). No pair of friends appears more than once.

OUTPUT FORMAT (print output to the terminal / stdout):

The number of edges you need to add or remove.

SAMPLE INPUT:

3 1
1 2

SAMPLE OUTPUT:

1

The network violates the property. We can add one of edges $(2,3)$ or $(1,3)$, or remove edge $(1,2)$ to fix this.

SAMPLE INPUT:

3 2
1 2
2 3

SAMPLE OUTPUT:

0

No changes are necessary.

SAMPLE INPUT:

4 4
1 2
1 3
1 4
2 3

SAMPLE OUTPUT:

1

SCORING:

  • Inputs 4-13: One input for each $N\in [6, 15]$ in increasing order.
  • Inputs 14-18: $N=16$

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 1.9k平均输出 token(含思考) ↓
  • 27.0输出速度 tokens/s ↑
  • 52 秒延迟 ↓
  • 0.92×CPU 时间 / 官方解 ↓
  • 同阶 2、更优 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 5.2k平均输出 token(含思考) ↓
  • 207.6输出速度 tokens/s ↑
  • 23 秒延迟 ↓
  • 0.51×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 7.8k平均输出 token(含思考) ↓
  • 142.3输出速度 tokens/s ↑
  • 53 秒延迟 ↓
  • 0.49×CPU 时间 / 官方解 ↓
  • 更优 2、同阶 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 50 行
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<int> adj(N, 0);
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        adj[a] |= 1 << b;
        adj[b] |= 1 << a;
    }

    int size = 1 << N;
    vector<int> edges(size, 0);

    for (int mask = 1; mask < size; ++mask) {
        int bit = mask & -mask;
        int v = __builtin_ctz(bit);
        int rest = mask ^ bit;
        edges[mask] = edges[rest] + __builtin_popcount(adj[v] & rest);
    }

    const int INF = 1e9;
    vector<int> dp(size, INF);
    dp[0] = 0;

    for (int mask = 1; mask < size; ++mask) {
        int required = mask & -mask;
        int totalPairs = __builtin_popcount(mask) * (__builtin_popcount(mask) - 1) / 2;

        for (int part = mask; part; part = (part - 1) & mask) {
            if (!(part & required)) continue;
            int rest = mask ^ part;
            int crossPairs = __builtin_popcount(part) * __builtin_popcount(rest);
            int crossEdges = edges[mask] - edges[part] - edges[rest];
            int cost = edges[part] + (crossPairs - crossEdges) + dp[rest];
            dp[mask] = min(dp[mask], cost);
        }
    }

    cout << dp[size - 1] << '\n';
    return 0;
}