QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#490417#6560. Broken Minimum Spanning TreeSmallAoPigBigPiPig#WA 4ms4072kbC++172.6kb2024-07-25 15:06:252024-07-25 15:06:34

Judging History

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

  • [2024-07-25 15:06:34]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:4072kb
  • [2024-07-25 15:06:25]
  • 提交

answer

#include<bits/stdc++.h>

typedef long long ll;
typedef std::pair<int,int> pii;
const int MAXN = 6011;
struct edge{int v,w,nxt;}e[MAXN<<1|1];
int cnt=1,last[MAXN];
bool del[MAXN];
void adde(int u,int v,int w)
{
    e[++cnt].v=v,e[cnt].w=w, e[cnt].nxt=last[u],last[u]=cnt;
}

int n,m;
struct one
{
    int u,v,w,dex;
    bool operator< (const one& you)const
    {
        return w<you.w;
    }
}pre[MAXN],now[MAXN];
int fa[MAXN],fw[MAXN], dep[MAXN];
int exchange[MAXN];
void dfs(int u)
{
    //printf("dfs %d\n",u);
    for(int i=last[u];i;i=e[i].nxt)
    {
        if(del[i>>1])continue;
        int v=e[i].v;
        if(fa[u]==v)continue;
        fa[v]=u,fw[v]=i>>1, dep[v]=dep[u]+1;
        //printf("Fw[%d]=%d\n",v,fw[v]);
        dfs(v);
    }
}
bool less(int a,int b)
{
    //printf("Less %d %d ;%d %d\n",a,e[a<<1].w,b,e[b<<1].w);
    if(e[a<<1].w<e[b<<1].w)return 1;
    if(e[a<<1].w>e[b<<1].w)return 0;
    return (a<=n-1) && (b>n-1);
}
int get(int u,int v)
{
    //printf("Get (%d,%d)\n",u,v);
    if(dep[u]>dep[v])std::swap(u,v);
    int dex=0;
    while(dep[u]!=dep[v])
    {
        //printf("v=%d,fw=%d\n",v,fw[v]);
        if(less(dex,fw[v]))dex=fw[v];
        v=fa[v];
    }
    while(u!=v)
    {
        if(less(dex,fw[v]))dex=fw[v];
        v=fa[v];
        if(less(dex,fw[u]))dex=fw[u];
        u=fa[u];
    }
    //printf("dex=%d\n",dex);
    return dex;
}

void update()
{
    memset(dep,0,sizeof dep),memset(fa,0,sizeof fa),memset(fw,0,sizeof fw);
    dep[1]=1,dfs(1);
}
int pos[MAXN];
std::vector<pii>seq;
int main()
{
    scanf("%d%d",&n,&m);
    m-=n-1;
    for(int i=1;i<=n-1;++i)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        adde(u,v,w),adde(v,u,w);
        exchange[i]=i,pos[i]=i;
    }
    for(int i=1;i<=m;++i)scanf("%d%d%d",&now[i].u,&now[i].v,&now[i].w),now[i].dex=n-1+i;
    std::sort(now+1,now+m+1);
    int ans=0;
    update();
    for(int i=1;i<=m;++i)
    {
        pos[n-1+i]=now[i].dex;
        int w=now[i].w;
        int p=get(now[i].u,now[i].v);
        int pw=e[p<<1].w;
        //printf("i=%d,w=%d,p=%d,pw=%d\n",i,w,p,pw);
        if(pw>w)
        {
            del[p]=1;
            adde(now[i].u,now[i].v,w);
            adde(now[i].v,now[i].u,w);
            update();
            exchange[now[i].dex]=exchange[pos[p]];
            exchange[pos[p]]=0;
        }
        else cnt+=2;
    }
    for(int i=1;i<=m;++i)
        if(exchange[n-1+i])seq.emplace_back(pii(exchange[n-1+i],n-1+i));
    printf("%d\n",int(seq.size()));
    for(auto [x,y]:seq)printf("%d %d\n",x,y);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3976kb

input:

4 4
1 2 10
2 3 3
3 4 1
1 4 4

output:

1
1 4

result:

ok correct!

Test #2:

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

input:

6 8
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
1 3 1
4 6 1

output:

2
2 7
5 8

result:

ok correct!

Test #3:

score: 0
Accepted
time: 1ms
memory: 4072kb

input:

