QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#288506#7415. Fast Spanning TreezhouhuanyiWA 6ms24856kbC++201.9kb2023-12-22 19:40:022023-12-22 19:40:03

Judging History

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

  • [2023-12-22 19:40:03]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:24856kb
  • [2023-12-22 19:40:02]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<set>
#include<queue>
#define N 300000
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
struct reads
{
	int num,data;
	bool operator < (const reads &t)const
	{
		return data!=t.data?data<t.data:num<t.num;
	}
};
int n,m,rt[N+1],st[N+1],leng,X[N+1],Y[N+1],Z[N+1],delta[N+1],tong[N+1],length;
bool used[N+1];
long long w[N+1];
set<reads>s[N+1];
priority_queue<int,vector<int>,greater<int>>q;
int find(int x)
{
	if (rt[x]==x) return x;
	return rt[x]=find(rt[x]);
}
void adder(int x)
{
	if (find(X[x])==find(Y[x])) return;
	if (s[find(X[x])].find((reads){x,(delta[x]+1)>>1})!=s[X[x]].end()) s[find(X[x])].erase((reads){x,(delta[x]+1)>>1});
	if (s[find(Y[x])].find((reads){x,(delta[x]+1)>>1})!=s[Y[x]].end()) s[find(Y[x])].erase((reads){x,(delta[x]+1)>>1});
	if (w[find(X[x])]+w[find(Y[x])]>=Z[x]) q.push(x);
	else delta[x]=Z[x]-w[find(X[x])]-w[find(Y[x])],s[find(X[x])].insert((reads){x,(delta[x]+1)>>1}),s[find(Y[x])].insert((reads){x,(delta[x]+1)>>1});
	return;
}
void solve(int x)
{
	length=0;
	while (!s[x].empty()&&(*s[x].begin()).data<=w[x]) tong[++length]=(*s[x].begin()).num,s[x].erase(s[x].begin());
	for (int i=1;i<=length;++i) adder(tong[i]);
	return;
}
void unionn(int x,int y)
{
	if (s[x].size()>s[y].size()) swap(s[x],s[y]);
	for (auto it:s[x]) s[y].insert(it);
	w[y]+=w[x],solve(y),rt[x]=y;
	return;
}
int main()
{
	int top;
	n=read(),m=read();
	for (int i=1;i<=n;++i) w[i]=read(),rt[i]=i;
	for (int i=1;i<=m;++i) X[i]=read(),Y[i]=read(),Z[i]=read(),adder(i);
	for (int i=1;i<=n;++i) solve(i);
	while (!q.empty())
	{
		top=q.top(),q.pop();
		if (find(X[top])==find(Y[top])) continue;
		st[++leng]=top,unionn(find(X[top]),find(Y[top]));
	}
	printf("%d\n",leng);
	for (int i=1;i<=leng;++i) printf("%d ",st[i]);
	puts("");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 24528kb

input:

5 5
1 4 3 4 0
4 5 5
3 1 1
2 5 2
4 3 1
4 1 4

output:

4
2 3 1 4 

result:

ok 5 number(s): "4 2 3 1 4"

Test #2:

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

input:

3 5
3 2 2
1 2 6
1 2 6
1 2 3
1 2 6
2 3 6

output:

2
3 5 

result:

ok 3 number(s): "2 3 5"

Test #3:

score: 0
Accepted
time: 3ms
memory: 24656kb

input:

10 20
4 6 6 1 7 9 1 8 7 9
5 3 2
1 10 10
5 6 7
9 6 9
3 1 1
6 8 1
5 7 0
9 5 4
10 3 4
8 6 8
3 10 6
5 3 8
3 7 9
1 9 10
10 1 0
5 7 6
6 10 1
6 5 9
5 8 2
9 2 4

output:

8
1 2 3 4 5 6 7 20 

result:

ok 9 numbers

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 24856kb

input:

10 20
0 10 4 6 2 0 2 0 2 8
5 10 8
7 1 6
10 5 0
9 8 0
5 8 4
5 1 8
10 3 6
5 4 3
9 2 6
10 3 6
4 3 1
3 10 3
6 1 3
3 2 5
6 9 2
4 2 5
10 6 5
8 6 3
2 1 0
2 3 6

output:

9
1 4 5 7 8 9 15 19 2 

result:

wrong answer 5th numbers differ - expected: '6', found: '7'