QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#232974#6402. MEXimum Spanning Treeucup-team134WA 3ms4216kbC++145.5kb2023-10-31 06:41:542023-10-31 06:41:55

Judging History

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

  • [2023-10-31 06:41:55]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:4216kb
  • [2023-10-31 06:41:54]
  • 提交

answer

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define ll long long
using namespace std;
typedef pair<int,int> pii;

const int maxn=1e3+10;

struct dsu{

    int n;
    int cc;
    vector<int>p,sz;


    dsu(){
    }

    dsu(int n){
        this->n=n;
        this->cc=n;
        p.resize(n+1);
        sz.resize(n+1);
        for(int i=1;i<=n;i++){
            p[i]=i;
            sz[i]=1;
        }
    }

    void reset(){
        for(int i=1;i<=n;i++){
            p[i]=i;
            sz[i]=1;
        }
        cc=n;
    }
    int root(int x){
        if(p[x]==x)return x;
        return p[x]=root(p[x]);
    }
    bool join(int u,int v){
        u=root(u);
        v=root(v);
        if(u==v)return false;
        if(sz[u]>sz[v])swap(u,v);
        p[u]=v;
        sz[v]+=sz[u];
        cc--;
        return true;
    }

};

struct edge{
    int u,v,w;
    bool operator <(edge &b){
        return w<b.w;
    }
};
int n,m;
vector<edge>e;
struct gse{
    int index;
    bool in_s;
};
vector<gse>groundset;
vector<int>s;
vector<int>vect[maxn];
dsu d;
int color[maxn];

void prepare_colorful(){
    memset(color,0,sizeof(color));
    for(int i=0;i<s.size();i++)
        color[e[s[i]].w]++;
}
bool oracle_color_swap(int x,int y){
    color[e[x].w]--;
    bool ret=false;
    if(color[e[y].w]==0)ret=true;
    color[e[x].w]++;
    return ret;
}
bool oracle_color_add(int x){
    return color[e[x].w]^1;
}


int tmm=0;
int start[maxn],en[maxn];
void dfs(int x){
    start[x]=++tmm;
    for(int i=0;i<vect[x].size();i++){

    }
    en[x]=tmm;
}
void prepare_mst(){

    dsu pom(n);
    d=pom;

    for(int i=1;i<=n;i++)vect[i].clear();
    for(int i=0;i<s.size();i++){
        int id=s[i];
        vect[e[id].u].pb(e[id].v);
        vect[e[id].v].pb(e[id].u);

        d.join(e[id].u,e[id].v);
    }

    tmm=0;
    memset(start,0,sizeof(start));
    memset(en,0,sizeof(en));
    for(int i=1;i<=n;i++){
        if(start[i])continue;
        dfs(i);
    }
}
bool oracle_mst_swap(int x,int y){

    int u1,u2,v1,v2;
    u1=e[x].u;
    v1=e[x].v;
    u2=e[y].u;
    v2=e[y].v;

    if(d.root(u2)!=d.root(v2))return true;

    int pom1=(start[u1]<=start[u2] && start[u2]<=en[u1])+(start[u1]<=start[v2] && start[v2]<=en[u1]);
    int pom2=(start[v1]<=start[u2] && start[u2]<=en[v1])+(start[v1]<=start[v2] && start[v2]<=en[v1]);

    pom1=min(pom1,pom2);

    if(pom1==1)return true;
    return false;
}
bool oracle_mst_add(int x){
    int u2,v2;
    u2=e[x].u;
    v2=e[x].v;
    if(d.root(u2)!=d.root(v2))return true;
    return false;
}

vector<int>Y1,Y2;
int endpos[maxn];
bool augment(){

    prepare_mst();
    prepare_colorful();

    Y1.clear();
    Y2.clear();
    for(int i=0;i<groundset.size();i++){
        if(groundset[i].in_s)continue;
        if(oracle_mst_add(i))Y1.pb(i);
        if(oracle_color_add(i))Y2.pb(i);
    }

    queue<int>q;
    vector<int>p(groundset.size(),-1);
    memset(endpos,0,sizeof(endpos));
    for(int i=0;i<Y2.size();i++){
        endpos[Y2[i]]=1;
        //printf("%d Y2\n",Y2[i]);
    }
    int break_condition=0;
    for(int i=0;i<Y1.size();i++){
        int id=Y1[i];
        if(endpos[id])break_condition=1;
       // printf("%d Y1\n",Y1[i]);
        q.push(id);
        p[id]=-2;
    }

    while(q.size()){
        int x=q.front();
        q.pop();

        if(break_condition)break;

        if(groundset[x].in_s){

            /// mst

            for(int i=0;i<groundset.size();i++){
                if(groundset[i].in_s)continue;
                if(p[i]!=-1)continue;
                if(!oracle_mst_swap(x,i))continue;

                q.push(i);
                p[i]=x;
                if(endpos[i])break_condition=1;
                if(break_condition)break;
            }

        }
        else{

            for(int i=0;i<groundset.size();i++){
                if(!groundset[i].in_s)continue;
                if(p[i]!=-1)continue;
                if(!oracle_color_swap(i,x))continue;

                q.push(i);
                p[i]=x;
                if(endpos[i])break_condition=1;
                if(break_condition)break;
            }
        }
    }

    int id=-1;
    for(int i=0;i<Y2.size();i++){
        if(p[Y2[i]]!=-1){
            id=Y2[i];
            break;
        }
    }
    if(id==-1)return false;

    vector<int>path;
    while(id!=-2){
        //printf("%d AAA\n",id);
        path.pb(id);
        id=p[id];
    }


    for(int i=0;i<path.size();i++)
        groundset[path[i]].in_s^=1;

    s.clear();
    for(int i=0;i<groundset.size();i++)
        if(groundset[i].in_s)s.pb(i);

    return true;
}

int main(){


    ///freopen("test.txt","r",stdin);

    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        e.pb({u,v,w});
    }
    sort(e.begin(),e.end());

    int r=0;
    int rez=0;
    while(r<e.size()){
        int w=e[r].w;
        if(rez!=w)break;
        while(r<e.size() && e[r].w==w){
            groundset.pb({r,false});
            //vect[e[r].u].pb(e[r].v);
            //vect[e[r].v].pb(e[r].u);
            r++;
        }
        if(!augment())break;
        //printf("AUG %d %d\n",r,s.size());
        //for(int i=0;i<s.size();i++){
            //printf("%d %d SS\n",s[i],groundset[s[i]].in_s);
        //}
        rez++;
    }

    printf("%d\n",rez);

    return 0;
}

詳細信息

Test #1:

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

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: 3ms
memory: 4216kb

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: -100
Wrong Answer
time: 2ms
memory: 3936kb

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:

711

result:

wrong answer 1st numbers differ - expected: '801', found: '711'