UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#200107#2260. speedrunAnonyme1005204ms56092kbC++112.1kb2023-12-28 11:26:332023-12-28 13:56:07

answer

#include<bits/stdc++.h>
using namespace std;

#define QwQ330AwA return 0
#define ll long long
#define int long long

const int N = 2000005;
const int inf = 1e9;

struct Tree {
	int ls, rs;
	int val;
	int tag;
	int u, v;
	int dis;
} t[N];
int tot;
#define ls(p) (t[p].ls)
#define rs(p) (t[p].rs)

int get_new(int u, int v, int w) {
	tot++;
	t[tot].u = u, t[tot].v = v, t[tot].val = w;
	return tot;
}

void push(int p, int w) {
	t[p].tag += w;
	t[p].val += w;
}

void down(int p) {
	if (!t[p].tag) return ;
	push(ls(p), t[p].tag);
	push(rs(p), t[p].tag);
	t[p].tag = 0;
}

int merge(int u, int v) {
	if (!u || !v) return u + v;
	down(u), down(v);
	if (t[u].val > t[v].val) swap(u, v);
	rs(u) = merge(rs(u), v);
	if (t[ls(u)].dis < t[rs(u)].dis) swap(ls(u), rs(u));
	t[u].dis = t[rs(u)].dis + 1;
	return u;
}

int del(int x) {
	down(x);
	return merge(ls(x), rs(x));
}

int rt[N], fa[N];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void link(int u, int v, int w) {
	if (u == v) return ;
	int qwq = get_new(u, v, w);
	rt[v] = merge(rt[v], qwq);
}

int n;
int stk[N], top;
bool vis[N];
int cnt;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	n++;
	int r = 1;
	for (int i = 2; i <= n; i++) {
		int sp, w;
		cin >> sp >> w;
		sp++;
		link(sp, i, w);
		for (int j = 1; j <= n; j++) {
			int w;
			cin >> w;
			link(j, i, w);
		}
	}
	for (int i = 1; i <= 2 * n; i++) fa[i] = i;
	for (int i = 1; i <= n; i++) link(i, i % n + 1, inf);
	top = 0;
	cnt = n;
	stk[++top] = r;
	vis[r] = 1;
	int ans = 0;
	while (rt[stk[top]]) {
		int id = rt[stk[top]];
		int u = find(t[id].u);
		if (u == stk[top]) {
			rt[stk[top]] = del(rt[stk[top]]);
			continue;
		}
		if (!vis[u]) {
			stk[++top] = u;
			vis[u] = 1;
			continue;
		}
		int p = ++cnt;
		while (vis[u]) {
			int v = stk[top];
			top--;
			vis[v] = 0;
			fa[v] = p;
			int ww = t[rt[v]].val;
			push(rt[v], -ww);
			if (find(t[rt[v]].v) != find(r)) ans += ww;
			rt[v] = del(rt[v]);
			rt[p] = merge(rt[p], rt[v]);
		}
		stk[++top] = p;
		vis[p] = 1;
	}
	cout << ans;
	QwQ330AwA;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 1292kb

input:

20
5 13929885 905968038 902465839 877284493 873226739 857380082 807817887 802863226 745191489 731682...

output:

1770026980

result:

ok "1770026980"

Test #2:

score: 0
Accepted
time: 1ms
memory: 1292kb

input:

20
8 41650813 982733904 965010316 947828639 931996408 890812482 882195345 874127465 843426203 840191...

output:

572871488

result:

ok "572871488"

Test #3:

score: 0
Accepted
time: 0ms
memory: 1288kb

input:

20
7 5609736 992686743 762471617 729573133 694448052 565847128 561937708 494490080 480492380 4529956...

output:

914997037

result:

ok "914997037"

Subtask #2:

score: 30
Accepted

Test #4:

score: 30
Accepted
time: 374ms
memory: 56092kb

input:

1000
1000 1514669 999821548 999208019 999058039 997885811 997519963 996887939 992771285 991618116 99...

output:

1867648404

result:

ok "1867648404"

Test #5:

