제출 #421463

#제출 시각아이디문제언어결과실행 시간메모리
421463Aldas25이상적인 도시 (IOI12_city)C++14
100 / 100
442 ms36752 KiB
#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;

const int MAXN = 100100;
const ll MOD = 1000 * 1000 * 1000;

ll par[MAXN], sz[MAXN];
int nodeId[MAXN];
int find (int a) {return par[a] = par[a]==a ? a : find(par[a]);}
bool same (int a, int b) {return find(a) == find(b);}
void unite (int a, int b) {
    a = find(a), b = find(b);
    if (a == b) return;
    par[b] = a;
    sz[a] += sz[b];
}

int x[MAXN], y[MAXN];
ll n;
ll ans = 0;
unordered_set<int> adj[MAXN];
map<pair<ll, ll>, int> toId;
ll dp[MAXN];

void dfs (int v, int p) {

    for (int u : adj[v]) {
        if (u == p) continue;
        dfs(u, v);
        dp[v] += dp[u];
    }

    ans += dp[v]*(n-dp[v]) % MOD;
    ans %= MOD;
}

int cnt = 0;

int DistanceSum(int N, int *X, int *Y) {

    n = N;
    FOR(i, 0, n-1) x[i] = X[i];
    FOR(i, 0, n-1) y[i] = Y[i];
    FOR(i, 0, n-1) toId[{x[i], y[i]}] = i+1;

    /// first tree

    FOR(i, 0, n-1) {
        par[i] = i;
        sz[i] = 1;
    }

    FOR(i, 0, n-1) {
        int id = toId[{x[i], y[i]-1}];
        if (id > 0) {
            id--;
            unite(i, id);
        }
    }

    FOR(i, 0, n-1) {
        if (find(i) != i) continue;
        nodeId[i] = ++cnt;
        dp[cnt] = sz[i];
    }

    FOR(i, 0, n-1) {
        int id = toId[{x[i]+1, y[i]}];
        if (id > 0) {
            id--;
            int u = nodeId[find(i)];
            int v = nodeId[find(id)];
            adj[u].insert(v);
            adj[v].insert(u);
        }
    }

    /// second tree

    FOR(i, 0, n-1) {
        par[i] = i;
        sz[i] = 1;
    }

    FOR(i, 0, n-1) {
        int id = toId[{x[i]-1, y[i]}];
        if (id > 0) {
            id--;
            unite(i, id);
        }
    }

    FOR(i, 0, n-1) {
        if (find(i) != i) continue;
        nodeId[i] = ++cnt;
        dp[cnt] = sz[i];
    }

    FOR(i, 0, n-1) {
        int id = toId[{x[i], y[i]+1}];
        if (id > 0) {
            id--;
            int u = nodeId[find(i)];
            int v = nodeId[find(id)];
            adj[u].insert(v);
            adj[v].insert(u);
        }
    }

    dfs (1, -1);
    dfs (cnt, -1);

    return ans%MOD;
}

/*

15
7 5
7 6
8 3
8 4
8 5
8 6
9 3
9 4
9 5
9 6
10 3
10 4
10 5
10 6
11 5
ans: 278?

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...