Submission #429352

# Submission time Handle Problem Language Result Execution time Memory
429352 2021-06-15T21:32:01 Z 2fat2code Simurgh (IOI17_simurgh) C++17
100 / 100
184 ms 6152 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define fr first
#define sc second
//#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)

const int nmax = 505;

int N, m, h[nmax], ansT, royal[nmax*nmax], lipsa[nmax*nmax], pardsu[nmax];
vector<int>T;
pair<int,int>low[nmax], p[nmax];
vector<pair<int,int>>nod[nmax];
bitset<nmax>viz;

void dfstree(int s, int lvl, int par){
    viz[s] = 1;
    h[s] = lvl;
    for(auto it : nod[s]){
        if(!viz[it.fr]){
            T.push_back(it.sc);
            dfstree(it.fr, lvl + 1, s);
            p[it.fr].fr = s;
            p[it.fr].sc = it.sc;
            if(low[it.fr] < low[s]){
                low[s] = low[it.fr];
            }
        }
    }
    for(auto it : nod[s]){
        if(viz[it.fr] && it.fr != par && h[it.fr] < low[s].fr){
            low[s].fr = h[it.fr];
            low[s].sc = it.sc;
        }
    }
}

void calc(vector<int>vecc, int indx){
    reverse(all(vecc));
    vector<int>ask;
    vector<pair<int,int>>Queries;
    for(auto it : vecc){
        if(royal[it] == -1){
            ask.clear();
            for(auto it1 : T){
                if(it1 != it){
                    ask.push_back(it1);
                }
            }
            ask.push_back(indx);
            lipsa[it] = count_common_roads(ask);
            Queries.push_back({lipsa[it], it});
        }
    }
    Queries.push_back({ansT, indx});
    sort(all(Queries));
    if(Queries[0].fr == Queries.back().fr){
        if(Queries.size() == vecc.size() + 1){
            for(auto it : vecc) royal[it] = 0;
        }
        else{
            ask.clear();
            for(auto it : T){
                if(it != vecc[0]){
                    ask.push_back(it);
                }
            }
            ask.push_back(indx);
            int lol = count_common_roads(ask);
            for(auto it : Queries){
                if(it.fr == lol){
                    if(royal[vecc[0]] == 1) royal[it.sc] = 1;
                    else royal[it.sc] = 0;
                }
                else{
                    if(royal[vecc[0]] == 0) royal[it.sc] = 1;
                    else royal[it.sc] = 0;
                }
            }
        }
    }
    else{
        for(auto it : Queries){
            if(it.fr == Queries.back().fr){
                royal[it.sc] = 0;
            }
            else{
                royal[it.sc] = 1;
            }
        }
    }
}

void dfs2(int s){
    viz[s] = 1;
    for(auto it : nod[s]){
        if(it.sc == low[s].sc && low[s].fr < h[s]){
            int curr = s;
            vector<int>vecc;
            while(h[curr] != h[it.fr]){
                vecc.push_back(p[curr].sc);
                curr = p[curr].fr;
            }
            calc(vecc, it.sc);
        }
    }
    for(auto it : nod[s]){
        if(!viz[it.fr]){
            dfs2(it.fr);
        }
    }
}

int findpar(int x){
    if(x != pardsu[x]){
        pardsu[x] = findpar(pardsu[x]);
    }
    return pardsu[x];
}

void unite(int x, int y){
    x = findpar(x);
    y = findpar(y);
    pardsu[x] = y;
}

