QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#808304#7871. 命运NineSuns5 981ms153288kbC++142.7kb2024-12-10 19:40:432024-12-10 19:40:44

Judging History

你现在查看的是最新测评结果

  • [2024-12-10 19:40:44]
  • 评测
  • 测评结果:5
  • 用时:981ms
  • 内存:153288kb
  • [2024-12-10 19:40:43]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pii pair <int, int>
#define fi first
#define se second
#define pb push_back

using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 5e5+5, M = 5e4+5;
int n, m, k, s, os[N], ck[N], cd;
ll ans;
map <int, int> mp[N];
struct dsu {
	int stk[N<<5], o[N<<5], v[N<<5], ed, d[M], fa[M], c;
	void init (int n) {
		for (int i = 1;i <= n;i++) fa[i] = i;
		c = n;
	}
	inline int find (int x) {
		while (fa[x]^x) x = fa[x];
		return x;
	}
	inline void mer (int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		if (d[x] < d[y]) swap(x, y);
		stk[++ed] = x; o[ed] = 0; v[ed] = fa[x]; fa[x] = y;
		if (d[x] == d[y]) stk[++ed] = y, o[ed] = 1, v[ed] = d[y], d[y]++;
		c--;
	}
	void del (int x) {
		while (ed) {
			if (o[ed]) d[stk[ed]] = v[ed];
			else fa[stk[ed]] = v[ed];
			ed--;
		}
	}
}fs, ds, ds2;
struct eg {
	int u, v, w;
}es[N];
vector <int> v[2][N<<2];
void uadd (int p, int l, int r, int tl, int tr, int o, int x) {
	if (tl > tr) return;
	if (tl <= l && r <= tr) return v[o][p].pb(x), void();
	int mid = l+r>>1;
	if (tl <= mid) uadd(p<<1, l, mid, tl, tr, o, x);
	if (tr > mid) uadd(p<<1|1, mid+1, r, tl, tr, o, x);
}
void query (int p, int l, int r) {
	int tc = ds.c, td = ds.ed, tc2 = ds2.c, td2 = ds2.ed;
	for (int x : v[0][p]) {
		ds.mer(es[x].u, es[x].v);
		if (es[x].u != s && es[x].v != s) ds2.mer(es[x].u, es[x].v);
	}
	for (int x : v[1][p]) {
		ds.mer(es[x].u, es[x].v); ds2.mer(es[x].u, es[x].v);
	}
	if (l == r) {
		int o = 1;
//		cout << ds.c << " " << ds2.c << endl;
		if (ds2.c-1 > k) o = 0;
		if (ds.c > 1) o = 0;
		if (k > ck[l+1]) o = 0;
//		cout << l << " " << o << "\n";
		if (l > m && o == 0) {
			cout << "nonnondayo"; exit(0);
		}
		if (!o) uadd(1, 1, m+1, l+1, m+1, 1, l), k -= (es[l].u == s || es[l].v == s), ans += es[l].w;
	}
	else {
		int mid = l+r>>1;
		query(p<<1, l, mid); query(p<<1|1, mid+1, r);
	}
	ds.c = tc; ds2.c = tc2;
	ds.del(td); ds2.del(td2);
}

