Submission #591767

# Submission time Handle Problem Language Result Execution time Memory
591767 2022-07-07T20:58:46 Z alirezasamimi100 Simurgh (IOI17_simurgh) C++17
32 / 100
130 ms 6828 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
const int N = 5e2 + 10, M = N * N / 2;

int d[M],p[N],f[N],h[N],pe[N],mt,P[N];
vector<pair<int,int>> adj[N];
vector<int> rt,V,U,ed[N];

int gp(int x){
    return P[x]==-1?x:P[x]=gp(P[x]);
}

bool uni(int v, int u){
    v=gp(v);
    u=gp(u);
    if(v==u) return 0;
    P[u]=v;
    return 1;
}

void dfs(int v){
    f[v]=1;
    for(auto [u,i] : adj[v]){
        if(f[u]) continue;
        h[u]=h[v]+1;
        p[u]=v;
        pe[u]=i;
        rt.pb(i);
        dfs(u);
    }
    if(!v) return;
}
pair<int,int> sfd(int v){
    int bk=-1,bu=-1;
    for(auto [u,i] : adj[v]){
        if(h[u]<h[v]-1 && (bu==-1 || h[u]<h[bu])){
            bk=i;
            bu=u;
        }else if(p[u]==v){
            pair<int,int> pi=sfd(u);
            if(pi.F!=-1 && (bu==-1 || h[pi.S]<h[bu])){
                bu=pi.S;
                bk=pi.F;
            }
        }
    }
    pair<int,int> pi={bk,bu};
    if(v==0) return pi;
    if(bk==-1){
        d[pe[v]]=1;
        return pi;
    }
    int bv=V[bk]^U[bk]^bu;
    if(d[pe[v]]!=-1) return pi;
    vector<int> v1,v2,t;
    while(bv!=bu){
        v1.pb(pe[bv]);
        bv=p[bv];
    }
    for(int i : v1){
        if(d[i]!=-1){
            if(d[bk]==-1){
                t={bk};
                for(int j : rt) if(j!=i) t.pb(j);
                int k=count_common_roads(t);
                if(k==mt) d[bk]=d[i];
                else d[bk]=1-d[i];
            }
            v2.pb(0);
        }else{
            t={bk};
            for(int j : rt) if(j!=i) t.pb(j);
            int k=count_common_roads(t);
            if(k==mt){
                v2.pb(0);
            }else{
                v2.pb(1);
                if(k>mt) d[bk]=1;
                else d[bk]=0;
            }
        }
    }
    if(d[bk]==-1) d[bk]=0;
    for(int i=0; i<(int)v2.size(); i++){
        if(d[v1[i]]!=-1) continue;
        if(v2[i]) d[v1[i]]=1-d[bk];
        else d[v1[i]]=d[bk];
    }
    return pi;
}

