Submission #768060

# Submission time Handle Problem Language Result Execution time Memory
768060 2023-06-27T11:58:26 Z boris_mihov Ideal city (IOI12_city) C++17
100 / 100
220 ms 28652 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <map>
 
typedef long long llong;
const int MAXN = 100000 + 10;
const int MOD = 1'000'000'000;
 
int n;
__int128 ans;
int sz[MAXN];
int par[MAXN];
__int128 dp[MAXN];
int chain[MAXN];
int inChain[MAXN];
int x[MAXN], y[MAXN];
std::vector <int> out[MAXN];
std::pair <int,int> delta[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::map <std::pair <int,int>, int> map;
std::vector <int> g[MAXN];
std::vector <int> t[MAXN];
std::queue <int> q;
 
void dfsDown(int node)
{
    dp[node] = 0;
    sz[node] = 1;
 
    for (const int &u : t[node])
    {
        if (u == par[node])
        {
            continue;
        }
        
        dfsDown(u);
        sz[node] += sz[u];
        dp[node] += dp[u] + sz[u];
    }
}
 
void dfsUp(int node)
{
    if (node != 1)
    {
        dp[node] = dp[par[node]] + n - 2 * chain[inChain[node]];
    }
    
    for (const int &u : t[node])
    {
        if (u != par[node])
        {
            dfsUp(u);
        }
    }
}
 
int timer;
void dfsChain(int node)
{
    inChain[node] = timer;
    chain[inChain[node]] += sz[node];
    for (const int &u : out[node])
    {
        if (inChain[u] == 0)
        {
            dfsChain(u);
        }
    }
}
 
int DistanceSum(int N, int *X, int *Y) 
{
    n = N;
    for (int i = 1 ; i <= n ; ++i)
    {
        x[i] = X[i - 1];
        y[i] = Y[i - 1];
        map[{x[i], y[i]}] = i;
    }
 
    for (int i = 1 ; i <= n ; ++i)
    {
        for (const auto &[dx, dy] : delta)
        {
            if (map.count({x[i] + dx, y[i] + dy}))
            {
                g[i].push_back(map[{x[i] + dx, y[i] + dy}]);
            }
        }
    }
 
    q.push(1);
    par[1] = 0;
    std::fill(par + 2, par + n + 1, -1);
    while (!q.empty())
    {
        int top = q.front();
        q.pop();
 
        for (const int &i : g[top])
        {
            if (par[i] == -1)
            {
                par[i] = top;
                t[top].push_back(i);
                q.push(i);
            } else if (par[i] != top && par[top] != i)
            {
                out[top].push_back(i);
            }
        }
    }
 
    dfsDown(1);
    for (int i = 1 ; i <= n ; ++i)
    {
        if (inChain[i] == 0)
        {
            timer++;
            dfsChain(i);
        }
    }
 
    dfsUp(1);
 
    for (int i = 1 ; i <= n ; ++i)
    {
        ans += dp[i];
    }
 
    return (ans / 2) % MOD;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7356 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7316 KB Output is correct
5 Correct 3 ms 7380 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 3 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 3 ms 7412 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 3 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7508 KB Output is correct
2 Correct 4 ms 7508 KB Output is correct
3 Correct 5 ms 7636 KB Output is correct
4 Correct 5 ms 7636 KB Output is correct
5 Correct 5 ms 7672 KB Output is correct
6 Correct 5 ms 7764 KB Output is correct
7 Correct 5 ms 7708 KB Output is correct
8 Correct 6 ms 7764 KB Output is correct
9 Correct 5 ms 7664 KB Output is correct
10 Correct 5 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 11340 KB Output is correct
2 Correct 40 ms 11468 KB Output is correct
3 Correct 96 ms 17740 KB Output is correct
4 Correct 90 ms 17672 KB Output is correct
5 Correct 209 ms 28084 KB Output is correct
6 Correct 208 ms 28040 KB Output is correct
7 Correct 215 ms 28256 KB Output is correct
8 Correct 210 ms 28108 KB Output is correct
9 Correct 202 ms 28040 KB Output is correct
10 Correct 172 ms 28652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 10896 KB Output is correct
2 Correct 32 ms 11348 KB Output is correct
3 Correct 85 ms 16564 KB Output is correct
4 Correct 85 ms 17240 KB Output is correct
5 Correct 179 ms 25804 KB Output is correct
6 Correct 220 ms 27624 KB Output is correct
7 Correct 179 ms 25936 KB Output is correct
8 Correct 197 ms 27300 KB Output is correct
9 Correct 203 ms 27460 KB Output is correct
10 Correct 203 ms 27412 KB Output is correct