Submission #877198

# Submission time Handle Problem Language Result Execution time Memory
877198 2023-11-23T03:21:23 Z hmuhmuhmu Rigged Roads (NOI19_riggedroads) C++14
100 / 100
438 ms 68600 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ldb long double
#define db double
#define fin(x) freopen(x,"r",stdin)
#define fout(x) freopen(x,"w",stdout)
#define fo(i,l,r) for(int i=(l);i<=(r);i++)
#define foi(i,l,r) for(int i=(l);i>=(r);i--)
#define el cout<<'\n';
#define cel cerr<<'\n';
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define gb(x,i) (((x)>>(i))&1)
#define mask(i) (1LL<<(i))
using namespace std;
const int N=3e5+5;
const int bl=60;
const ll base=1e9+7;
const ll inf=1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l,ll r)
{
    return l+(1ll*rng()*rng()%(r-l+1)+(r-l+1))%(r-l+1);
}
template<class X,class Y>bool maximize(X &a,Y b)
{
    if(a<b) return a=b,true;
    return false;
}
template<class X,class Y>bool minimize(X &a, Y b)
{
    if(a>b) return a=b,true;
    return false;
}
void add(auto &a,auto b)
{
    a+=b;
    if(a>=base) a-=base;
    if(a<0) a+=base;
}
int n,m,spe[N],pa[N],ans[N];
int sz[N],run,pos[N];
int head[N],h[N];
vector<ii>eg;
vector<int>g[N];
vector<int>need;
void dfs(int u,int p)
{
    sz[u]=1;
    int st=0,mx=0;
    fo(i,0,(int)g[u].size()-1)
    {
        int v=g[u][i];
        if(v==p) continue;
        pa[v]=u,dfs(v,u),sz[u]+=sz[v];
        if(maximize(mx,sz[v])) st=i;
    }
    swap(g[u][st],g[u][0]);
}
void hld(int u)
{
    run++,pos[u]=run;
    for(int v:g[u]) if(pa[v]==u)
    {
        if(2*sz[v]>=sz[u]) head[v]=head[u],h[v]=h[u];
        else head[v]=v,h[v]=h[u]+1;
        hld(v);
    }
}

int num[4*N],st[4*N],lazy[4*N],er[4*N];
void build(int id,int l,int r)
{
    lazy[id]=st[id]=1e9,num[id]=r-l+1;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(id<<1,l,mid),build(id<<1|1,mid+1,r);
}
void push(int id)
{
    fo(i,id<<1,id<<1|1)
    {
        if(er[id]) er[i]=1,num[i]=0;
        minimize(st[i],lazy[id]),minimize(lazy[i],lazy[id]);
    }
}
void update(int id,int l,int r,int u,int v,int pos)
{
    if(r<u or l>v) return;
    if(u<=l and r<=v)
    {
        num[id]=0,er[id]=1;
        minimize(st[id],pos),minimize(lazy[id],pos);
        return;
    }
    int mid=(l+r)>>1;
    push(id);
    update(id<<1,l,mid,u,v,pos),update(id<<1|1,mid+1,r,u,v,pos);
    num[id]=num[id<<1]+num[id<<1|1];
}
int sum(int id,int l,int r,int u,int v)
{
    if(r<u or l>v) return 0;
    if(u<=l and r<=v) return num[id];
    int mid=(l+r)>>1;
    push(id);
    return sum(id<<1,l,mid,u,v)+sum(id<<1|1,mid+1,r,u,v);
}
int get(int id,int l,int r,int pos)
{
    if(l==r) return st[id];
    int mid=(l+r)>>1;
    push(id);
    if(pos<=mid) return get(id<<1,l,mid,pos);
    return get(id<<1|1,mid+1,r,pos);
}
int getchain(int &u,int id)
{
    int x=sum(1,1,n,pos[head[u]],pos[u]);
    update(1,1,n,pos[head[u]],pos[u],id);
    u=pa[head[u]];
    return x;
}
int getedge(int u,int v,int id)
{
    int kq=0;
    if(h[u]<h[v]) swap(u,v);
    while(h[u]>h[v]) kq+=getchain(u,id);
    while(head[u]!=head[v]) kq+=getchain(u,id)+getchain(v,id);
    if(pos[u]>pos[v]) swap(u,v);
    if(pos[u]<pos[v]) kq+=sum(1,1,n,pos[u]+1,pos[v]),update(1,1,n,pos[u]+1,pos[v],id);
    return kq;
}
vector<ii>cc;
int main()
{
    #define task "ZONING"
    if(fopen(task".inp","r"))
    {
        fin(task".inp");
        fout(task".out");
    }
    srand(time(NULL));
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    fo(i,0,m-1)
    {
        int u,v;
        cin>>u>>v,eg.pb({u,v});
    }
    need.resize(n-1);
    for(int &e:need)
    {
        cin>>e,e--,spe[e]=1;
        auto [u,v]=eg[e];
        g[u].pb(v),g[v].pb(u);
    }
    dfs(1,0);
    head[1]=h[1]=1,hld(1);
    build(1,1,n);
    int run=0;
    memset(ans,-1,sizeof(ans));
    fo(i,0,m-1)
    {
        auto &[u,v]=eg[i];
        if(sz[u]<sz[v]) swap(u,v);
        if(spe[i])
        {
            int x=sum(1,1,n,pos[v],pos[v]);
            if(x) run++,ans[i]=run,update(1,1,n,pos[v],pos[v],-1);
        }
        else
        {
            int x=getedge(u,v,i);
            run+=x+1,ans[i]=run;
        }
    }
    memset(spe,0,sizeof(spe));
    fo(i,0,m-1) if(!~ans[i])
    {
        auto [u,v]=eg[i];
        cc.pb({i,get(1,1,n,pos[v])});
    }
    sort(all(cc),[](ii x,ii y)
    {
        if(x.s!=y.s) return x.s<y.s;
        return x.f<y.f;
    });
    fo(i,0,m-1) if(~ans[i]) spe[ans[i]]=1;
    run=1;
    for(auto [id,_]:cc)
    {
        while(spe[run]) run++;
        ans[id]=run,run++;
    }
    fo(i,0,m-1) cout<<ans[i]<<" ";
}