void make_bs(int s, vector<int>&u, vector<int>&v){
    vector<pair<int,int>>vecc;
    for(auto it : nod[s]){
        if(it.fr != p[s].fr && h[it.fr] < h[s]){
            vecc.push_back({it.fr, it.sc});
        }
    }
    for(int i=0;i<N;i++) pardsu[i] = i;
    vector<int>ask;
    for(auto it : vecc){
        ask.push_back(it.sc);
        unite(s, it.fr);
    }
    int curr = 0;
    for(auto it : T){
        int x = u[it];
        int y = v[it];
        x = findpar(x);
        y = findpar(y);
        if(x != y){
            pardsu[x] = y;
            ask.push_back(it);
            curr -= royal[it];
        }
    }
    curr += count_common_roads(ask);
    int l = 0;
    int r = (int)vecc.size() - 1;
    int lmao = curr;
    for(int j=1;j<=lmao;j++){
        int ok = 0;
        r = (int)vecc.size() - 1;
        while(l <= r){
            ask.clear();
            int mid = l + (r - l) / 2;
            for(int i=0;i<N;i++) pardsu[i] = i;
            for(int i=0;i<=mid;i++){
                ask.push_back(vecc[i].sc);
                unite(s, vecc[i].fr);
            }
            curr = 0;
            for(auto it : T){
                int x = u[it];
                int y = v[it];
                x = findpar(x);
                y = findpar(y);
                if(x != y){
                    pardsu[x] = y;
                    ask.push_back(it);
                    curr -= royal[it];
                }
            }
            curr += count_common_roads(ask);
            if(curr >= j){
                ok = mid;
                r = mid - 1;
            }
            else{
                l = mid + 1;
            }
        }
        royal[vecc[ok].sc] = 1;
        l = ok + 1;
    }
}

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
	m = (int)u.size();
	N = n;
	for(int i=0;i<m;i++){
        royal[i] = -1;
	}
	for(int i=0;i<n;i++){
        low[i].fr = n + 1;
        low[i].sc = -1;
	}
	for(int i=0;i<m;i++){
        nod[u[i]].push_back({v[i], i});
        nod[v[i]].push_back({u[i], i});
	}
	p[0].fr = -1;
	dfstree(0, 1, -1);
	ansT = count_common_roads(T);
	viz.reset();
	dfs2(0);
	for(auto it : T){
        if(royal[it] == -1) royal[it] = 1;
	}
	for(int i=1;i<n;i++){
        make_bs(i, u, v);
	}
	for(int i=0;i<m;i++){
        if(royal[i] == -1){
            royal[i] = 0;
        }
	}
	vector<int>answer;
	for(int i=0;i<m;i++){
        if(royal[i] == 1)answer.push_back(i);
	}
	return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 332 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 332 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 1 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 332 KB correct
