Submission #49510742


Source Code Expand

// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")
// g++ "-Wl,--stack,1078749825" q1.cpp -o ab
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// find_by_order(idx) and *order_of_key(val)
// for multiset use less_equal and lower_bound works as upper_bound and vice versa
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(x) x.begin(), x.end()
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define tup(i, x) get<i>(x)
#define rem 1000000007
#define PI 3.141592653589793238462643383279502

vector<indexed_set *> subtree;
vector<ll> adj[300001];
vector<pair<ll, ll>> smaller; // smaller than self and smaller than parent in subtree

void dfs(ll i, ll parent = -1)
{
    ll largest = -1;
    vector<ll> children;
    for (ll node : adj[i])
    {
        if (node != parent)
        {
            dfs(node, i);
            children.push_back(node);
            if (largest == -1 || subtree[largest]->size() < subtree[node]->size())
            {
                largest = node;
            }
        }
    }
    if (largest == -1)
    {
        subtree[i] = new indexed_set; // new set for leaf node
    }
    else
    {
        subtree[i] = subtree[largest]; // largest sized child
    }

    for (ll child : children)
    {
        if (child == largest)
            continue;
        for (auto x : *subtree[child])
            subtree[i]->insert(x);
    }
    subtree[i]->insert(i);
    smaller[i] = {subtree[i]->order_of_key(i), subtree[i]->order_of_key(parent)};
}

vector<ll> ans;
ll sum = 0;
void reroot(ll node, vector<ll> adj[], vector<bool> &visited)
{
    visited[node] = true;
    ans[node] = sum;
    // do stuff
    for (auto x : adj[node])
    {
        if (!visited[x])
        {
            // to every child we go only through the parent while rerooting
            pair<ll, ll> p1 = smaller[node], p2 = smaller[x];
            ll osum = sum;
            sum -= p1.first;
            sum -= p2.first;
            smaller[x] = {x - 1, 0}; // correct
            ll val = x - 1 - p2.first;
            if (node < x)
            {
                val++;
            }
            smaller[node] = {node - 1 - p2.second, val};
            sum += smaller[x].first;
            sum += smaller[node].first;
            reroot(x, adj, visited);
            sum = osum;
            smaller[node] = p1;
            smaller[x] = p2;
        }
    }
}

int main()
{
    FAST;
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    /*ll tests;
    cin>>tests;
    for (int gg=0;gg<tests;gg++)
    {}*/
    ll n;
    cin >> n;
    subtree.resize(n + 1, NULL);
    for (int i = 0; i < n - 1; i++)
    {
        ll u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    smaller.assign(n + 1, {0, 0});
    dfs(1, -1);
    for (int i = 1; i <= n; i++)
    {
        sum += smaller[i].first;
    }
    ans.assign(n + 1, 0);
    vector<bool> visit(n + 1, false);
    reroot(1, adj, visit);
    for (int i = 1; i <= n; i++)
    {
        cout << ans[i] << " ";
    }
    cout << "\n";
}

Submission Info

Submission Time
Task G - Tree Inversion
User white__walker
Language C++ 20 (gcc 12.2)
Score 600
Code Size 3494 Byte
Status AC
Exec Time 447 ms
Memory 137580 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 72
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 02_handmade_64.txt, 02_handmade_65.txt, 02_handmade_66.txt, 02_handmade_67.txt, 02_handmade_68.txt, 02_handmade_69.txt, 02_handmade_70.txt, 02_handmade_71.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 2 ms 3612 KiB
00_sample_01.txt AC 2 ms 3408 KiB
00_sample_02.txt AC 2 ms 3528 KiB
01_random_03.txt AC 274 ms 68788 KiB
01_random_04.txt AC 363 ms 59344 KiB
01_random_05.txt AC 442 ms 137504 KiB
01_random_06.txt AC 363 ms 97120 KiB
01_random_07.txt AC 270 ms 68720 KiB
01_random_08.txt AC 361 ms 54616 KiB
01_random_09.txt AC 444 ms 137440 KiB
01_random_10.txt AC 361 ms 96764 KiB
01_random_11.txt AC 273 ms 68868 KiB
01_random_12.txt AC 296 ms 59348 KiB
01_random_13.txt AC 410 ms 137476 KiB
01_random_14.txt AC 330 ms 97044 KiB
01_random_15.txt AC 224 ms 68948 KiB
01_random_16.txt AC 315 ms 54856 KiB
01_random_17.txt AC 429 ms 137580 KiB
01_random_18.txt AC 372 ms 94512 KiB
01_random_19.txt AC 277 ms 68716 KiB
01_random_20.txt AC 369 ms 59012 KiB
01_random_21.txt AC 447 ms 137480 KiB
01_random_22.txt AC 370 ms 98952 KiB
01_random_23.txt AC 273 ms 68888 KiB
01_random_24.txt AC 362 ms 57756 KiB
01_random_25.txt AC 446 ms 137436 KiB
01_random_26.txt AC 362 ms 96288 KiB
01_random_27.txt AC 227 ms 68948 KiB
01_random_28.txt AC 293 ms 57848 KiB
01_random_29.txt AC 399 ms 137436 KiB
01_random_30.txt AC 321 ms 95552 KiB
01_random_31.txt AC 6 ms 5864 KiB
01_random_32.txt AC 48 ms 16196 KiB
01_random_33.txt AC 224 ms 80660 KiB
01_random_34.txt AC 294 ms 89736 KiB
01_random_35.txt AC 133 ms 40848 KiB
01_random_36.txt AC 311 ms 60452 KiB
01_random_37.txt AC 35 ms 15256 KiB
01_random_38.txt AC 89 ms 31144 KiB
01_random_39.txt AC 29 ms 9540 KiB
01_random_40.txt AC 87 ms 32468 KiB
01_random_41.txt AC 302 ms 84104 KiB
01_random_42.txt AC 76 ms 27020 KiB
01_random_43.txt AC 157 ms 37816 KiB
01_random_44.txt AC 377 ms 127060 KiB
01_random_45.txt AC 27 ms 15172 KiB
01_random_46.txt AC 105 ms 24820 KiB
01_random_47.txt AC 12 ms 7648 KiB
01_random_48.txt AC 293 ms 90416 KiB
01_random_49.txt AC 107 ms 42624 KiB
01_random_50.txt AC 159 ms 37928 KiB
01_random_51.txt AC 147 ms 56752 KiB
01_random_52.txt AC 219 ms 68800 KiB
01_random_53.txt AC 290 ms 58164 KiB
01_random_54.txt AC 397 ms 135328 KiB
01_random_55.txt AC 309 ms 94708 KiB
01_random_56.txt AC 214 ms 69476 KiB
01_random_57.txt AC 274 ms 54808 KiB
01_random_58.txt AC 392 ms 137476 KiB
01_random_59.txt AC 322 ms 96132 KiB
01_random_60.txt AC 289 ms 69472 KiB
01_random_61.txt AC 354 ms 59488 KiB
01_random_62.txt AC 436 ms 135592 KiB
01_random_63.txt AC 365 ms 96320 KiB
02_handmade_64.txt AC 2 ms 3440 KiB
02_handmade_65.txt AC 2 ms 3420 KiB
02_handmade_66.txt AC 265 ms 60924 KiB
02_handmade_67.txt AC 263 ms 60864 KiB
02_handmade_68.txt AC 259 ms 60916 KiB
02_handmade_69.txt AC 261 ms 60880 KiB
02_handmade_70.txt AC 267 ms 60912 KiB
02_handmade_71.txt AC 261 ms 60880 KiB