답안 #937236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937236 2024-03-03T16:34:35 Z sleepntsheep Rigged Roads (NOI19_riggedroads) C++17
58 / 100
2000 ms 31056 KB
#include<stdio.h>
int hi(int a,int b){return a>b?a:b;}
unsigned X=12345;int rand_(){return(X*=3)>>1;}
int(*compar)(int,int);
void sort(int*aa,int l,int r){
    while(l<r){int i=l,j=l,k=r,tmp,p=aa[l+rand_()%(r-l)];
        while(j<k)switch(compar(aa[j],p)){case 0:++j;break;case -1:tmp=aa[j],aa[j]=aa[i],aa[i]=tmp,++i,++j;break;case 1:--k,tmp=aa[j],aa[j]=aa[k],aa[k]=tmp;break;}
        sort(aa,l,i);l=k;
    }
}
#define N 300005


int ttt[N],n,m,h[N],e[N<<1][2],ne=1,y[N],id,pp[N],jj[N],ee[N],dd[N],w[N],tin[N],tout[N],timer=1;
void _l(int u,int v){e[++ne][0]=v,e[ne][1]=h[u],h[u]=ne;}

void __u(int*t,int p,int k){if(p<1)p=1; for(;p<N;p+=p&-p)t[p]+=k; }
int __q(int*t,int p){if(p<1)return 0;int z=0;for(;p;p-=p&-p)z+=t[p];return z;}
int __s(int*t,int k){int p=0,v=0,j=1<<20;for(;j>>=1;)if(p+j<N&&v+t[p+j]<k)v+=t[p+=j];return p+1;}

void doedge(int v,int k){
    __u(ttt,tin[v],k);
    __u(ttt,tout[v]+1,-k);
}

void dfs(int u,int p){
    tin[u]=timer++;
    for(int v,j=h[u];j;j=e[j][1])if((v=e[j][0])!=p&&y[j>>1]){
        dd[v]=dd[pp[v]=u]+1;
        ee[v]=j>>1;
        pp[v]=u;
        jj[v]=(dd[p]-dd[jj[u]]==dd[jj[u]]-dd[jj[jj[u]]])?jj[jj[u]]:u;
        dfs(v,u);
        doedge(v,1);
    }
    tout[u]=timer-1;
}

int qq(int u,int v){
    if(dd[u]>dd[v])return qq(v,u);
    while(dd[v]>dd[u])
        if(dd[jj[v]]>=dd[u]) v=jj[v];
        else v=pp[v];
    while(u-v)
        if(jj[v]-jj[u])u=jj[u],v=jj[v];
        else u=pp[u],v=pp[v];
    return u;
}

int nx(int u,int aa){
    int base=__q(ttt,tin[u]);
    while(dd[u]>dd[aa]&&__q(ttt,tin[pp[u]])==base){
        u=(dd[jj[u]]>dd[aa]&&__q(ttt,tin[jj[u]])==base)?jj[u]:pp[u];
    }
    return u;
}

int need[N],np,ord=1;

int c1(int i,int j){return ee[i]<ee[j]?-1:ee[i]>ee[j]?1:0;}

int main(){
    scanf("%d%d",&n,&m);
    for(int u,v,i=1;i<=m;++i)scanf("%d%d",&u,&v),_l(u,v),_l(v,u);
    for(int j,i=1;i<n;++i)scanf("%d",&j),y[j]=1;
    pp[1]=jj[1]=1;dfs(1,1);

    for(int j=1;j<=m;++j)if(!w[j]){
        int u=e[j*2][0],v=e[j*2+1][0];
        if(dd[v]+1==dd[u]){int t=u;u=v;v=t;}
        if(!y[j]){
            int lca=qq(u,v),aa[]={u,v};
            for(int kk=0;kk<2;++kk){
                int ii=nx(aa[kk],lca);
                while(dd[ii]>dd[lca])
                {
                    doedge(ii,-1),need[np++]=ii;
                    ii=nx(ii,lca);
                }
            }
            compar=c1;sort(need,0,np);
            for(int i=0;i<np;++i) w[ee[need[i]]]=ord++;
            np=0;
            w[j]=ord++;
        }else{
            w[j]=ord++;
            doedge(v,-1);
        }
    }
    for(int j=1;j<=m;++j)printf("%d ",w[j]);
}

Compilation message

riggedroads.cpp: In function 'int main()':
riggedroads.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     scanf("%d%d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
riggedroads.cpp:64:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     for(int u,v,i=1;i<=m;++i)scanf("%d%d",&u,&v),_l(u,v),_l(v,u);
      |                              ~~~~~^~~~~~~~~~~~~~
riggedroads.cpp:65:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     for(int j,i=1;i<n;++i)scanf("%d",&j),y[j]=1;
      |                           ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 14352 KB Output is correct
2 Correct 82 ms 15440 KB Output is correct
3 Correct 81 ms 17020 KB Output is correct
4 Correct 97 ms 18004 KB Output is correct
5 Correct 106 ms 18564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2060 ms 20564 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 26284 KB Output is correct
2 Correct 124 ms 31056 KB Output is correct
3 Correct 34 ms 17744 KB Output is correct
4 Correct 46 ms 19888 KB Output is correct
5 Correct 127 ms 29352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 23216 KB Output is correct
2 Correct 50 ms 20304 KB Output is correct
3 Correct 134 ms 25836 KB Output is correct
4 Correct 130 ms 24052 KB Output is correct
5 Correct 13 ms 11016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 3 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 38 ms 14352 KB Output is correct
10 Correct 82 ms 15440 KB Output is correct
11 Correct 81 ms 17020 KB Output is correct
12 Correct 97 ms 18004 KB Output is correct
13 Correct 106 ms 18564 KB Output is correct
14 Execution timed out 2060 ms 20564 KB Time limit exceeded
15 Halted 0 ms 0 KB -