QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#139554#4272. Phone PlanskyEEcccccc0 395ms84224kbC++142.9kb2023-08-13 21:16:382023-08-13 21:16:40

Judging History

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

  • [2023-08-13 21:16:40]
  • 评测
  • 测评结果:0
  • 用时:395ms
  • 内存:84224kb
  • [2023-08-13 21:16:38]
  • 提交

answer

// Author: kyEEcccccc

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using ULL = unsigned long long;

#define F(i, l, r) for (int i = (l); i <= (r); ++i)
#define FF(i, r, l) for (int i = (r); i >= (l); --i)
#define MAX(a, b) ((a) = max(a, b))
#define MIN(a, b) ((a) = min(a, b))
#define SZ(a) ((int)((a).size()) - 1)

constexpr int N = 200005;

struct Edge
{
	int u, v;
	int l;
};

int n, ma, mb;
LL lim;
Edge ea[N], eb[N];

int ida[N], idb[N];
vector<int> pa[N], pb[N];
map<int, int> cnta[N], cntb[N];
int tar[N], from[N], num[N];

LL perf(int i)
{
	int t1 = ida[ea[i].u], t2 = ida[ea[i].v];
	if (t1 == t2) return 0;
	if (pa[t1] > pa[t2]) swap(t1, t2);
	LL res = (LL)pa[t1].size() * (LL)pa[t2].size();
	for (auto x : pa[t2]) res -= cnta[t1][idb[x]];
	for (auto x : pa[t2])
	{
		pa[t1].push_back(x);
		ida[x] = t1;
		cnta[t1][idb[x]] += 1;
		if ((cntb[idb[x]][t2] -= 1) == 0) cntb[idb[x]].erase(t2);
		cntb[idb[x]][t1] += 1;
	}
	cnta[t2].clear();
	pa[t2].clear();
	return res;
}

LL redo(int i)
{
	if (tar[i] == -1) return 0;
	int t1 = tar[i], t2 = from[i];
	assert(pb[t2].empty());
	F(_, 1, num[i])
	{
		int x = pb[t1].back();
		idb[x] = t2;
		cntb[t2][ida[x]] += 1;
		if ((cntb[t1][ida[x]] -= 1) == 0) cntb[t1].erase(ida[x]);
		if ((cnta[ida[x]][t1] -= 1) == 0) cnta[ida[x]].erase(t1);
		cnta[ida[x]][t2] += 1;
		pb[t2].push_back(x);
		pb[t1].pop_back();
	}
	reverse(pb[from[i]].begin(), pb[from[i]].end());
	LL res = (LL)pb[t1].size() * (LL)pb[t2].size();
	for (auto x : pb[t2]) res -= cntb[t1][ida[x]];
	return res;
}

signed main(void)
{
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(nullptr);

	cin >> n >> ma >> mb >> lim;
	F(i, 1, ma) cin >> ea[i].u >> ea[i].v >> ea[i].l;
	F(i, 1, mb) cin >> eb[i].u >> eb[i].v >> eb[i].l;

	sort(ea + 1, ea + ma + 1, [] (Edge x, Edge y) { return x.l < y.l; });
	sort(eb + 1, eb + mb + 1, [] (Edge x, Edge y) { return x.l < y.l; });

	F(i, 1, n)
	{
		idb[i] = i;
		pb[i].push_back(i);
	}
	F(i, 1, mb)
	{
		int t1 = idb[eb[i].u], t2 = idb[eb[i].v];
		if (t1 == t2)
		{
			tar[i] = -1;
			continue;
		}
		if (pb[t1].size() < pb[t2].size()) swap(t1, t2);
		tar[i] = t1, from[i] = t2;
		num[i] = pb[t2].size();
		for (auto x : pb[t2])
		{
			pb[t1].push_back(x);
			idb[x] = t1;
		}
		pb[t2].clear();
	}
	F(i, 1, n)
	{
		cntb[idb[i]][i] += 1;

		ida[i] = i;
		pa[i].push_back(i);
		cnta[i][idb[i]] += 1;
	}
	LL cur = 0;
	F(i, 1, mb)
	{
		int t = pb[i].size();
		cur += (LL)t * (t - 1) / 2;
	}

	int j = mb;
	int ans = INT_MAX;
	F(i, 0, ma)
	{
		if (i != 0) cur += perf(i);
		while (j > 0 && cur >= lim)
		{
			cur -= redo(j);
			--j;
		}

		if (j == mb)
		{
			MIN(ans, i);
			continue;
		}
		if (cur >= lim) MIN(ans, i == 0 ? 0 : ea[i].l);
		else MIN(ans, (i == 0 ? 0 : ea[i].l) + eb[j + 1].l);
	}
	cout << ans << '\n';
	
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 6
Accepted
time: 0ms
memory: 36364kb

input:

6 4 4 9
1 2 1
2 3 2
1 4 3
3 4 4
5 6 40
1 5 30
2 6 20
3 6 10

output:

33

result:

ok single line: '33'

Test #2:

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

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #3:

score: -6
Wrong Answer
time: 1ms
memory: 34368kb

input:

2 0 0 1

output:

0

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #53:

score: 5
Accepted
time: 395ms
memory: 84224kb

input:

200000 100000 100000 7628995
23677 113459 839167193
165893 15365 669621527
26287 109671 214795757
156871 136723 522277985
9957 100463 806718116
104783 161385 156110975
184577 92225 545172629
57373 130083 980035338
167231 49597 919886451
115601 24325 717233004
99413 141311 737488449
83437 62759 76873...

output:

502149991

result:

ok single line: '502149991'

Test #54:

score: -5
Wrong Answer
time: 321ms
memory: 73328kb

input:

200000 200000 87 2694
197233 86229 181875035
85167 196363 328068482
177795 65479 693403443
119609 181977 588691030
115815 139981 486110961
92473 20483 199129328
166989 95385 210368646
98095 54673 357307451
122543 94377 907487846
46611 80735 71787832
158893 117071 521262491
92051 104395 365725125
142...

output:

0

result:

wrong answer 1st lines differ - expected: '11965880', found: '0'

Subtask #3:

score: 0
Wrong Answer

Test #103:

score: 6
Accepted
time: 9ms
memory: 34200kb

input:

1 0 0 0

output:

0

result:

ok single line: '0'

Test #104:

score: -6
Wrong Answer
time: 0ms
memory: 36200kb

input:

2 0 0 1

output:

0

result:

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

Subtask #4:

score: 0
Skipped

Dependency #1:

0%