signed main () {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m >> k >> s;
	ds.init(n); ds2.init(n); 
	for (int i = 1;i <= m;i++) {
		int u, v, w; cin >> u >> v >> w;
		if (u > v) swap(u, v);
		es[i] = (eg){u, v, w};
	}
	sort(es+1, es+1+m, [&](eg x, eg y) { return x.w > y.w; });
	for (int i = 1;i <= m;i++) {
		if (mp[es[i].u].count(es[i].v) && (es[i].u == s || es[i].v == s)) {
			os[i] = 0; continue;
		}
		os[i] = 1; mp[es[i].u][es[i].v] = 1;
		uadd(1, 1, m+1, 1, i-1, 0, i);
	}
	for (int i = m;i >= 1;i--) ck[i] = ck[i+1]+(es[i].u == s || es[i].v == s);
	query(1, 1, m+1);
	cout << ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 973ms
memory: 150904kb

input:

49277 49276 1 49277
1 2 2000
2 3 3000
2 4 4000
3 5 5000
3 6 6000
4 7 7000
1 8 8000
7 9 9000
1 10 10000
5 11 11000
4 12 12000
3 13 13000
13 14 14000
1 15 15000
7 16 16000
11 17 17000
2 18 18000
10 19 19000
6 20 20000
4 21 21000
17 22 22000
1 23 23000
14 24 24000
4 25 25000
16 26 26000
15 27 27000
9 2...

output:

1214136002000

result:

ok single line: '1214136002000'

Test #2:

score: 5
Accepted
time: 977ms
memory: 152604kb

input:

49324 49323 1 49324
1 2 2
2 3 3
2 4 4
3 5 5
4 6 6
6 7 7
2 8 8
2 9 9
4 10 10
6 11 11
9 12 12
4 13 13
8 14 14
8 15 15
7 16 16
11 17 17
15 18 18
2 19 19
10 20 20
19 21 21
14 22 22
16 23 23
20 24 24
23 25 25
3 26 26
2 27 27
8 28 28
11 29 29
20 30 30
15 31 31
16 32 32
19 33 33
29 34 34
5 35 35
21 36 36
1...

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Test #3:

score: 5
Accepted
time: 725ms
memory: 153288kb

input:

49423 49422 1 1
1 2 2
2 3 3
1 4 4
3 5 5
2 6 6
1 7 7
1 8 8
7 9 9
3 10 10
3 11 11
5 12 12
3 13 13
9 14 14
10 15 15
13 16 16
5 17 17
9 18 18
12 19 19
17 20 20
4 21 21
5 22 22
12 23 23
21 24 24
21 25 25
11 26 26
17 27 27
21 28 28
18 29 29
17 30 30
2 31 31
21 32 32
28 33 33
17 34 34
31 35 35
11 36 36
8 3...

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Test #4:

score: 5
Accepted
time: 981ms
memory: 149240kb

input:

49501 49500 49501 49501
1 2 2
1 3 3
3 4 4
2 5 5
5 6 6
1 7 7
6 8 8
5 9 9
6 10 10
5 11 11
2 12 12
12 13 13
13 14 14
8 15 15
13 16 16
5 17 17
9 18 18
9 19 19
9 20 20
14 21 21
18 22 22
16 23 23
7 24 24
1 25 25
7 26 26
1 27 27
15 28 28
22 29 29
20 30 30
12 31 31
4 32 32
16 33 33
22 34 34
11 35 35
27 36 3...

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Subtask #2:

score: 0
Wrong Answer

Test #5:

score: 10
Accepted
time: 8ms
memory: 139736kb

input:

21 20 2 21
1 2 18581
1 3 9620
2 4 4362
1 5 7702
5 6 17435
4 7 11679
3 8 14832
1 9 15073
2 10 6595
5 11 19676
11 12 16943
12 13 5268
5 14 20262
8 15 8192
7 16 12340
7 17 1437
7 18 3064
2 19 10437
12 20 2882
19 21 13483

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Test #6:

score: 0
Wrong Answer
time: 15ms
memory: 140304kb

input:

10 20 4 3
1 2 18247
2 3 9041
2 4 4031
2 5 7363
3 6 17172
1 7 11014
6 8 14781
8 9 15347
8 10 6598
5 9 19331
3 10 16523
10 6 5732
2 3 20357
6 9 8827
10 3 12864
6 3 1551
5 6 3932
3 1 10223
9 3 2296
8 5 13620

output:

61781

result:

wrong answer 1st lines differ - expected: '54418', found: '61781'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #14:

score: 0
Time Limit Exceeded

input:

49997 54855 3516 1
3 2 123052288
4 2 96660489
5 4 21916339
6 4 110026562
7 2 37432698
8 4 38560777
9 5 83209343
10 9 80352789
11 2 88956732
12 7 65449905
13 2 38239108
14 5 90154247
15 4 30810782
16 13 96492679
17 14 112886179
18 9 87190321
19 5 91181643
20 3 107304129
21 15 101439791
22 19 12060197...

output:

nonnondayo

result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

9992 99999 500 1
1 2 96661121
1 3 21915940
1 4 110026320
1 5 37433129
1 6 38560320
1 7 83209024
1 8 80352054
1 9 88957672
1 10 65449874
1 11 38239186
1 12 90153728
1 13 30810974
1 14 96493404
1 15 112886259
1 16 87190053
1 17 91182163
1 18 107303768
1 19 101439823
1 20 120601875
1 21 122197599
1 22 ...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%