QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105643#6402. MEXimum Spanning TreezhouhuanyiTL 1691ms6244kbC++112.1kb2023-05-14 16:56:262023-05-14 16:56:31

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 16:56:31]
  • 评测
  • 测评结果:TL
  • 用时:1691ms
  • 内存:6244kb
  • [2023-05-14 16:56:26]
  • 提交

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 (Z[i]<=d&&!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 (Z[i]<=d&&used[i])
	{
	    for (int j=1;j<=n;++j) rt[j]=j;
	    for (int j=1;j<=m;++j)
		if (Z[j]<=d&&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: 3676kb

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: 0
Accepted
time: 554ms
memory: 4952kb

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:

502

result:

ok 1 number(s): "502"

Test #3:

score: 0
Accepted
time: 1691ms
memory: 6244kb

input:

900 1000
232 890 107
425 399 19
5 74 753
105 333 163
779 42 582
359 647 524
767 409 48
239 780 443
484 489 546
97 634 562
627 866 714
500 357 590
60 728 591
407 686 210
547 32 370
76 772 500
407 584 772
73 699 69
332 847 516
829 754 727
562 756 678
819 303 128
781 667 263
535 672 767
89 762 216
878 ...

output:

801

result:

ok 1 number(s): "801"

Test #4:

score: 0
Accepted
time: 475ms
memory: 4992kb

input:

500 1000
381 118 230
258 331 21
403 71 207
170 2 125
467 99 6
369 100 492
70 187 352
99 163 123
135 51 352
461 175 486
275 194 236
299 14 19
16 1 68
7 229 316
235 433 320
411 179 463
112 329 326
464 169 52
377 93 51
84 336 335
42 240 379
182 496 344
377 481 195
88 286 491
199 425 165
37 292 44
197 2...

output:

403

result:

ok 1 number(s): "403"

Test #5:

score: -100
Time Limit Exceeded

input:

900 1000
698 454 775
6 762 755
585 346 86
220 245 253
54 634 184
634 249 234
454 363 546
520 799 501
103 346 134
381 346 792
835 782 614
359 220 485
634 68 54
411 220 439
701 364 791
220 876 15
70 346 317
220 461 769
577 431 117
488 107 706
160 39 864
220 172 721
431 400 556
801 364 716
37 845 83
99...

output:


result: