Submission #372291

# Submission time Handle Problem Language Result Execution time Memory
372291 2021-02-27T14:30:08 Z doowey Simurgh (IOI17_simurgh) C++14
100 / 100
157 ms 12072 KB
#include <bits/stdc++.h>
#include "simurgh.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair

const int N = 500;
const int M = N * (N - 1) / 2;
vector<pii> T[N];
vector<pii> Q[N];
int dep[N];
pii low[N];
int in[N];

int edge[M];
int know[M];
vector<int> fq;

void dfs(int u, int pp){
    low[u] = mp(dep[u], -1);
    for(auto x : T[u]){
        if(x.fi == pp) continue;
        if(dep[x.fi] == -1){
            in[x.fi]=x.se;
            dep[x.fi]=dep[u]+1;
            dfs(x.fi, u);
            fq.push_back(x.se);
            low[u] = min(low[u], low[x.fi]);
            if(dep[u] < low[x.fi].fi){
                know[x.se] = 1;
                edge[x.se] = -1; // bridge
            }
            else{
                edge[x.se] = 0; //
            }
        }
        else{
            edge[x.se] = 2; // these f*ck everything up
            low[u] = min(low[u], mp(dep[x.fi], x.se));
        }
    }
}

vector<int> G[M];

int get(int ii, int jj){
    vector<int> aq;
    for(auto x : fq) if(x != ii) aq.push_back(x);
    aq.push_back(jj);
    return count_common_roads(aq);
}

vector<int> cyc;
bool vis[M];
void dfs1(int u){
    vis[u]=true;
    cyc.push_back(u);
    for(auto x : G[u]){
        if(!vis[x]){
            dfs1(x);
        }
    }
}

int par[N];
int fin_node(int x){
    if(par[x] == x) return x;
    return par[x]=fin_node(par[x]);
}

bool unite(int a, int b){
    a = fin_node(a);
    b = fin_node(b);
    if(a == b) return false;
    par[a] = b;
    return true;
}

int n;

vector<pii> saf;
vector<int>u,v;

int bins(int l, int r, int val){
    if(val == -1){
        vector<int> E;
        for(int i = 0 ; i < n; i ++ ){
            par[i]=i;
        }
        int rem = 0;
        for(int j = l ; j <= r; j ++ ){
            E.push_back(saf[j].se);
            unite(u[saf[j].se], v[saf[j].se]);
        }
        for(auto x : fq){
            if(unite(u[x], v[x])){
                rem += know[x];
                E.push_back(x);
            }
        }
        val = count_common_roads(E) - rem;
    }
    if(val == 0) return 0;
    if(l == r){
        know[saf[l].se]=1;
        return val;
    }
    int mid = (l + r) / 2;
    int lef = bins(l, mid, -1);
    bins(mid + 1, r, val - lef);
    return val;
}

