QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#604976#2946. Abridged ReadingxyyyWA 102ms4148kbC++142.3kb2024-10-02 14:52:012024-10-02 14:52:01

Judging History

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

  • [2024-10-02 14:52:01]
  • 评测
  • 测评结果:WA
  • 用时:102ms
  • 内存:4148kb
  • [2024-10-02 14:52:01]
  • 提交

answer

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
#define N 5000
#define M 2*N
#define int long long
vector<int>arr(N),sum(N);
int e[M],ne[M],h[N];
int depth[N];   
int fa[N][32];
int idx;
int q[N];
void add(int a,int b){
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void bfs(int root){
    for(int i=0;i<N;i++)depth[i]=1e16;
    depth[0]=0;
    depth[root]=1;
    int hh=0,tt=0;
    q[0]=root;
    while(hh<=tt){
        int t=q[hh++];
        for(int i=h[t];i!=-1;i=ne[i]){
            int j=e[i];
            if(depth[j]>depth[t]+1){
                depth[j]=depth[t]+1;
                q[++tt]=j;
                fa[j][0]=t;
                sum[j]=sum[t]+arr[j];
                for(int k=1;k<=31;k++){
                    fa[j][k]=fa[fa[j][k-1]][k-1];
                }
            }
        }
    }
}
int lca(int a,int b){
    if(depth[a]<depth[b])swap(a,b);
    for(int k=31;k>=0;k--){
        if(depth[fa[a][k]]>=depth[b]){
            a=fa[a][k];
        }
    }
    if(a==b)return a;
    for(int k=31;k>=0;k--){
        if(fa[a][k]!=fa[b][k]){
            a=fa[a][k];
            b=fa[b][k];
        }
    }
    return fa[a][0];
}
void solve(){
    int n, m;
    cin>>n>>m;
    memset(h,-1,sizeof h);
    vector<int>inde(5000),outde(5000);
    vector<int>op;
    for(int i=1;i<=n;i++)cin>>arr[i];
    for(int i=1;i<=m;i++){
        int a,b;
        cin>>a>>b;
        inde[b]++;
        outde[a]++;
        add(a,b);

    }
    int root=-1;
    for(int i=1;i<=n;i++){
        if(inde[i]==0){
            root=i;
            sum[root]=arr[root];
            bfs(root);
        }
        
    }
    for(int i=1;i<=n;i++){
        if(outde[i]==0){
            op.push_back(i);
        }
    }
    int ans=1e16;
    for(int i=0;i<op.size();i++){
        for(int j=i+1;j<op.size();j++){
            int a=op[i],b=op[j];
            int f=lca(a,b);
            int t=0;
            if(f==0){
                t=sum[a]+sum[b];
            }
            else{
                t=sum[a]+sum[b]-sum[f];
            }
            ans=min(ans,t);

        }
    }
    cout<<ans<<endl;

    

}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3792kb

input:

2 0
10 20

output:

30

result:

ok single line: '30'

Test #2:

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

input:

3 2
10 20 30
1 2
1 3

output:

60

result:

ok single line: '60'

Test #3:

score: 0
Accepted
time: 27ms
memory: 4096kb

input:

1000 999
589 300 181 203 350 691 812 963 333 957 894 681 630 24 774 397 768 215 403 868 995 248 156 160 528 940 862 965 889 535 939 782 453 935 641 733 864 681 364 225 661 198 305 778 17 901 408 208 134 555 316 57 128 821 307 882 519 302 5 421 194 348 491 912 820 156 797 254 374 70 175 95 497 315 17...

output:

1196

result:

ok single line: '1196'

Test #4:

score: 0
Accepted
time: 26ms
memory: 4092kb

input:

1000 999
400 323 358 806 874 176 813 905 306 813 441 195 671 190 924 733 252 22 892 289 561 545 821 490 830 87 255 104 998 956 935 832 211 316 476 435 484 260 777 133 8 932 618 189 485 800 548 509 570 54 662 783 373 601 406 463 394 36 937 160 368 835 371 486 185 139 255 947 311 898 443 581 990 105 4...

output:

1069

result:

ok single line: '1069'

Test #5:

score: 0
Accepted
time: 27ms
memory: 4100kb

input:

1000 999
432 313 769 817 653 951 97 56 224 152 283 857 315 377 289 578 124 748 599 926 535 19 722 684 533 13 669 783 166 646 546 961 951 488 106 824 423 320 25 298 724 291 961 501 437 552 449 791 419 943 169 836 425 910 269 982 309 741 92 79 914 303 492 234 891 689 147 695 614 799 596 501 363 570 47...

output:

1334

result:

ok single line: '1334'

Test #6:

score: 0
Accepted
time: 26ms
memory: 4100kb

input:

1000 999
599 62 219 707 844 743 690 882 240 569 198 276 812 26 213 688 353 880 154 474 891 927 892 472 869 158 559 496 674 484 978 557 813 56 564 280 350 318 683 602 218 53 59 743 981 61 615 815 359 702 727 823 88 403 518 628 305 254 694 519 345 677 882 852 696 498 80 429 348 184 570 515 277 177 104...

output:

1275

result:

ok single line: '1275'

Test #7:

score: 0
Accepted
time: 102ms
memory: 4108kb

input:

1000 0
376 610 318 581 85 805 725 305 931 572 790 288 642 350 740 398 716 222 577 544 750 766 176 549 221 989 670 17 870 591 951 911 534 943 997 458 384 291 699 496 917 780 152 60 773 937 512 638 3 599 388 745 327 23 640 569 918 300 805 287 52 279 229 891 393 785 716 381 192 628 31 136 156 918 844 4...

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 72ms
memory: 3992kb

input:

1000 196
543 842 44 237 511 790 282 553 481 697 246 362 659 655 607 222 51 380 743 468 834 601 643 659 824 711 697 398 753 151 538 525 188 851 134 779 996 485 415 872 911 962 326 34 331 786 250 909 833 758 79 564 706 518 682 810 831 623 147 570 973 62 187 623 644 36 173 49 776 949 721 309 72 286 339...

output:

3

result:

ok single line: '3'

Test #9:

score: 0
Accepted
time: 44ms
memory: 4076kb

input:

1000 419
608 725 693 959 612 919 561 776 862 856 761 638 640 703 798 798 520 986 774 927 688 796 623 523 631 966 614 608 887 971 856 616 846 850 747 686 809 881 718 530 960 908 906 729 887 634 605 807 999 732 700 794 561 921 593 796 504 626 872 820 938 860 636 622 736 738 907 590 871 961 642 827 940...

output:

1001

result:

ok single line: '1001'

Test #10:

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

input:

7 6
10 9 6 4 2 10 12
1 2
1 3
2 4
2 5
3 6
3 7

output:

25

result:

ok single line: '25'

Test #11:

score: 0
Accepted
time: 25ms
memory: 4148kb

input:

1000 885
543 757 983 811 867 900 922 917 530 553 507 548 962 644 773 888 800 558 627 750 913 580 862 941 522 976 707 923 914 564 883 827 901 754 762 753 806 703 942 854 951 761 633 602 777 623 730 964 855 611 903 865 800 940 503 549 705 861 715 708 949 571 937 633 995 903 812 894 778 936 748 839 581...

output:

1052

result:

ok single line: '1052'

Test #12:

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

input:

127 126
1
1 10
1 1 10 10
1 1 1 1 10 10 10 10
1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 ...

output:

71

result:

ok single line: '71'

Test #13:

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

input:

127 126
1
1 10
1 1 10 10
1 1 1 1 10 10 10 10
1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 1000 1000 1000 1000 1000
1000 1000 1000 ...

output:

71

result:

ok single line: '71'

Test #14:

score: -100
Wrong Answer
time: 1ms
memory: 3848kb

input:

128 126
22 1 44 35 9 88 37 54 37 6 75 17 4 32 78 25 15 95 1 13 78 99 39 97 100 51 13 60 19 56 70 27 24 63 80 78 100 3 92 78 94 3 5 6 84 2 94 90 14 100 81 58 90 61 25 96 30 82 16 42 37 87 30 47 8 21 1 44 54 60 10 78 2 61 14 59 1 19 66 66 5 20 9 1 91 22 55 26 14 92 95 62 85 87 67 2 11 63 36 39 27 83 4...

output:

220

result:

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