1 条题解

  • 0
    @ 2026-5-5 22:50:27

    主要观察结论是:如果图是一组不相交的团(clique),则输出 "YES"(在每一个不是团的连通分量中,都存在一个三元组 X,Y,ZX,Y,Z,使得 XYX-YYZY-Z 成立,但 XZX-Z 不成立)。

    为了检查每个连通分量是否都是团,可以运行 DFS,并统计该连通分量中的顶点数与边数。当且仅当 $\text{边数} = \frac{\text{顶点数} \times (\text{顶点数} - 1)}{2}$ 时,该分量是一个团。

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 150005;
    
    vector<int> g[N];
    bool vis[N];
    int n, m;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        cin >> n >> m;
        for (int i = 0; i < m; ++i) {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
    
        bool ok = true;
    
        for (int i = 1; i <= n; ++i) {
            if (!vis[i]) {
                // BFS 找连通分量
                queue<int> q;
                q.push(i);
                vis[i] = true;
                int vertices = 0;
                long long edges = 0; // 实际边数(每条边会被计两次,最后除以2)
    
                while (!q.empty()) {
                    int u = q.front(); q.pop();
                    vertices++;
                    edges += g[u].size();
                    for (int v : g[u]) {
                        if (!vis[v]) {
                            vis[v] = true;
                            q.push(v);
                        }
                    }
                }
    
                edges /= 2; // 无向边每条被计两次
                long long expected = 1LL * vertices * (vertices - 1) / 2;
                if (edges != expected) {
                    ok = false;
                    break;
                }
            }
        }
    
        cout << (ok ? "YES" : "NO") << '\n';
    
        return 0;
    }
    
    • 1

    信息

    ID
    6972
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    递交数
    0
    已通过
    0
    上传者