21 Correct 2 ms 332 KB correct
22 Correct 2 ms 320 KB correct
23 Correct 1 ms 376 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 332 KB correct
30 Correct 1 ms 320 KB correct
31 Correct 1 ms 320 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 332 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 1 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 332 KB correct
21 Correct 2 ms 332 KB correct
22 Correct 2 ms 320 KB correct
23 Correct 1 ms 376 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 332 KB correct
30 Correct 1 ms 320 KB correct
31 Correct 1 ms 320 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 33 ms 1740 KB correct
35 Correct 39 ms 1612 KB correct
36 Correct 28 ms 1484 KB correct
37 Correct 9 ms 332 KB correct
38 Correct 33 ms 1612 KB correct
39 Correct 29 ms 1612 KB correct
40 Correct 27 ms 1500 KB correct
41 Correct 30 ms 1744 KB correct
42 Correct 30 ms 1612 KB correct
43 Correct 14 ms 1228 KB correct
44 Correct 13 ms 972 KB correct
45 Correct 14 ms 972 KB correct
46 Correct 13 ms 844 KB correct
47 Correct 10 ms 648 KB correct
48 Correct 4 ms 332 KB correct
49 Correct 6 ms 460 KB correct
50 Correct 10 ms 588 KB correct
51 Correct 16 ms 1064 KB correct
52 Correct 13 ms 972 KB correct
53 Correct 13 ms 844 KB correct
54 Correct 15 ms 1248 KB correct
55 Correct 13 ms 1080 KB correct
56 Correct 13 ms 1072 KB correct
57 Correct 13 ms 972 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 108 ms 4540 KB correct
4 Correct 164 ms 6136 KB correct
5 Correct 157 ms 6024 KB correct
6 Correct 168 ms 6136 KB correct
7 Correct 152 ms 6084 KB correct
8 Correct 139 ms 6132 KB correct
9 Correct 156 ms 6152 KB correct
10 Correct 160 ms 6084 KB correct
11 Correct 160 ms 6140 KB correct
12 Correct 161 ms 6152 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 161 ms 6124 KB correct
15 Correct 161 ms 6080 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 332 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 1 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 332 KB correct
21 Correct 2 ms 332 KB correct
22 Correct 2 ms 320 KB correct
23 Correct 1 ms 376 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 332 KB correct
30 Correct 1 ms 320 KB correct
31 Correct 1 ms 320 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 33 ms 1740 KB correct
35 Correct 39 ms 1612 KB correct
36 Correct 28 ms 1484 KB correct
37 Correct 9 ms 332 KB correct
38 Correct 33 ms 1612 KB correct
39 Correct 29 ms 1612 KB correct
40 Correct 27 ms 1500 KB correct
41 Correct 30 ms 1744 KB correct
42 Correct 30 ms 1612 KB correct
43 Correct 14 ms 1228 KB correct
44 Correct 13 ms 972 KB correct
45 Correct 14 ms 972 KB correct
46 Correct 13 ms 844 KB correct
47 Correct 10 ms 648 KB correct
48 Correct 4 ms 332 KB correct
49 Correct 6 ms 460 KB correct
50 Correct 10 ms 588 KB correct
51 Correct 16 ms 1064 KB correct
52 Correct 13 ms 972 KB correct
53 Correct 13 ms 844 KB correct
54 Correct 15 ms 1248 KB correct
55 Correct 13 ms 1080 KB correct
56 Correct 13 ms 1072 KB correct
57 Correct 13 ms 972 KB correct
58 Correct 1 ms 312 KB correct
59 Correct 1 ms 332 KB correct
60 Correct 108 ms 4540 KB correct
61 Correct 164 ms 6136 KB correct
62 Correct 157 ms 6024 KB correct
63 Correct 168 ms 6136 KB correct
64 Correct 152 ms 6084 KB correct
65 Correct 139 ms 6132 KB correct
66 Correct 156 ms 6152 KB correct
67 Correct 160 ms 6084 KB correct
68 Correct 160 ms 6140 KB correct
69 Correct 161 ms 6152 KB correct
70 Correct 1 ms 332 KB correct
71 Correct 161 ms 6124 KB correct
72 Correct 161 ms 6080 KB correct
73 Correct 1 ms 332 KB correct
74 Correct 162 ms 6148 KB correct
75 Correct 167 ms 5984 KB correct
76 Correct 93 ms 2604 KB correct
77 Correct 160 ms 6148 KB correct
78 Correct 159 ms 6144 KB correct
79 Correct 156 ms 6016 KB correct
80 Correct 159 ms 6040 KB correct
81 Correct 142 ms 5524 KB correct
82 Correct 184 ms 5956 KB correct
83 Correct 119 ms 3532 KB correct
84 Correct 79 ms 4168 KB correct
85 Correct 67 ms 4044 KB correct
86 Correct 57 ms 2768 KB correct
87 Correct 53 ms 2252 KB correct
88 Correct 46 ms 1672 KB correct
89 Correct 50 ms 1628 KB correct
90 Correct 45 ms 1480 KB correct
91 Correct 28 ms 460 KB correct
92 Correct 16 ms 408 KB correct
93 Correct 63 ms 3988 KB correct
94 Correct 55 ms 2636 KB correct
95 Correct 58 ms 2688 KB correct
96 Correct 45 ms 1556 KB correct
97 Correct 46 ms 1612 KB correct
98 Correct 49 ms 2328 KB correct
99 Correct 45 ms 1612 KB correct
100 Correct 30 ms 716 KB correct
101 Correct 17 ms 472 KB correct
102 Correct 60 ms 3236 KB correct
103 Correct 59 ms 3340 KB correct
104 Correct 60 ms 3236 KB correct
105 Correct 56 ms 3268 KB correct