2000 1999
1262 1505 968661582
323 1681 787089412
1132 129 88786681
1909 587 762050278
979 1371 230688681
1686 521 980519364
975 191 887826021
869 461 899130441
1433 259 961154249
1718 547 721696188
1254 1042 458319755
1779 267 85751052
1170 813 283230029
309 20 971682908
224 417 255325364
1084 986 7...

output:

0

result:

ok correct!

Test #4:

score: 0
Accepted
time: 1ms
memory: 3948kb

input:

1999 1998
1757 1820 444157563
1757 395 754598547
1757 1571 432619009
1757 1009 456234067
1757 824 935569725
1757 1698 476714469
1757 1420 901765343
1757 1175 225295107
1757 1512 721959801
1757 1585 955067704
1757 1739 635181418
1757 1686 891225461
1757 84 132683224
1757 1696 48915557
1757 1623 42602...

output:

0

result:

ok correct!

Test #5:

score: 0
Accepted
time: 1ms
memory: 4044kb

input:

1999 1998
1345 647 232183406
40 837 279910457
819 857 137486924
255 1378 517489941
827 1565 894953662
1556 1545 898170464
965 877 72248541
1631 298 635713424
895 197 366305735
966 1160 515776809
1870 1638 220711661
1736 220 716014108
1914 1609 759121968
1293 153 272816132
1936 1433 263859075
985 460...

output:

0

result:

ok correct!

Test #6:

score: 0
Accepted
time: 1ms
memory: 3920kb

input:

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

output:

0

result:

ok correct!

Test #7:

score: 0
Accepted
time: 1ms
memory: 4000kb

input:

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

output:

0

result:

ok correct!

Test #8:

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

input:

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

output:

499
1 500
2 501
3 502
4 503
5 504
6 505
7 506
8 507
9 508
10 509
11 510
12 511
13 512
14 513
15 514
16 515
17 516
18 517
19 518
20 519
21 520
22 521
23 522
24 523
25 524
26 525
27 526
28 527
29 528
30 529
31 530
32 531
33 532
34 533
35 534
36 535
37 536
38 537
39 538
40 539
41 540
42 541
43 542
44 5...

result:

ok correct!

Test #9:

score: 0
Accepted
time: 1ms
memory: 4000kb

input:

500 998
198 227 1
227 315 1
315 426 1
426 400 1
400 61 1
61 143 1
143 487 1
487 65 1
65 415 1
415 434 1
434 327 1
327 190 1
190 411 1
411 51 1
51 91 1
91 364 1
364 185 1
185 393 1
393 89 1
89 53 1
53 66 1
66 69 1
69 13 1
13 5 1
5 45 1
45 314 1
314 291 1
291 490 1
490 92 1
92 175 1
175 458 1
458 218 ...

output:

0

result:

ok correct!

Test #10:

score: 0
Accepted
time: 1ms
memory: 4004kb

input:

500 998
360 250 1
250 71 1
71 170 1
170 492 1
492 419 1
419 145 1
145 188 1
188 433 1
433 186 1
186 161 1
161 398 1
398 19 1
19 479 1
479 401 1
401 40 1
40 176 1
176 212 1
212 353 1
353 290 1
290 43 1
43 322 1
322 447 1
447 47 1
47 468 1
468 456 1
456 343 1
343 339 1
339 52 1
52 251 1
251 130 1
130 ...

output:

0

result:

ok correct!

Test #11:

score: -100
Wrong Answer
time: 4ms
memory: 3968kb

input:

500 998
369 45 2
45 364 2
364 300 2
300 195 2
195 291 2
291 390 2
390 122 2
122 331 2
331 408 2
408 91 2
91 298 2
298 116 2
116 301 2
301 287 2
287 338 2
338 4 2
4 79 2
79 177 2
177 387 2
387 125 2
125 477 2
477 11 2
11 284 2
284 102 2
102 305 2
305 395 2
395 112 2
112 280 2
280 294 2
294 232 2
232 ...

output:

499
398 500
10 501
181 502
483 503
428 504
484 505
249 506
171 507
115 508
179 509
82 510
274 511
177 512
147 513
13 514
379 515
54 516
392 517
381 518
53 519
72 520
224 521
493 522
278 523
490 524
215 525
385 526
18 527
121 528
357 529
229 530
48 531
135 532
343 533
314 534
104 535
149 536
150 537
...

result:

wrong answer not a spanning tree after swap 3