Compilation message

riggedroads.cpp:40:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   40 | void add(auto &a,auto b)
      |          ^~~~
riggedroads.cpp:40:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   40 | void add(auto &a,auto b)
      |                  ^~~~
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:160:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  160 |         auto [u,v]=eg[e];
      |              ^
riggedroads.cpp:170:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  170 |         auto &[u,v]=eg[i];
      |               ^
riggedroads.cpp:186:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  186 |         auto [u,v]=eg[i];
      |              ^
riggedroads.cpp:196:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  196 |     for(auto [id,_]:cc)
      |              ^
riggedroads.cpp:9:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define fin(x) freopen(x,"r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~
riggedroads.cpp:145:9: note: in expansion of macro 'fin'
  145 |         fin(task".inp");
      |         ^~~
riggedroads.cpp:10:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 | #define fout(x) freopen(x,"w",stdout)
      |                 ~~~~~~~^~~~~~~~~~~~~~
riggedroads.cpp:146:9: note: in expansion of macro 'fout'
  146 |         fout(task".out");
      |         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21852 KB Output is correct
2 Correct 3 ms 21852 KB Output is correct
3 Correct 3 ms 21852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21852 KB Output is correct
2 Correct 3 ms 21852 KB Output is correct
3 Correct 3 ms 21852 KB Output is correct
4 Correct 4 ms 22104 KB Output is correct
5 Correct 4 ms 21952 KB Output is correct
6 Correct 4 ms 22108 KB Output is correct
7 Correct 4 ms 21852 KB Output is correct
8 Correct 5 ms 22108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 31168 KB Output is correct
2 Correct 182 ms 36436 KB Output is correct
3 Correct 263 ms 32668 KB Output is correct
4 Correct 241 ms 52156 KB Output is correct
5 Correct 267 ms 52908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 43716 KB Output is correct
2 Correct 121 ms 33992 KB Output is correct
3 Correct 61 ms 28112 KB Output is correct
4 Correct 121 ms 38420 KB Output is correct
5 Correct 35 ms 29476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 57228 KB Output is correct
2 Correct 254 ms 63572 KB Output is correct
3 Correct 59 ms 34928 KB Output is correct
4 Correct 91 ms 38604 KB Output is correct
5 Correct 320 ms 68600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 50792 KB Output is correct
2 Correct 107 ms 38844 KB Output is correct
3 Correct 370 ms 60144 KB Output is correct
4 Correct 318 ms 53852 KB Output is correct
5 Correct 26 ms 26072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 21852 KB Output is correct
2 Correct 3 ms 21852 KB Output is correct
3 Correct 3 ms 21852 KB Output is correct
4 Correct 4 ms 22104 KB Output is correct
5 Correct 4 ms 21952 KB Output is correct
6 Correct 4 ms 22108 KB Output is correct
7 Correct 4 ms 21852 KB Output is correct
8 Correct 5 ms 22108 KB Output is correct
9 Correct 86 ms 31168 KB Output is correct
10 Correct 182 ms 36436 KB Output is correct
11 Correct 263 ms 32668 KB Output is correct
12 Correct 241 ms 52156 KB Output is correct
13 Correct 267 ms 52908 KB Output is correct
14 Correct 145 ms 43716 KB Output is correct
15 Correct 121 ms 33992 KB Output is correct
16 Correct 61 ms 28112 KB Output is correct
17 Correct 121 ms 38420 KB Output is correct
18 Correct 35 ms 29476 KB Output is correct
19 Correct 236 ms 57228 KB Output is correct
20 Correct 254 ms 63572 KB Output is correct
21 Correct 59 ms 34928 KB Output is correct
22 Correct 91 ms 38604 KB Output is correct
23 Correct 320 ms 68600 KB Output is correct
24 Correct 177 ms 50792 KB Output is correct
25 Correct 107 ms 38844 KB Output is correct
26 Correct 370 ms 60144 KB Output is correct
27 Correct 318 ms 53852 KB Output is correct
28 Correct 26 ms 26072 KB Output is correct
29 Correct 438 ms 58556 KB Output is correct
30 Correct 438 ms 56560 KB Output is correct
31 Correct 388 ms 54976 KB Output is correct
32 Correct 301 ms 31508 KB Output is correct
33 Correct 377 ms 55840 KB Output is correct