int jask(vector<int> vec){
    memset(P,-1,sizeof P);
    vector<int> t;
    int ans=0;
    for(int i : vec){
        uni(V[i],U[i]);
        t.pb(i);
    }
    for(int i : rt){
        if(uni(V[i],U[i])){
            t.pb(i);
            ans-=d[i];
        }
    }
    return ans+count_common_roads(t);
}
void solve(vector<int> vec, int x){
    int k=vec.size();
    if(k==1){
        d[vec[0]]=x;
        return;
    }
    if(x==0){
        for(int i : vec) d[i]=0;
        return;
    }
    vector<int> ls,rs;
    for(int i=0; i<k; i++){
        if(i<k/2) ls.pb(vec[i]);
        else rs.pb(vec[i]);
    }
    int y=jask(ls);
    solve(ls,y);
    solve(rs,x-y);
}

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
    int m=v.size();
    V=v;
    U=u;
    memset(d,-1,sizeof d);
    for(int i=0; i<m; i++){
        adj[u[i]].pb({v[i],i});
        adj[v[i]].pb({u[i],i});
        ed[abs(V[i]-U[i])].pb(i);
    }
    p[0]=-1;
    dfs(0);
    mt=count_common_roads(rt);
    sfd(0);
    for(int i=1; i<n; i++){
        if(ed[i].empty()) continue;
        solve(ed[i],jask(ed[i]));
    }
    vector<int> ans;
    for(int i=0; i<m; i++) if(d[i]==1) ans.pb(i);
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 724 KB correct
2 Correct 1 ms 852 KB correct
3 Correct 0 ms 724 KB correct
4 Correct 0 ms 724 KB correct
5 Correct 0 ms 724 KB correct
6 Correct 1 ms 724 KB correct
7 Correct 1 ms 852 KB correct
8 Correct 1 ms 852 KB correct
9 Correct 1 ms 848 KB correct
10 Correct 1 ms 852 KB correct
11 Correct 1 ms 852 KB correct
12 Correct 1 ms 852 KB correct
13 Correct 1 ms 852 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 724 KB correct
2 Correct 1 ms 852 KB correct
3 Correct 0 ms 724 KB correct
4 Correct 0 ms 724 KB correct
5 Correct 0 ms 724 KB correct
6 Correct 1 ms 724 KB correct
7 Correct 1 ms 852 KB correct
8 Correct 1 ms 852 KB correct
9 Correct 1 ms 848 KB correct
10 Correct 1 ms 852 KB correct
11 Correct 1 ms 852 KB correct
12 Correct 1 ms 852 KB correct
13 Correct 1 ms 852 KB correct
14 Correct 1 ms 852 KB correct
15 Correct 2 ms 852 KB correct
16 Correct 2 ms 852 KB correct
17 Correct 2 ms 852 KB correct
18 Correct 1 ms 852 KB correct
19 Correct 1 ms 852 KB correct
20 Correct 2 ms 980 KB correct
21 Correct 1 ms 852 KB correct
22 Incorrect 1 ms 852 KB WA in grader: NO
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 724 KB correct
2 Correct 1 ms 852 KB correct
3 Correct 0 ms 724 KB correct
4 Correct 0 ms 724 KB correct
5 Correct 0 ms 724 KB correct
6 Correct 1 ms 724 KB correct
7 Correct 1 ms 852 KB correct
8 Correct 1 ms 852 KB correct
9 Correct 1 ms 848 KB correct
10 Correct 1 ms 852 KB correct
11 Correct 1 ms 852 KB correct
12 Correct 1 ms 852 KB correct
13 Correct 1 ms 852 KB correct
14 Correct 1 ms 852 KB correct
15 Correct 2 ms 852 KB correct
16 Correct 2 ms 852 KB correct
17 Correct 2 ms 852 KB correct
18 Correct 1 ms 852 KB correct
19 Correct 1 ms 852 KB correct
20 Correct 2 ms 980 KB correct
21 Correct 1 ms 852 KB correct
22 Incorrect 1 ms 852 KB WA in grader: NO
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 724 KB correct
2 Correct 1 ms 852 KB correct
3 Correct 74 ms 5072 KB correct
4 Correct 117 ms 6740 KB correct
5 Correct 125 ms 6812 KB correct
6 Correct 130 ms 6812 KB correct
7 Correct 129 ms 6796 KB correct
8 Correct 122 ms 6732 KB correct
9 Correct 118 ms 6688 KB correct
10 Correct 117 ms 6796 KB correct
11 Correct 116 ms 6800 KB correct
12 Correct 120 ms 6824 KB correct
13 Correct 0 ms 724 KB correct
14 Correct 129 ms 6828 KB correct
15 Correct 119 ms 6688 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 724 KB correct
2 Correct 1 ms 852 KB correct
3 Correct 0 ms 724 KB correct
4 Correct 0 ms 724 KB correct
5 Correct 0 ms 724 KB correct
6 Correct 1 ms 724 KB correct
7 Correct 1 ms 852 KB correct
8 Correct 1 ms 852 KB correct
9 Correct 1 ms 848 KB correct
10 Correct 1 ms 852 KB correct
11 Correct 1 ms 852 KB correct
12 Correct 1 ms 852 KB correct
13 Correct 1 ms 852 KB correct
14 Correct 1 ms 852 KB correct
15 Correct 2 ms 852 KB correct
16 Correct 2 ms 852 KB correct
17 Correct 2 ms 852 KB correct
18 Correct 1 ms 852 KB correct
19 Correct 1 ms 852 KB correct
20 Correct 2 ms 980 KB correct
21 Correct 1 ms 852 KB correct
22 Incorrect 1 ms 852 KB WA in grader: NO
23 Halted 0 ms 0 KB -