提出 #49516509


ソースコード 拡げる

// Author: vrintle (Rahul Verma)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

template <typename T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

const int N = 2e5 + 7;
vi g[N];
pbds<int>* s[N];
int tin[N], tout[N];

void solve() {
	int n;
	cin >> n;
	for(int _ = 0; _ < n - 1; _++) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int t = 0;
	function<void(int, int)> dfs0 = [&](int i, int p) -> void {
		tin[i] = t++;
		for(auto& v: g[i]) {
			if(v != p) {
				dfs0(v, i);
			}
		}
		tout[i] = t++;
	};
	dfs0(0, -1);
	vi pfx(n * 2);
	function<void(int, int)> dfs = [&](int i, int p) -> void {
		s[i] = new pbds<int>();
		int mx = 0, big = i;
		for(auto& e: g[i]) {
			if(e != p) {
				dfs(e, i);
				if((int) s[e]->size() > mx) mx = s[e]->size(), big = e;
			}
		}
		if(big != i) {
			int r = s[big]->order_of_key(i);
			pfx[0] += r; pfx[tin[big]] -= r;
			pfx[tout[big]] += r; pfx[2 * n - 1] -= r;
			s[i] = s[big];
		}
		s[i]->insert(i);
		for(auto& e: g[i]) {
			if(e != p && e != big) {
				int r = s[e]->order_of_key(i);
				pfx[0] += r; pfx[tin[e]] -= r;
				pfx[tout[e]] += r; pfx[2 * n - 1] -= r;
				for(auto it = s[e]->begin(); it != s[e]->end(); it++) {
					s[i]->insert(*it);
				}
			}
		}
		int out = i - s[i]->order_of_key(i);
		pfx[tin[i]] += out;
		pfx[tout[i]] -= out;
	};
	dfs(0, -1);
	for(int i = 1; i < n * 2; i++) {
		pfx[i] += pfx[i - 1];
	}
	for(int i = 0; i < n; i++) {
		cout << pfx[tin[i]] << ' ';
	}
}

// PRE-SUBMIT CHECKLIST
// --------------------
//
// -- reset the global arrays
// -- sort vector of vectors carefully
// more to be added...

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t = 1;
	// cin >> t;
	while(t--) {
		solve();
		cout << endl;
		// cerr << endl;
	}
	return 0;
}

提出情報

提出日時
問題 G - Tree Inversion
ユーザ BlindingKnight
言語 C++ 20 (gcc 12.2)
得点 600
コード長 2276 Byte
結果 AC
実行時間 424 ms
メモリ 150348 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 3
AC × 72
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 2 ms 3472 KiB
00_sample_01.txt AC 2 ms 3628 KiB
00_sample_02.txt AC 2 ms 3432 KiB
01_random_03.txt AC 226 ms 70340 KiB
01_random_04.txt AC 367 ms 85508 KiB
01_random_05.txt AC 416 ms 149844 KiB
01_random_06.txt AC 332 ms 109520 KiB
01_random_07.txt AC 220 ms 70292 KiB
01_random_08.txt AC 360 ms 79908 KiB
01_random_09.txt AC 418 ms 149900 KiB
01_random_10.txt AC 337 ms 108976 KiB
01_random_11.txt AC 233 ms 70364 KiB
01_random_12.txt AC 370 ms 85528 KiB
01_random_13.txt AC 420 ms 149840 KiB
01_random_14.txt AC 338 ms 109508 KiB
01_random_15.txt AC 235 ms 70320 KiB
01_random_16.txt AC 370 ms 80220 KiB
01_random_17.txt AC 422 ms 149896 KiB
01_random_18.txt AC 346 ms 106892 KiB
01_random_19.txt AC 236 ms 70344 KiB
01_random_20.txt AC 373 ms 85232 KiB
01_random_21.txt AC 419 ms 150256 KiB
01_random_22.txt AC 346 ms 111592 KiB
01_random_23.txt AC 225 ms 70272 KiB
01_random_24.txt AC 361 ms 83616 KiB
01_random_25.txt AC 417 ms 150348 KiB
01_random_26.txt AC 338 ms 108768 KiB
01_random_27.txt AC 228 ms 70312 KiB
01_random_28.txt AC 369 ms 83660 KiB
01_random_29.txt AC 424 ms 149920 KiB
01_random_30.txt AC 332 ms 107944 KiB
01_random_31.txt AC 5 ms 6016 KiB
01_random_32.txt AC 54 ms 22292 KiB
01_random_33.txt AC 234 ms 88228 KiB
01_random_34.txt AC 310 ms 101588 KiB
01_random_35.txt AC 109 ms 42136 KiB
01_random_36.txt AC 305 ms 80856 KiB
01_random_37.txt AC 32 ms 16832 KiB
01_random_38.txt AC 71 ms 31888 KiB
01_random_39.txt AC 27 ms 12784 KiB
01_random_40.txt AC 80 ms 35712 KiB
01_random_41.txt AC 281 ms 94624 KiB
01_random_42.txt AC 58 ms 27540 KiB
01_random_43.txt AC 169 ms 50156 KiB
01_random_44.txt AC 383 ms 139116 KiB
01_random_45.txt AC 25 ms 15364 KiB
01_random_46.txt AC 123 ms 35308 KiB
01_random_47.txt AC 12 ms 8404 KiB
01_random_48.txt AC 312 ms 102180 KiB
01_random_49.txt AC 114 ms 43800 KiB
01_random_50.txt AC 170 ms 50956 KiB
01_random_51.txt AC 156 ms 62012 KiB
01_random_52.txt AC 244 ms 70256 KiB
01_random_53.txt AC 369 ms 84084 KiB
01_random_54.txt AC 408 ms 147880 KiB
01_random_55.txt AC 354 ms 107504 KiB
01_random_56.txt AC 249 ms 70248 KiB
01_random_57.txt AC 371 ms 80396 KiB
01_random_58.txt AC 422 ms 149880 KiB
01_random_59.txt AC 332 ms 108540 KiB
01_random_60.txt AC 228 ms 70408 KiB
01_random_61.txt AC 365 ms 85456 KiB
01_random_62.txt AC 408 ms 147844 KiB
01_random_63.txt AC 341 ms 108956 KiB
02_handmade_64.txt AC 2 ms 3560 KiB
02_handmade_65.txt AC 2 ms 3420 KiB
02_handmade_66.txt AC 281 ms 87312 KiB
02_handmade_67.txt AC 278 ms 87420 KiB
02_handmade_68.txt AC 281 ms 87332 KiB
02_handmade_69.txt AC 280 ms 87308 KiB
02_handmade_70.txt AC 276 ms 87340 KiB
02_handmade_71.txt AC 268 ms 87344 KiB