結果

問題 No.3517 Snake Kunekune Graph
コンテスト
ユーザー abc864197532
提出日時 2026-04-24 23:09:30
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 3,391 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,701 ms
コンパイル使用メモリ 361,036 KB
実行使用メモリ 30,892 KB
最終ジャッジ日時 2026-04-24 23:09:45
合計ジャッジ時間 10,235 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#ifdef Doludu
template <typename T>
ostream& operator << (ostream &o, vector <T> vec) {
    o << "{"; int f = 0;
    for (T i : vec) o << (f++ ? " " : "") << i;
    return o << "}";
}
void bug__(int c, auto ...a) {
    cerr << "\e[1;" << c << "m";
    (..., (cerr << a << " "));
    cerr << "\e[0m" << endl;
}
#define bug_(c, x...) bug__(c, __LINE__, "[" + string(#x) + "]", x)
#define bug(x...) bug_(32, x)
#define bugv(x...) bug_(36, vector(x))
#define safe bug_(33, "safe")
#else
#define bug(x...) void(0)
#define bugv(x...) void(0)
#define safe void(0)
#endif
const int mod = 998244353, N = 2000006;

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n, m, c, k;
    cin >> n >> m >> c >> k;
    vector <int> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    vector <vector <pii>> adj(n);
    vector <pii> ed;
    int tot = 0;
    for (int i = 0, u, v; i < m; ++i) {
        cin >> u >> v, --u, --v;
        if (abs(a[u] - a[v]) > c) continue;
        adj[u].emplace_back(v, tot);
        adj[v].emplace_back(u, tot++);
        ed.emplace_back(u, v);
    }

    if (k == 2) {
        ll ans = 0;
        vector <int> vis(n, 0);
        for (int i = 0; i < n; ++i) if (!vis[i]) {
            queue <int> q;
            q.push(i), vis[i] = 1;
            int cnt = 0;
            while (!q.empty()) {
                int v = q.front(); q.pop();
                cnt += v < n;
                for (auto [u, id] : adj[v]) if (!vis[u]) {
                    q.push(u);
                    vis[u] = 1;
                } 
            }
            ans += 1ll * cnt * (cnt - 1);
        }
        cout << ans << "\n";
        return 0;
    }

    vector <vector <int>> g(tot);
    for (int i = 0; i < n; ++i) {
        sort(all(adj[i]), [&](auto x, auto y) {
            return a[x.first] < a[y.first];
        });
        for (int j = 1; j < sz(adj[i]); ++j) {
            auto [u, eid1] = adj[i][j - 1];
            auto [v, eid2] = adj[i][j];
            if (abs(a[u] - a[v]) <= c) {
                bug(eid1, eid2);
                g[eid1].pb(eid2);
                g[eid2].pb(eid1);
            }
        }
    }

    vector <int> cc(tot, -1), cnt, vis(n);
    for (int i = 0, j = 0; i < tot; ++i) if (cc[i] == -1) {
        queue <int> q;
        q.push(i), cc[i] = j;
        vector <int> vec;
        int now = 0;
        while (!q.empty()) {
            int v = q.front(); q.pop();
            auto [x, y] = ed[v];
            if (!vis[x]) now++;
            if (!vis[y]) now++;
            vis[x] = vis[y] = 1;
            vec.pb(v);
            for (int u : g[v]) {
                if (cc[u] == -1) {
                    cc[u] = j, q.push(u);
                }
            }
        }
        j++;
        for (int id : vec) {
            auto [x, y] = ed[id];
            vis[x] = vis[y] = 0;
        }
        cnt.pb(now);
    }

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int res = 0;
        set <int> S;
        for (auto [v, id] : adj[i]) {
            S.insert(cc[id]);
        }
        bug(i);
        bugv(all(S));
        for (int id : S) {
            res += cnt[id] - 1;
        }
        bug(i, res);
        ans += res;
    }
    cout << ans << "\n";
}
0