QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#234398#2269. To be Connected, or not to be, that is the QuestionNYCU_CartesianTree#WA 41ms11812kbC++202.2kb2023-11-01 16:54:202023-11-01 16:54:20

Judging History

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

  • [2023-11-01 16:54:20]
  • 评测
  • 测评结果:WA
  • 用时:41ms
  • 内存:11812kb
  • [2023-11-01 16:54:20]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define F first
#define S second


vector<int>node[100005];
int dsu[100005],a[100005];
int num=0;
int n,m;
int p1[100005],p2[100005],kuai[100005];

void init(){
    for(int i=1;i<=n;i++) dsu[i]=i;
    num=0;
}

int find(int g){
    if(g==dsu[g]) return g;
    dsu[g]=find(dsu[g]);
    return dsu[g];
}

void un(int g,int h){
    g=find(g),h=find(h);
    // cerr<<g<<" "<<h<<"\n";
    if(g==h) return;
    dsu[g]=h;
    num--;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    init();

    vector<int>all;
    vector<pair<int,int>>gogo;
    for(int i=1;i<=n;i++){
        int g;
        cin>>g;
        a[i]=g;
        gogo.pb({g,i});
        all.pb(g);
    }

    sort(all.begin(),all.end());
    sort(gogo.begin(),gogo.end());
    all.resize(distance(all.begin(),unique(all.begin(),all.end())));

    for(int i=1;i<=m;i++){
        int g,h;
        cin>>g>>h;
        node[g].pb(h);
        node[h].pb(g);
    }
    if(all.size()==1){
        cout<<"-1\n";
        return 0;
    }

    int id=0;int now=0;
    for(int i=0;i<all.size();i++){
        int ii=all[i];
        while(id<gogo.size()&&gogo[id].F<=ii){
            num++;
            now++;
            for(int mm:node[gogo[id].S]){
                if(a[mm]<=ii){
                    // cerr<<all[i]<<" "<<mm<<" "<<gogo[id].S<<"\n";
                    un(mm,gogo[id].S);
                }
            }
            id++;
        }
        p1[i]=now;p2[i]=n-now;
        kuai[i]=num;
    }

    init();id=gogo.size()-1;
    for(int i=all.size()-2;i>=0;i--){
        int ii=all[i];
        while(id>=0&&gogo[id].F>ii){
            num++;
            for(int mm:node[gogo[id].S]){
                if(a[mm]>ii){
                    un(mm,gogo[id].S);
                }
            }
            id--;
        }
        kuai[i]+=num;
        // cerr<<num<<" "<<i<<"\n";
    }


    for(int i=0;i<all.size();i++){
        // cerr<<p1[i]<<" "<<p2[i]<<" "<<kuai[i]<<"\n";
        if(p1[i]>=kuai[i]-1&&p2[i]>=kuai[i]-1){
            cout<<all[i]<<"\n";
            return 0;
        }
    }

    cout<<"-1\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2 0
861866350 106689920

output:

106689920

result:

ok single line: '106689920'

Test #2:

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

input:

3 0
582295931 120217528 845887275

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 12ms
memory: 7860kb

input:

52033 0
432755200 936478974 298744051 31368207 847742874 81290408 425992405 651328821 903238557 526933479 356290128 722885083 779029575 480262946 443316470 542413465 170562283 440427743 438763956 784112617 255213130 899556824 323259505 857165776 533714690 565510843 859610987 686006833 211894364 9600...

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 17ms
memory: 9116kb

input:

100000 0
514837648 759500586 899265033 24085608 545610751 221779667 568755229 169602804 284396186 169538713 571993850 645890208 375601406 769765103 805781464 228017324 648075651 874669052 771742115 269678248 190757592 220852391 275602630 816966668 111244645 208546040 715493307 277760893 770626133 25...

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 41ms
memory: 11812kb

input:

100000 99999
299608910 294889223 380597480 583050141 733930013 271705935 623956575 293208851 168678637 517320846 970153874 376864085 620559158 384944405 959726556 311522848 233740144 852169507 874336822 670072232 182817184 163689537 962302870 278762094 916902553 742474244 377317908 941252256 1153825...

output:

500886962

result:

ok single line: '500886962'

Test #6:

score: -100
Wrong Answer
time: 30ms
memory: 10176kb

input:

74426 74425
502580802 844381862 660278137 133338847 482745545 247460402 538172402 808255530 40356010 108510584 849411507 316373292 792804254 963129923 254086752 499276056 480699103 83684034 315438237 930422686 782095189 819730693 122590837 465841667 953771312 705072601 968407044 458518835 349083576 ...

output:

999989041

result:

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