score: 0
Accepted
time: 401ms
memory: 56092kb

input:

1000
1000 1346542 999240460 998282474 996725148 996189387 995687123 994922739 994615389 994427312 99...

output:

1826553056

result:

ok "1826553056"

Test #6:

score: 0
Accepted
time: 386ms
memory: 56092kb

input:

1000
1000 175777 997492053 997058970 996726517 996602523 995311350 995251035 995182486 993676274 993...

output:

1830991954

result:

ok "1830991954"

Subtask #3:

score: 30
Accepted

Test #7:

score: 30
Accepted
time: 15ms
memory: 3480kb

input:

200
177 9381925 995829624 983336723 979813951 977460662 975836272 967058907 961441255 960141236 9527...

output:

1816020211

result:

ok "1816020211"

Test #8:

score: 0
Accepted
time: 14ms
memory: 3480kb

input:

200
30 4286552 996621468 995826876 995519905 985655327 981252734 967441410 966383152 965652807 96527...

output:

1809301989

result:

ok "1809301989"

Test #9:

score: 0
Accepted
time: 11ms
memory: 3480kb

input:

200
173 3808221 999851000 986003435 985595659 979562075 963889418 943173147 935907573 934100679 9287...

output:

1827678229

result:

ok "1827678229"

Test #10:

score: 0
Accepted
time: 12ms
memory: 3476kb

input:

200
139 549058 998491959 994834766 984031612 982116934 978606391 977630480 976313462 976032220 97027...

output:

861008894

result:

ok "861008894"

Subtask #4:

score: 30
Accepted

Test #11:

score: 30
Accepted
time: 401ms
memory: 56088kb

input:

1000
621 114499 997721313 996514597 996308290 996253926 995993402 995523141 994699743 993937693 9925...

output:

898630932

result:

ok "898630932"

Test #12:

score: 0
Accepted
time: 387ms
memory: 56088kb

input:

1000
262 399801 998446888 997833841 996957270 996508115 996280316 995786496 995706706 994730053 9933...

output:

1068160867

result:

ok "1068160867"

Test #13:

score: 0
Accepted
time: 389ms
memory: 56084kb

input:

1000
1000 2737489 997513594 996345497 995871410 993469950 990161990 989691913 989437751 988018235 98...

output:

1003512081

result:

ok "1003512081"

Test #14:

score: 0
Accepted
time: 397ms
memory: 56088kb

input:

1000
326 201735 998550730 997518027 996614837 996266576 994608532 994005807 993493977 992120785 9901...

output:

843170570

result:

ok "843170570"

Test #15:

score: 0
Accepted
time: 422ms
memory: 56084kb

input:

1000
796 535702 995325443 994668895 994479064 994460977 994102432 993195360 990429156 989318285 9888...

output:

838963063

result:

ok "838963063"

Test #16:

score: 0
Accepted
time: 404ms
memory: 56084kb

input:

1000
846 531482 999379549 997730884 997022561 996149895 995981413 992507358 990807173 990643156 9902...

output:

1829819841

result:

ok "1829819841"

Test #17:

score: 0
Accepted
time: 408ms
memory: 56088kb

input:

1000
831 1169729 999868611 999536097 999117102 998006582 995979435 995748971 994460584 993676740 992...

output:

1820715424

result:

ok "1820715424"

Test #18:

score: 0
Accepted
time: 410ms
memory: 56084kb

input:

1000
139 247568 998351797 996223682 995706470 994165883 993650660 993378098 991868206 990091599 9898...

output:

1817999412

result:

ok "1817999412"

Test #19:

score: 0
Accepted
time: 380ms
memory: 56084kb

input:

1000
774 83343 999994666 999533673 999009643 997973120 997593294 996028006 992186045 991754669 98946...

output:

1852424307

result:

ok "1852424307"

Test #20:

score: 0
Accepted
time: 392ms
memory: 56088kb

input:

1000
309 1033613 999958159 999112545 998716374 997750098 997749385 997697598 994730198 994296209 993...

output:

1818623142

result:

ok "1818623142"