답안 #64071

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
64071 2018-08-03T10:01:29 Z Bodo171 Min-max tree (BOI18_minmaxtree) C++14
7 / 100
1000 ms 18888 KB
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=70005;
vector<int> v[nmax],ad[nmax];
pair<int,int> st[nmax],dr[nmax];
bool viz[nmax];
int l[nmax],r[nmax];
int rmq[20][2*nmax];
int tt[nmax],mark[nmax],first[nmax],lev[nmax];
int n,m,i,j,L,val,a,b,nr,niv,lg[nmax];
char ch;
bool changed=0;
int minlev(int A,int B)
{
    if(lev[A]<lev[B]) return A;
    return B;
}
void build_rmq()
{
    for(i=2;i<=nr;i++)
        lg[i]=lg[i/2]+1;
    for(i=1;(1<<i)<=nr;i++)
        for(j=1;j<=nr-(1<<i)+1;j++)
           rmq[i][j]=minlev(rmq[i-1][j],rmq[i-1][j+(1<<(i-1))]);
}
void dfs(int x)
{
    rmq[0][++nr]=x;first[x]=nr;
    for(int i=0;i<v[x].size();i++)
       if(!tt[v[x][i]])
    {
        lev[v[x][i]]=lev[x]+1;
        tt[v[x][i]]=x;
        dfs(v[x][i]);
        rmq[0][++nr]=x;
    }
}
int lca(int unu,int doi)
{
    unu=first[unu];doi=first[doi];
    if(unu>doi) swap(unu,doi);
    niv=lg[doi-unu+1];
    return minlev(rmq[niv][unu],rmq[niv][doi-(1<<niv)+1]);
}
void set_mn(int x,int y)
{
    while(x!=L)
    {
        if(val>st[x].first)
            st[x]={val,i};
        x=tt[x];
    }
    while(y!=L)
    {
        if(val>st[y].first)
         st[y]={val,i};
         y=tt[y];
    }
}
void set_mx(int x,int y)
{
    while(x!=L)
    {
        if(val<dr[x].first)
            dr[x]={val,i};
        x=tt[x];
    }
    while(y!=L)
    {
        if(val<dr[y].first)
           dr[y]={val,i};
        y=tt[y];
    }
}
bool cup(int x)
{
    if(viz[x]) return 0;
    viz[x]=1;
    for(int i=0;i<ad[x].size();i++)
        if(!r[ad[x][i]])
    {
        l[x]=ad[x][i];
        r[ad[x][i]]=x;
        return 1;
    }
    for(int i=0;i<ad[x].size();i++)
        if(cup(r[ad[x][i]]))
    {
        l[x]=ad[x][i];
        r[ad[x][i]]=x;
        return 1;
    }
    return 0;
}

int main()
{
   // freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    tt[1]=-1;
    dfs(1);
    for(i=1;i<=n;i++)
       st[i]={-1,0},dr[i]={(1<<30),0};
    build_rmq();
    cin>>m;
    for(i=1;i<=m;i++)
    {
        cin>>ch>>a>>b>>val;
        L=lca(a,b);
        if(ch=='m') set_mn(a,b);
        else set_mx(a,b);
    }
    for(i=1;i<=n;i++)
    {
        if(st[i].second) ad[i].push_back(st[i].second);
        if(dr[i].second) ad[i].push_back(dr[i].second);
    }
    changed=1;
    while(changed)
    {
        changed=0;
        for(i=1;i<=n;i++)
            viz[i]=0;
        for(i=1;i<=n;i++)
            if((!l[i])&&cup(i))
              changed=1;
    }
    for(i=2;i<=n;i++)
    {
        if(!l[i])
        {
            if(st[i].second) val=st[i].first;
            if(dr[i].second) val=dr[i].first;
            cout<<i<<' '<<tt[i]<<' '<<val<<'\n';
        }
        else
        {
            if(l[i]==st[i].second) val=st[i].first;
            else val=dr[i].first;
            cout<<i<<' '<<tt[i]<<' '<<val<<'\n';
        }
    }
    return 0;
}

Compilation message

minmaxtree.cpp: In function 'void dfs(int)':
minmaxtree.cpp:31:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<v[x].size();i++)
                 ~^~~~~~~~~~~~
minmaxtree.cpp: In function 'void set_mn(int, int)':
minmaxtree.cpp:57:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
         if(val>st[y].first)
         ^~
minmaxtree.cpp:59:10: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
          y=tt[y];
          ^
minmaxtree.cpp: In function 'bool cup(int)':
minmaxtree.cpp:81:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
minmaxtree.cpp:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<ad[x].size();i++)
                 ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3704 KB Output is correct
2 Correct 5 ms 3704 KB Output is correct
3 Correct 7 ms 3760 KB Output is correct
4 Correct 7 ms 3836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1079 ms 18888 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 18888 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3704 KB Output is correct
2 Correct 5 ms 3704 KB Output is correct
3 Correct 7 ms 3760 KB Output is correct
4 Correct 7 ms 3836 KB Output is correct
5 Execution timed out 1079 ms 18888 KB Time limit exceeded
6 Halted 0 ms 0 KB -