답안 #987725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987725 2024-05-23T12:46:01 Z vjudge1 이상적인 도시 (IOI12_city) C++17
0 / 100
31 ms 12436 KB
#pragma once
#include "bits/stdc++.h"
#define F first
#define S second
#define ll long long
#define pii pair<ll,ll>
const int mxN = 2e5 + 5;
const int mod = 1e9;
using namespace std;
vector<vector<int>>v;
ll dpD[mxN],dpU[mxN];
ll subD[mxN],subU[mxN];
ll w[mxN];
int p[mxN];
ll ans;
void dfs(int u,int par){
	subD[u] = w[u];
    p[u] = par;
	for(auto x : v[u]){
		if(x == par) continue;
		dfs(x,u);
		dpD[u] += dpD[x] + subD[x] * w[u];
		subD[u] += subD[x];
	}
}
void hfs(int u,int par){
	// subU[u] = w[u];
	dpU[u] = dpU[par] + subU[par] * w[u];
	subU[u] += subU[par];
	if(par){
		dpU[u] += (dpD[par] - (dpD[u] + subD[u] * w[par])) + (subD[par] - subD[u]) * w[u];
        subU[u] += subD[par] - subD[u];
	}
	for(auto x : v[u]){
		if(x == par) continue;
		hfs(x,u);
	}
	//cout<<u<<' '<<dpU[u]<<'\n';
}
ll solve(int N,int X[],int Y[]){
    ans = 0;
    for(int i = 0; i <= N;i++){
        dpD[i] = 0;
        dpU[i] = 0;
        subD[i] = 0;
        subU[i] = 0;
        w[i] = 0;
        p[i] = 0;
    }
    map<pii,int>mp;
    map<pii,bool>vis;
    for(auto &x : v) x.clear();
    v.clear();
    v.resize(N + 1);
    int id = 0;
    vector<pii>a;
    for(int i = 0;i < N;i++){
        a.push_back({X[i],Y[i]});
    }
    sort(a.begin(),a.end());
    int pX = -1,pY = -1;
    for(auto x : a){
        if(x.F != pX || x.S != pY + 1){
            id++;
        }
        mp[x] = id;
        w[id]++;
        if(mp[{x.F - 1,x.S}] && !vis[{mp[x],mp[{x.F - 1,x.S}]}]){
            v[mp[{x.F - 1,x.S}]].push_back(mp[x]);
            v[mp[x]].push_back(mp[{x.F - 1,x.S}]);
            vis[{mp[x],mp[{x.F - 1,x.S}]}] = 1;
        }
        pX = x.F;
        pY = x.S;
    }
    // for(int i = 1;i < 7;i++){
    //     cout<<i<<' '<<w[i]<<"{ ";
    //     for(auto x : v[i]){
    //         cout<<x<<' ';
    //     }
    //     cout<<"}\n";
    // }
    dfs(1,0);
    hfs(1,0);
    for(int i = 1;i <= N;i++){
        ans += dpD[i] + dpU[i];
        // cout<<dpD[i]<<' ';
    }
    // cout<<'\n';
    // for(int i = 1;i <= N;i++){
    //     cout<<dpU[i]<<' ';
    // }
    // cout<<'\n';
    // cout<<'\n';
    // ans /= 2;
    // ans %= mod;
    return ans;
}
int DistanceSum(int N, int X[], int Y[]){
    return (((solve(N,Y,X) + solve(N,X,Y)) / 2) % mod);
}

Compilation message

city.cpp:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 8656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 11672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 31 ms 12436 KB Output isn't correct
2 Halted 0 ms 0 KB -