답안 #936129

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
936129 2024-03-01T07:53:51 Z fdnfksd Rigged Roads (NOI19_riggedroads) C++14
100 / 100
430 ms 102304 KB
#include<bits/stdc++.h>
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxn=3e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
bool ins[maxn];
ll lab[maxn],top[maxn],P[maxn][20];
ll findset(ll x)
{
    return lab[x]<0?x:lab[x]=findset(lab[x]);
}
vector<pli>g[maxn];
ll h[maxn];
void unite(ll u,ll v)
{
    u=findset(u);
    v=findset(v);
    if(u==v) return;
    if(lab[u]>lab[v]) swap(u,v);
    lab[u]+=lab[v];
    lab[v]=u;
    if(h[top[u]]>h[top[v]]) top[u]=top[v];
}
ll idx[maxn];
void dfs(ll u,ll p)
{
    P[u][0]=p;
    for(int i=1;i<=18;i++) P[u][i]=P[P[u][i-1]][i-1];
    for(auto [v,id]:g[u])
    {
        if(v!=p)
        {
            h[v]=h[u]+1;
            idx[v]=id;
            dfs(v,u);
        }
    }
}
void update(ll u)
{
    ins[u]=true;
    top[u]=u;
    for(auto [v,id]:g[u])
    {
        if(ins[v]==true) unite(u,v);
    }
}
ll par[maxn];
vector<pli>get(ll u,ll p)
{
    vector<pli>ans;
    while(h[u]>h[p])
    {
        if(ins[u]==true)
        {
            u=top[findset(u)];
            u=P[u][0];
        }
        else
        {
            ans.pb({u,idx[u]});
            u=P[u][0];
        }
    }
    return ans;
}
ll n,e,x[maxn],y[maxn];
bool in[maxn];
ll ans[maxn];
ll lca(ll u,ll v)
{
    if(h[u]<h[v]) swap(u,v);
    for(int i=18;i>=0;i--) if(h[u]-(1<<i)>=h[v]) u=P[u][i];
    if(u==v) return u;
    for(int i=18;i>=0;i--) if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i];
    return P[u][0];
}
bool cmp(pli x,pli y)
{
    return x.se<y.se;
}
void solve()
{
    cin >> n >> e;
    for(int i=1;i<=n;i++) lab[i]=-1;
    for(int i=1;i<=e;i++)
    {
        cin >> x[i] >> y[i];
        in[i]=false;
    }
    for(int i=1;i<n;i++)
    {
        ll id;
        cin >> id;
        in[id]=true;
        g[x[id]].pb({y[id],id});
        g[y[id]].pb({x[id],id});
    }
    dfs(1,0);
    ll dien=0;
    for(int i=1;i<=e;i++)
    {
        if(ans[i]>0) continue;
        if(in[i]==false)
        {
            ll u=x[i];
            ll v=y[i];
            ll c=lca(u,v);
            vector<pli>cc=get(u,c);
            vector<pli>vc=get(v,c);
            for(auto zz:vc) cc.pb(zz);
            sort(cc.begin(),cc.end(),cmp);
            for(auto zz:cc)
            {
                update(zz.fi);
                ans[zz.se]=++dien;
            }
            ans[i]=++dien;
        }
        else
        {
            ll u=x[i];
            ll v=y[i];
            if(h[u]<h[v]) swap(u,v);
            update(u);
            ans[i]=++dien;
        }
    }
    for(int i=1;i<=e;i++)
    {
        cout << ans[i]<<' ';
    }
}
int main()
{
    fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}

Compilation message

riggedroads.cpp: In function 'void dfs(ll, ll)':
riggedroads.cpp:35:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   35 |     for(auto [v,id]:g[u])
      |              ^
riggedroads.cpp: In function 'void update(ll)':
riggedroads.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   49 |     for(auto [v,id]:g[u])
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 22872 KB Output is correct
2 Correct 4 ms 23028 KB Output is correct
3 Correct 5 ms 23024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 22872 KB Output is correct
2 Correct 4 ms 23028 KB Output is correct
3 Correct 5 ms 23024 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 4 ms 23132 KB Output is correct
6 Correct 4 ms 23384 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 4 ms 22876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 46020 KB Output is correct
2 Correct 106 ms 55184 KB Output is correct
3 Correct 85 ms 32968 KB Output is correct
4 Correct 150 ms 88700 KB Output is correct
5 Correct 137 ms 89012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 55656 KB Output is correct
2 Correct 57 ms 37496 KB Output is correct
3 Correct 31 ms 30244 KB Output is correct
4 Correct 73 ms 47684 KB Output is correct
5 Correct 25 ms 33412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 88124 KB Output is correct
2 Correct 234 ms 98116 KB Output is correct
3 Correct 48 ms 44068 KB Output is correct
4 Correct 70 ms 52492 KB Output is correct
5 Correct 262 ms 102304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 182 ms 67748 KB Output is correct
2 Correct 115 ms 54344 KB Output is correct
3 Correct 368 ms 93312 KB Output is correct
4 Correct 316 ms 88920 KB Output is correct
5 Correct 19 ms 31068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 22872 KB Output is correct
2 Correct 4 ms 23028 KB Output is correct
3 Correct 5 ms 23024 KB Output is correct
4 Correct 5 ms 22876 KB Output is correct
5 Correct 4 ms 23132 KB Output is correct
6 Correct 4 ms 23384 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 4 ms 22876 KB Output is correct
9 Correct 48 ms 46020 KB Output is correct
10 Correct 106 ms 55184 KB Output is correct
11 Correct 85 ms 32968 KB Output is correct
12 Correct 150 ms 88700 KB Output is correct
13 Correct 137 ms 89012 KB Output is correct
14 Correct 104 ms 55656 KB Output is correct
15 Correct 57 ms 37496 KB Output is correct
16 Correct 31 ms 30244 KB Output is correct
17 Correct 73 ms 47684 KB Output is correct
18 Correct 25 ms 33412 KB Output is correct
19 Correct 189 ms 88124 KB Output is correct
20 Correct 234 ms 98116 KB Output is correct
21 Correct 48 ms 44068 KB Output is correct
22 Correct 70 ms 52492 KB Output is correct
23 Correct 262 ms 102304 KB Output is correct
24 Correct 182 ms 67748 KB Output is correct
25 Correct 115 ms 54344 KB Output is correct
26 Correct 368 ms 93312 KB Output is correct
27 Correct 316 ms 88920 KB Output is correct
28 Correct 19 ms 31068 KB Output is correct
29 Correct 420 ms 84920 KB Output is correct
30 Correct 430 ms 88740 KB Output is correct
31 Correct 362 ms 88400 KB Output is correct
32 Correct 111 ms 33292 KB Output is correct
33 Correct 324 ms 88968 KB Output is correct