QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105647#6402. MEXimum Spanning TreezhouhuanyiWA 902ms5816kbC++112.1kb2023-05-14 17:04:112023-05-14 17:04:15

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-14 17:04:15]
  • 评测
  • 测评结果:WA
  • 用时:902ms
  • 内存:5816kb
  • [2023-05-14 17:04:11]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define N 2000
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;
}
int n,m,d,s,t,len,rt[N+1],pv[N+1],X[N+1],Y[N+1],Z[N+1],cnt[N+1];
bool used[N+1],vis[N+1],vst[N+1];
vector<int>E[N+1];
void add(int x,int y)
{
    E[x].push_back(y);
    return;
}
int find(int x)
{
    if (rt[x]==x) return x;
    return rt[x]=find(rt[x]);
}
void unionn(int x,int y)
{
    rt[x]=y;
    return;
}
void add_edge()
{
    for (int i=s;i<=t;++i) pv[i]=0,E[i].clear();
    for (int i=0;i<=m;++i) vst[i]=0;
    for (int i=1;i<=n;++i) rt[i]=i;
    for (int i=1;i<=m;++i)
	if (used[i])
	{
	    vst[Z[i]]=1;
	    if (find(X[i])!=find(Y[i])) unionn(find(X[i]),find(Y[i]));
	}
    for (int i=1;i<=m;++i)
	if (!used[i])
	{
	    if (!vst[Z[i]]) add(s,i);
	    if (find(X[i])!=find(Y[i])) add(i,t);
	}
    for (int i=1;i<=m;++i)
	if (used[i])
	{
	    for (int j=1;j<=n;++j) rt[j]=j;
	    for (int j=1;j<=m;++j)
		if (used[j]&&find(X[j])!=find(Y[j])&&j!=i)
		    unionn(find(X[j]),find(Y[j]));
	    for (int j=1;j<=m;++j)
		if (Z[j]<=d&&!used[j])
		{
		    if (Z[i]==Z[j]) add(i,j);
		    if (find(X[j])!=find(Y[j])) add(j,i);
		}
	}
    return;
}
bool find_rd()
{
    int top;
    queue<int>q;
    for (int i=s;i<=t;++i) vis[i]=0;
    q.push(s),vis[s]=1;
    while (!q.empty())
    {
	top=q.front(),q.pop();
	for (int i=0;i<E[top].size();++i)
	    if (!vis[E[top][i]])
	    {
		vis[E[top][i]]=1,pv[E[top][i]]=top,q.push(E[top][i]);
		if (E[top][i]==t) return 1;
	    }
    }
    return 0;
}
void solve()
{
    int x=pv[t];
    while (x!=s) used[x]^=1,x=pv[x];
    return;
}
bool check(int x)
{
    d=x,add_edge();
    if (!find_rd()) return 0;
    solve();
    return 1;
}
int main()
{
    n=read(),m=read(),s=0,t=m+1;
    for (int i=1;i<=m;++i) X[i]=read(),Y[i]=read(),Z[i]=read();
    for (int i=0;i<=m;++i)
	if (!check(i))
	{
	    printf("%d\n",i);
	    return 0;
	}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3620kb

input:

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

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: -100
Wrong Answer
time: 902ms
memory: 5816kb

input:

1000 1000
647 790 6
91 461 435
90 72 74
403 81 240
893 925 395
817 345 136
88 71 821
831 962 53
164 270 298
14 550 317
99 580 81
26 477 488
977 474 861
413 483 167
872 675 17
819 327 449
594 242 68
381 983 319
867 582 358
869 225 669
274 352 392
40 388 998
246 477 44
508 979 286
483 776 71
580 438 6...

output:

704

result:

wrong answer 1st numbers differ - expected: '502', found: '704'