답안 #304677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
304677 2020-09-21T17:18:33 Z Hehehe 이상적인 도시 (IOI12_city) C++14
0 / 100
253 ms 262148 KB
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
//#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 1e9 + 0;
const int N = 1e5 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

//ifstream in(".in");
//ofstream out(".out");

pi v[N];
int comp[N], siz[N], siz1[N], sum[N], sum1[N], viz[N], k;
map<pi, int> a;
set<int> g[N], g1[N];

void dfs(int nod, int p){

    sum[nod] = siz[nod];
    for(auto it : g[nod])
        if(it != p){
            dfs(it, nod);
            sum[nod] += sum[it];
        }
}

void dfs1(int nod, int p){

    sum1[nod] = siz1[nod];
    for(auto it : g1[nod])
        if(it != p){
            dfs1(it, nod);
            sum1[nod] += sum1[it];
        }
}

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

    for(int i = 1; i <= n; i++){
        v[i] = {X[i - 1], Y[i - 1]};
        a[v[i]] = i;
    }

    //horizontal
    for(int i = 1; i <= n; i++){
        if(viz[i])continue;

        comp[i] = ++k;

        int cnt = 0;

        int x = X[i - 1], y = Y[i - 1];
        while(a[{x, y + 1}]){
            y++;

            int idx = a[{x, y}];
            comp[idx] = k;
            viz[idx] = 1;

            cnt++;
        }

        y = Y[i - 1];
        while(a[{x, y - 1}]){
            y--;

            int idx = a[{x, y}];
            comp[idx] = k;
            viz[idx] = 1;

            cnt++;
        }

        siz[k] = cnt;
    }

    for(int i = 1; i <= n; i++){
        int I = X[i - 1], J = Y[i - 1];
        int now = comp[i];

        for(int l = 0; l < 4; l++){
            int x = I + dx[l], y = J + dy[l];

            if(!a[{x, y}])continue;
            int next = comp[a[{x, y}]];

            if(now == next)continue;

            g[now].insert(next);
            g[next].insert(now);
        }
    }

    dfs(1, 0);

    int ans = 0;

    for(int i = 1; i <= k; i++){
        ans = (ans + sum[i] * (n - sum[i]) + mod) % mod;
    }

    k = 0;

    //memset(viz, 0, sizeof(viz));
    //memset(comp, 0, sizeof(comp));

    //vertical
    for(int i = 1; i <= n; i++){
        if(viz[i])continue;

        comp[i] = ++k;

        int cnt = 0;

        int x = X[i - 1], y = Y[i - 1];
        while(a[{x + 1, y}]){
            x++;

            int idx = a[{x, y}];
            comp[idx] = k;
            viz[idx] = 1;

            cnt++;
        }

        x = X[i - 1];
        while(a[{x - 1, y}]){
            x--;

            int idx = a[{x, y}];
            comp[idx] = k;
            viz[idx] = 1;

            cnt++;
        }

        siz1[k] = cnt;
    }

    for(int i = 1; i <= n; i++){
        int I = X[i - 1], J = Y[i - 1];
        int now = comp[i];

        for(int l = 0; l < 4; l++){
            int x = I + dx[l], y = J + dy[l];

            if(!a[{x, y}])continue;
            int next = comp[a[{x, y}]];

            if(now == next)continue;

            g1[now].insert(next);
            g1[next].insert(now);
        }
    }

    dfs1(1, 0);

    for(int i = 1; i <= k; i++){
        ans = (ans + sum1[i] * (n - sum1[i]) + mod) % mod;
    }

    cout << ans << '\n';

}

Compilation message

city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:179:1: warning: no return statement in function returning non-void [-Wreturn-type]
  179 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 165 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 170 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 253 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 249 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -