Submission #423317

# Submission time Handle Problem Language Result Execution time Memory
423317 2021-06-11T00:16:36 Z Rudy420 Simurgh (IOI17_simurgh) C++17
100 / 100
336 ms 10944 KB
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define lg length()
#define INF 2000000005
#define LINF 1000000000000000005
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(rng);}
#include "simurgh.h"

const pii nul = {-1, -1};
int n,m;
vector<vector<pii>> g,G;
vector<int> f,h,act,hg,w,vz,p,inans;
vector<pii> pr,sb;
set <int> t,b;

void DFS(int nod){
	act[nod] = 1;
	for(pii i : g[nod]){
		if(hg[i.x]){
			if(i.x != pr[nod].x && (sb[nod].x == -1 || hg[i.x] < hg[sb[nod].x])){
				sb[nod] = i;
				for(pii j : g[i.x]){
					if(act[j.x] && pr[j.x].x == i.x){
						h[i.y] = j.y;
					}
				}
			}
			continue;
		}
		hg[i.x] = hg[nod] + 1;
		pr[i.x] = make_pair(nod, i.y);
		t.insert(i.y);
		DFS(i.x);
		if(sb[i.x].x != -1 && hg[sb[i.x].x] <= hg[nod]) f[i.y] = sb[i.x].y; 

		if(sb[i.x].x != -1 && (sb[nod].x == -1 || hg[sb[i.x].x] < hg[sb[nod].x])){
			sb[nod] = sb[i.x];
		}
	}
	if(sb[nod].x != -1) b.insert(sb[nod].y);
	act[nod] = 0;
}

void DFS2(int nod){
	vz[nod] = 1;
	for(pii i : G[nod]){
		if(vz[i.x]) continue;
		w[i.x] = w[nod] - i.y;
		DFS2(i.x);
	}
}

int Par(int nod){
    if(p[p[nod]] == p[nod]) return p[nod];
    else return p[nod] = Par(p[nod]);
}

void Unite(int x, int y){
    x = Par(x); y = Par(y);
    if(x == y) return;
    if(rand(0,1)%2) swap(x,y);
    p[y] = x;
}

int Ask(int nod, int l, int r, vector<int> &u, vector<int> &v){
	vector<int> qry;
	int c = 0;
	for(int j=0;j<n;j++) p[j] = j;
	for(int j=l;j<=r;j++){
		qry.push_back(g[nod][j].y);
		Unite(nod, g[nod][j].x);
		c += inans[g[nod][j].y];
	}
	for(int i : t){
		if(Par(u[i]) != Par(v[i])){
			Unite(u[i], v[i]);
			qry.push_back(i);
			c += w[i];
		}
	}
	return count_common_roads(qry) - c;
}

vector<int> find_roads(int N, vector<int> u, vector<int> v) {
	vector <int> ans;
	n = N; m = sz(u);
	for(int i=0;i<n;i++) g.push_back({});
	for(int i=0;i<m;i++){
		g[u[i]].push_back({v[i],i});
		g[v[i]].push_back({u[i],i});
	}

	f.resize(m,-1);
	h.resize(m,-1);
	hg.resize(n,0);
	sb.resize(n,nul);
	pr.resize(n,nul);
	act.resize(n,0);
	w.resize(m,-1);
	vz.resize(m,0);

	hg[0] = 1;
	DFS(0);

	//for(int i : t) cout << i << ',' << f[i] << '\n';
	//cout << '\n';
	//for(int i : b) cout << i << ',' << h[i] << '\n';

	for(int i=0;i<m;i++) G.push_back({});
	vector <int> qry;
	for(int i : t) qry.push_back(i);
	int C = count_common_roads(qry);
	for(int i : t){
		if(f[i] == -1) continue;
		qry.erase(find(all(qry),i));
		qry.push_back(f[i]);
		int c = count_common_roads(qry);
		qry.pop_back();
		qry.push_back(i);
		G[i].push_back({f[i],C-c});
		G[f[i]].push_back({i,c-C});
		if(C-c) w[i] = (C-c+1)/2;
	}
	for(int i : b){
		qry.erase(find(all(qry),h[i]));
		qry.push_back(i);
		int c = count_common_roads(qry);
		qry.pop_back();
		qry.push_back(h[i]);
		G[i].push_back({h[i],c-C});
		G[h[i]].push_back({i,C-c});
		if(C-c) w[h[i]] = (C-c+1)/2;
	}

	for(int i : t){
		if(!vz[i] && w[i]!=-1) DFS2(i);
	}
	for(int i : t){
		if(f[i] == -1) w[i] = 1, DFS2(i);
		else if(w[i] == -1) w[i] = 0, DFS2(i);
	}

	inans.resize(m, 0);
	p.resize(n);
	vector<int> d(n,0);
	queue <int> q;
	for(int i=0;i<n;i++){
		d[i] = Ask(i, 0, sz(g[i]) - 1, u, v);
		if(d[i] == 1) q.push(i);
	}

	for(int i=1;i<n;i++){
		if(sz(q) == 0) break;
		int t = q.front();
		q.pop();
		int l=0,r=sz(g[t])-1;
		while(l!=r){
			int mid = (l+r)/2;
			if(Ask(t, l, mid, u, v)) r = mid;
			else l = mid + 1;
		}
		ans.push_back(g[t][l].y);
		inans[g[t][l].y] = 1;
		int p = g[t][l].x;
		d[p]--;
		if(d[p] == 1) q.push(p);
	}

	return ans;

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 204 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 204 KB correct
14 Correct 3 ms 332 KB correct
15 Correct 3 ms 360 KB correct
16 Correct 3 ms 460 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 3 ms 332 KB correct
20 Correct 3 ms 332 KB correct
21 Correct 3 ms 332 KB correct
22 Correct 2 ms 332 KB correct
23 Correct 2 ms 332 KB correct
24 Correct 2 ms 388 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 2 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 2 ms 332 KB correct
29 Correct 2 ms 332 KB correct
30 Correct 2 ms 332 KB correct
31 Correct 3 ms 332 KB correct
32 Correct 2 ms 332 KB correct
33 Correct 2 ms 332 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 204 KB correct
14 Correct 3 ms 332 KB correct
15 Correct 3 ms 360 KB correct
16 Correct 3 ms 460 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 3 ms 332 KB correct
20 Correct 3 ms 332 KB correct
21 Correct 3 ms 332 KB correct
22 Correct 2 ms 332 KB correct
23 Correct 2 ms 332 KB correct
24 Correct 2 ms 388 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 2 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 2 ms 332 KB correct
29 Correct 2 ms 332 KB correct
30 Correct 2 ms 332 KB correct
31 Correct 3 ms 332 KB correct
32 Correct 2 ms 332 KB correct
33 Correct 2 ms 332 KB correct
34 Correct 62 ms 2872 KB correct
35 Correct 63 ms 2760 KB correct
36 Correct 59 ms 2540 KB correct
37 Correct 24 ms 460 KB correct
38 Correct 76 ms 2880 KB correct
39 Correct 80 ms 2708 KB correct
40 Correct 56 ms 2508 KB correct
41 Correct 62 ms 2896 KB correct
42 Correct 61 ms 2888 KB correct
43 Correct 51 ms 1868 KB correct
44 Correct 47 ms 1548 KB correct
45 Correct 59 ms 1596 KB correct
46 Correct 45 ms 1424 KB correct
47 Correct 37 ms 952 KB correct
48 Correct 17 ms 388 KB correct
49 Correct 26 ms 512 KB correct
50 Correct 37 ms 844 KB correct
51 Correct 49 ms 1660 KB correct
52 Correct 51 ms 1552 KB correct
53 Correct 45 ms 1420 KB correct
54 Correct 51 ms 1876 KB correct
55 Correct 50 ms 1676 KB correct
56 Correct 60 ms 1604 KB correct
57 Correct 50 ms 1676 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 192 ms 7844 KB correct
4 Correct 335 ms 10936 KB correct
5 Correct 304 ms 10924 KB correct
6 Correct 298 ms 10928 KB correct
7 Correct 308 ms 10936 KB correct
8 Correct 313 ms 10940 KB correct
9 Correct 327 ms 10908 KB correct
10 Correct 335 ms 10936 KB correct
11 Correct 307 ms 10920 KB correct
12 Correct 333 ms 10944 KB correct
13 Correct 1 ms 204 KB correct
14 Correct 314 ms 10896 KB correct
15 Correct 322 ms 10868 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 0 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 204 KB correct
14 Correct 3 ms 332 KB correct
15 Correct 3 ms 360 KB correct
16 Correct 3 ms 460 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 3 ms 332 KB correct
20 Correct 3 ms 332 KB correct
21 Correct 3 ms 332 KB correct
22 Correct 2 ms 332 KB correct
23 Correct 2 ms 332 KB correct
24 Correct 2 ms 388 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 2 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 2 ms 332 KB correct
29 Correct 2 ms 332 KB correct
30 Correct 2 ms 332 KB correct
31 Correct 3 ms 332 KB correct
32 Correct 2 ms 332 KB correct
33 Correct 2 ms 332 KB correct
34 Correct 62 ms 2872 KB correct
35 Correct 63 ms 2760 KB correct
36 Correct 59 ms 2540 KB correct
37 Correct 24 ms 460 KB correct
38 Correct 76 ms 2880 KB correct
39 Correct 80 ms 2708 KB correct
40 Correct 56 ms 2508 KB correct
41 Correct 62 ms 2896 KB correct
42 Correct 61 ms 2888 KB correct
43 Correct 51 ms 1868 KB correct
44 Correct 47 ms 1548 KB correct
45 Correct 59 ms 1596 KB correct
46 Correct 45 ms 1424 KB correct
47 Correct 37 ms 952 KB correct
48 Correct 17 ms 388 KB correct
49 Correct 26 ms 512 KB correct
50 Correct 37 ms 844 KB correct
51 Correct 49 ms 1660 KB correct
52 Correct 51 ms 1552 KB correct
53 Correct 45 ms 1420 KB correct
54 Correct 51 ms 1876 KB correct
55 Correct 50 ms 1676 KB correct
56 Correct 60 ms 1604 KB correct
57 Correct 50 ms 1676 KB correct
58 Correct 1 ms 204 KB correct
59 Correct 1 ms 204 KB correct
60 Correct 192 ms 7844 KB correct
61 Correct 335 ms 10936 KB correct
62 Correct 304 ms 10924 KB correct
63 Correct 298 ms 10928 KB correct
64 Correct 308 ms 10936 KB correct
65 Correct 313 ms 10940 KB correct
66 Correct 327 ms 10908 KB correct
67 Correct 335 ms 10936 KB correct
68 Correct 307 ms 10920 KB correct
69 Correct 333 ms 10944 KB correct
70 Correct 1 ms 204 KB correct
71 Correct 314 ms 10896 KB correct
72 Correct 322 ms 10868 KB correct
73 Correct 1 ms 204 KB correct
74 Correct 299 ms 10900 KB correct
75 Correct 336 ms 10620 KB correct
76 Correct 173 ms 4680 KB correct
77 Correct 326 ms 10920 KB correct
78 Correct 299 ms 10812 KB correct
79 Correct 309 ms 10784 KB correct
80 Correct 306 ms 10644 KB correct
81 Correct 311 ms 9776 KB correct
82 Correct 313 ms 10616 KB correct
83 Correct 261 ms 5936 KB correct
84 Correct 256 ms 8124 KB correct
85 Correct 254 ms 6540 KB correct
86 Correct 217 ms 4804 KB correct
87 Correct 212 ms 3528 KB correct
88 Correct 221 ms 2760 KB correct
89 Correct 181 ms 2636 KB correct
90 Correct 197 ms 2504 KB correct
91 Correct 105 ms 588 KB correct
92 Correct 70 ms 464 KB correct
93 Correct 252 ms 6540 KB correct
94 Correct 215 ms 4800 KB correct
95 Correct 218 ms 4824 KB correct
96 Correct 218 ms 2632 KB correct
97 Correct 189 ms 2764 KB correct
98 Correct 206 ms 3404 KB correct
99 Correct 189 ms 2816 KB correct
100 Correct 136 ms 964 KB correct
101 Correct 67 ms 504 KB correct
102 Correct 249 ms 5720 KB correct
103 Correct 252 ms 5712 KB correct
104 Correct 250 ms 5612 KB correct
105 Correct 242 ms 5572 KB correct