vector<int> find_roads(int _n, vector<int> u_, vector<int> v_) {
    n = _n;
    u = u_;
    v = v_;
    int m = u.size();
    for(int i = 0 ; i < m ; i ++ ){
        T[u[i]].push_back(mp(v[i], i));
        T[v[i]].push_back(mp(u[i], i));
        edge[i] = -1;
        know[i]=-1;
    }
    for(int i = 0 ; i < n; i ++ ){
        dep[i] = -1;
    }
    dep[0]=0;
    dfs(0,-1);
    for(int i = 0 ; i < m ; i ++) {
        if(dep[u[i]] > dep[v[i]])
            swap(u[i], v[i]);
    }
    for(int i = 1; i < n; i ++ ){
        edge[low[i].se] = 1;
    }
    int faf = count_common_roads(fq);
    int nw;
    int uu, vv;
    int go;
    for(int i = 0 ; i < m ; i ++ ){
        if(edge[i] == 0){
            uu = i;
            vv = low[v[i]].se;
            nw = get(uu, vv);
            if(faf == nw){
                G[uu].push_back(vv);
                G[vv].push_back(uu);
            }
            else{
                if(nw > faf){
                    know[vv]=1;
                    know[uu]=0;
                }
                else{
                    know[vv]=0;
                    know[uu]=1;
                }
            }
        }
        else if(edge[i] == 1){
            vv = i;
            go = v[i];
            while(u[in[go]] != u[i]){
                go = u[in[go]];
            }
            uu = in[go];
            nw = get(uu, vv);
            if(faf == nw){
                G[uu].push_back(vv);
                G[vv].push_back(uu);
            }
            else{
                if(nw > faf){
                    know[vv]=1;
                    know[uu]=0;
                }
                else{
                    know[vv]=0;
                    know[uu]=1;
                }
            }
        }
    }
    int val;
    for(int i = 0 ; i < m ; i ++ ){
        if(edge[i] == 0 || edge[i] == 1){
            if(!vis[i]){
                cyc.clear();
                dfs1(i);
                val = -1;
                for(auto x : cyc){
                    if(know[x] != -1) val = know[x];
                }
                if(val == -1) val = 0;
                for(auto x : cyc) know[x] = val;
            }
        }
    }
    vector<int> soln;
    for(int i = 0 ; i < m ; i ++ ){
        if(edge[i] == 2){
            Q[u[i]].push_back(mp(v[i], i));
        }
    }
    int cnt;
    int cur;
    for(int i = 0 ; i < n; i ++ ){
        if(Q[i].empty()) continue;
        saf = Q[i];
        bins(0, (int)saf.size() - 1, -1);
    }
    for(int i = 0 ; i < m ; i ++ ){
        if(know[i] == 1) soln.push_back(i);
    }
	return soln;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:212:9: warning: unused variable 'cnt' [-Wunused-variable]
  212 |     int cnt;
      |         ^~~
simurgh.cpp:213:9: warning: unused variable 'cur' [-Wunused-variable]
  213 |     int cur;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3308 KB correct
2 Correct 3 ms 3308 KB correct
3 Correct 2 ms 3308 KB correct
4 Correct 2 ms 3308 KB correct
5 Correct 2 ms 3308 KB correct
6 Correct 2 ms 3308 KB correct
7 Correct 2 ms 3308 KB correct
8 Correct 2 ms 3308 KB correct
9 Correct 3 ms 3308 KB correct
10 Correct 2 ms 3308 KB correct
11 Correct 2 ms 3308 KB correct
12 Correct 3 ms 3308 KB correct
13 Correct 2 ms 3308 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3308 KB correct
2 Correct 3 ms 3308 KB correct
3 Correct 2 ms 3308 KB correct
4 Correct 2 ms 3308 KB correct
5 Correct 2 ms 3308 KB correct
6 Correct 2 ms 3308 KB correct
7 Correct 2 ms 3308 KB correct
8 Correct 2 ms 3308 KB correct
9 Correct 3 ms 3308 KB correct
10 Correct 2 ms 3308 KB correct
11 Correct 2 ms 3308 KB correct
12 Correct 3 ms 3308 KB correct
13 Correct 2 ms 3308 KB correct
14 Correct 4 ms 3436 KB correct
15 Correct 3 ms 3436 KB correct
16 Correct 3 ms 3436 KB correct
17 Correct 3 ms 3308 KB correct
18 Correct 3 ms 3308 KB correct
19 Correct 3 ms 3308 KB correct
20 Correct 3 ms 3308 KB correct
21 Correct 3 ms 3308 KB correct
22 Correct 3 ms 3328 KB correct
23 Correct 3 ms 3308 KB correct
24 Correct 3 ms 3308 KB correct
25 Correct 3 ms 3308 KB correct
26 Correct 3 ms 3308 KB correct
27 Correct 3 ms 3308 KB correct
28 Correct 3 ms 3308 KB correct
29 Correct 3 ms 3308 KB correct
30 Correct 3 ms 3308 KB correct
31 Correct 3 ms 3308 KB correct
32 Correct 3 ms 3308 KB correct
33 Correct 3 ms 3436 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3308 KB correct
2 Correct 3 ms 3308 KB correct
3 Correct 2 ms 3308 KB correct
4 Correct 2 ms 3308 KB correct
5 Correct 2 ms 3308 KB correct
6 Correct 2 ms 3308 KB correct
7 Correct 2 ms 3308 KB correct
8 Correct 2 ms 3308 KB correct
9 Correct 3 ms 3308 KB correct
10 Correct 2 ms 3308 KB correct
11 Correct 2 ms 3308 KB correct
12 Correct 3 ms 3308 KB correct
13 Correct 2 ms 3308 KB correct
14 Correct 4 ms 3436 KB correct
15 Correct 3 ms 3436 KB correct
16 Correct 3 ms 3436 KB correct
17 Correct 3 ms 3308 KB correct
18 Correct 3 ms 3308 KB correct
19 Correct 3 ms 3308 KB correct
20 Correct 3 ms 3308 KB correct
21 Correct 3 ms 3308 KB correct
22 Correct 3 ms 3328 KB correct
23 Correct 3 ms 3308 KB correct
24 Correct 3 ms 3308 KB correct
25 Correct 3 ms 3308 KB correct
26 Correct 3 ms 3308 KB correct
27 Correct 3 ms 3308 KB correct
28 Correct 3 ms 3308 KB correct
29 Correct 3 ms 3308 KB correct
30 Correct 3 ms 3308 KB correct
31 Correct 3 ms 3308 KB correct
32 Correct 3 ms 3308 KB correct
33 Correct 3 ms 3436 KB correct
34 Correct 31 ms 5228 KB correct
35 Correct 31 ms 5100 KB correct
36 Correct 27 ms 4716 KB correct
37 Correct 9 ms 3436 KB correct
38 Correct 32 ms 5100 KB correct
39 Correct 27 ms 4972 KB correct
40 Correct 24 ms 4716 KB correct
41 Correct 31 ms 5100 KB correct
42 Correct 25 ms 5100 KB correct
43 Correct 16 ms 4460 KB correct
44 Correct 14 ms 4076 KB correct
45 Correct 15 ms 4204 KB correct
46 Correct 14 ms 4076 KB correct
47 Correct 10 ms 3692 KB correct
48 Correct 5 ms 3308 KB correct
49 Correct 8 ms 3436 KB correct
50 Correct 11 ms 3692 KB correct
51 Correct 15 ms 4204 KB correct
52 Correct 14 ms 4076 KB correct
53 Correct 14 ms 4076 KB correct
54 Correct 16 ms 4460 KB correct
55 Correct 15 ms 4204 KB correct
56 Correct 17 ms 4204 KB correct
57 Correct 14 ms 4204 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3308 KB correct
2 Correct 3 ms 3308 KB correct
3 Correct 87 ms 8556 KB correct
4 Correct 138 ms 10988 KB correct
5 Correct 145 ms 10988 KB correct
6 Correct 79 ms 10860 KB correct
7 Correct 113 ms 10988 KB correct
8 Correct 121 ms 10988 KB correct
9 Correct 136 ms 10988 KB correct
10 Correct 143 ms 10860 KB correct
11 Correct 140 ms 11116 KB correct
12 Correct 143 ms 11116 KB correct
13 Correct 2 ms 3308 KB correct
14 Correct 122 ms 10988 KB correct
15 Correct 140 ms 10988 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3308 KB correct
2 Correct 3 ms 3308 KB correct
3 Correct 2 ms 3308 KB correct
4 Correct 2 ms 3308 KB correct
5 Correct 2 ms 3308 KB correct
6 Correct 2 ms 3308 KB correct
7 Correct 2 ms 3308 KB correct
8 Correct 2 ms 3308 KB correct
9 Correct 3 ms 3308 KB correct
10 Correct 2 ms 3308 KB correct
11 Correct 2 ms 3308 KB correct
12 Correct 3 ms 3308 KB correct
13 Correct 2 ms 3308 KB correct
14 Correct 4 ms 3436 KB correct
15 Correct 3 ms 3436 KB correct
16 Correct 3 ms 3436 KB correct
17 Correct 3 ms 3308 KB correct
18 Correct 3 ms 3308 KB correct
19 Correct 3 ms 3308 KB correct
20 Correct 3 ms 3308 KB correct
21 Correct 3 ms 3308 KB correct
22 Correct 3 ms 3328 KB correct
23 Correct 3 ms 3308 KB correct
24 Correct 3 ms 3308 KB correct
25 Correct 3 ms 3308 KB correct
26 Correct 3 ms 3308 KB correct
27 Correct 3 ms 3308 KB correct
28 Correct 3 ms 3308 KB correct
29 Correct 3 ms 3308 KB correct
30 Correct 3 ms 3308 KB correct
31 Correct 3 ms 3308 KB correct
32 Correct 3 ms 3308 KB correct
33 Correct 3 ms 3436 KB correct
34 Correct 31 ms 5228 KB correct
35 Correct 31 ms 5100 KB correct
36 Correct 27 ms 4716 KB correct
37 Correct 9 ms 3436 KB correct
38 Correct 32 ms 5100 KB correct
39 Correct 27 ms 4972 KB correct
40 Correct 24 ms 4716 KB correct
41 Correct 31 ms 5100 KB correct
42 Correct 25 ms 5100 KB correct
43 Correct 16 ms 4460 KB correct
44 Correct 14 ms 4076 KB correct
45 Correct 15 ms 4204 KB correct
46 Correct 14 ms 4076 KB correct
47 Correct 10 ms 3692 KB correct
48 Correct 5 ms 3308 KB correct
49 Correct 8 ms 3436 KB correct
50 Correct 11 ms 3692 KB correct
51 Correct 15 ms 4204 KB correct
52 Correct 14 ms 4076 KB correct
53 Correct 14 ms 4076 KB correct
54 Correct 16 ms 4460 KB correct
55 Correct 15 ms 4204 KB correct
56 Correct 17 ms 4204 KB correct
57 Correct 14 ms 4204 KB correct
58 Correct 2 ms 3308 KB correct
59 Correct 3 ms 3308 KB correct
60 Correct 87 ms 8556 KB correct
61 Correct 138 ms 10988 KB correct
62 Correct 145 ms 10988 KB correct
63 Correct 79 ms 10860 KB correct
64 Correct 113 ms 10988 KB correct
65 Correct 121 ms 10988 KB correct
66 Correct 136 ms 10988 KB correct
67 Correct 143 ms 10860 KB correct
68 Correct 140 ms 11116 KB correct
69 Correct 143 ms 11116 KB correct
70 Correct 2 ms 3308 KB correct
71 Correct 122 ms 10988 KB correct
72 Correct 140 ms 10988 KB correct
73 Correct 2 ms 3308 KB correct
74 Correct 142 ms 10988 KB correct
75 Correct 136 ms 11628 KB correct
76 Correct 70 ms 6508 KB correct
77 Correct 151 ms 12072 KB correct
78 Correct 143 ms 12012 KB correct
79 Correct 140 ms 11884 KB correct
80 Correct 144 ms 11756 KB correct
81 Correct 87 ms 10860 KB correct
82 Correct 157 ms 11628 KB correct
83 Correct 103 ms 7916 KB correct
84 Correct 67 ms 8940 KB correct
85 Correct 63 ms 8428 KB correct
86 Correct 54 ms 6636 KB correct
87 Correct 56 ms 5868 KB correct
88 Correct 41 ms 5228 KB correct
89 Correct 43 ms 5228 KB correct
90 Correct 40 ms 4844 KB correct
91 Correct 22 ms 3820 KB correct
92 Correct 12 ms 3308 KB correct
93 Correct 62 ms 8428 KB correct
94 Correct 56 ms 6764 KB correct
95 Correct 54 ms 6636 KB correct
96 Correct 42 ms 4972 KB correct
97 Correct 44 ms 5228 KB correct
98 Correct 47 ms 5868 KB correct
99 Correct 42 ms 5228 KB correct
100 Correct 31 ms 3820 KB correct
101 Correct 14 ms 3436 KB correct
102 Correct 57 ms 7660 KB correct
103 Correct 61 ms 7660 KB correct
104 Correct 58 ms 7660 KB correct
105 Correct 57 ms 7660 KB correct