Submission #404908

# Submission time Handle Problem Language Result Execution time Memory
404908 2021-05-15T09:35:21 Z b00n0rp Simurgh (IOI17_simurgh) C++17
100 / 100
199 ms 7064 KB
#include<bits/stdc++.h>
#include "simurgh.h"
using namespace std;

#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define F first
#define S second
#define pb push_back
#define REP(i,n) for(int i = 0; i < n; i ++)
#define FOR(i,a,b) for(int i = a; i < b; i ++)

const int MX = 505;

int N;
vpii adj[MX];
vpii edges;
vi T;
vi ans;
int status[MX*MX];
bitset<MX> vis;
int dep[MX];
pii low[MX],par[MX];

void dfs(int u,int d){
	vis[u] = 1;
	dep[u] = d;
	for(auto v:adj[u]){
		if(vis[v.F] and dep[v.F] > d){
			low[u] = min(low[u],low[v.F]);
		}
		else if(vis[v.F]){
			low[u] = min(low[u],{dep[v.F],v.S});
		}
		else{
			T.pb(v.S);
			par[v.F] = {u,v.S};
			low[v.F] = {d,v.S};
			dfs(v.F,d+1);
		}
	}
}

int dsu_par[MX],dsu_rank[MX];

int find(int x){
	if(dsu_par[x] == x) return dsu_par[x];
	return dsu_par[x] = find(dsu_par[x]);
}

void merge(int u,int v){
	u = find(u);
	v = find(v);
	if(dsu_rank[u] < dsu_rank[v]) swap(u,v);
	dsu_par[v] = u;
	dsu_rank[u]++;
}

int count(vi gg){
	FOR(i,1,N+1){
		dsu_par[i] = i;
		dsu_rank[i] = 1;
	}
	for(auto x:gg) merge(edges[x].F,edges[x].S);
	int precount = 0;
	for(auto x:T){
		if(find(edges[x].F) != find(edges[x].S)){
			merge(edges[x].F,edges[x].S);
			gg.pb(x);
			precount += status[x];
		}
	}
	return count_common_roads(gg)-precount;
}

void solve(vi gg,int k){
	if(k == 0) return;
	if(gg.size() == k){
		for(auto x:gg){
			status[x] = 1;
		}
		return;
	}
	vi gg1,gg2;
	REP(i,gg.size()){
		if(i < gg.size()/2) gg1.pb(gg[i]);
		else gg2.pb(gg[i]);
	}
	int c = count(gg1);
	solve(gg1,c);
	solve(gg2,k-c);
}

vi find_roads(int n, vi u, vi v) {
	N = n;
	int m = u.size();
	REP(i,m){
		u[i]++;
		v[i]++;
		adj[u[i]].pb({v[i],i});
		adj[v[i]].pb({u[i],i});
		edges.pb({u[i],v[i]});
	}
	vis.reset();
	par[1] = {1,-1};
	low[1] = {0,-1};
	dfs(1,0);
	sort(T.begin(),T.end());
	REP(i,m) status[i] = -1;
	int basic = count_common_roads(T);
	FOR(i,1,n+1){
		if(low[i].S != -1 and (!binary_search(T.begin(),T.end(),low[i].S))){
			int u = edges[low[i].S].F;
			int v = edges[low[i].S].S; 
			if(dep[u] < dep[v]) swap(u,v);
			int cur = u;
			vi known;
			vi unknown;
			while(cur != v){
				if(status[par[cur].S] == -1) unknown.pb(par[cur].S);
				else known.pb(par[cur].S);
				cur = par[cur].F;
			}
			if(unknown.empty()) continue;
			if(known.empty()){
				vpii bruh;
				int mn = basic,mx = basic;
				for(auto y:unknown){
					vi gg;
					gg.pb(low[i].S);
					for(auto x:T){
						if(x != y) gg.pb(x);
					}
					bruh.pb({y,count_common_roads(gg)});
					mn = min(mn,bruh.back().S);
					mx = max(mx,bruh.back().S);
				}
				if(mn == mx){
					for(auto y:unknown) status[y] = 0;
				}
				else{
					for(auto y:bruh){
						if(y.S == mn) status[y.F] = 1;
						else status[y.F] = 0;
					}
				}
			}
			else{
				vi gg;
				gg.pb(low[i].S);
				for(auto x:T){
					if(x != known[0]) gg.pb(x);
				}
				int roy = count_common_roads(gg)-(basic-status[known[0]]);
				for(auto y:unknown){
					gg.clear();
					gg.pb(low[i].S);
					for(auto x:T){
						if(x != y) gg.pb(x);
					}
					status[y] = basic-count_common_roads(gg)+roy;
				}
			}
		}
	}
	for(auto x:T){
		if(status[x] == -1) status[x] = 1;
	}
	FOR(i,1,n+1){
		vi gg;
		for(auto x:adj[i]){
			if(x.F > i) gg.pb(x.S);
		}
		solve(gg,count(gg));
	}
	REP(i,m){
		if(status[i] == 1){
			ans.pb(i);
		}
	}
	return ans;
}

Compilation message

simurgh.cpp: In function 'void solve(std::vector<int>, int)':
simurgh.cpp:79:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |  if(gg.size() == k){
      |     ~~~~~~~~~~^~~~
simurgh.cpp:11:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 | #define REP(i,n) for(int i = 0; i < n; i ++)
......
   86 |  REP(i,gg.size()){
      |      ~~~~~~~~~~~                   
simurgh.cpp:86:2: note: in expansion of macro 'REP'
   86 |  REP(i,gg.size()){
      |  ^~~
simurgh.cpp:87:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   if(i < gg.size()/2) gg1.pb(gg[i]);
      |      ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 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 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 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 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 2 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 332 KB correct
23 Correct 2 ms 332 KB correct
24 Correct 2 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 2 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 312 KB correct
30 Correct 2 ms 332 KB correct
31 Correct 2 ms 332 KB correct
32 Correct 2 ms 332 KB correct
33 Correct 2 ms 316 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 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 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 2 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 332 KB correct
23 Correct 2 ms 332 KB correct
24 Correct 2 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 2 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 312 KB correct
30 Correct 2 ms 332 KB correct
31 Correct 2 ms 332 KB correct
32 Correct 2 ms 332 KB correct
33 Correct 2 ms 316 KB correct
34 Correct 43 ms 1884 KB correct
35 Correct 38 ms 1892 KB correct
36 Correct 32 ms 1484 KB correct
37 Correct 12 ms 460 KB correct
38 Correct 39 ms 1864 KB correct
39 Correct 32 ms 1748 KB correct
40 Correct 28 ms 1484 KB correct
41 Correct 37 ms 1864 KB correct
42 Correct 38 ms 1864 KB correct
43 Correct 23 ms 1356 KB correct
44 Correct 21 ms 1100 KB correct
45 Correct 23 ms 1160 KB correct
46 Correct 19 ms 972 KB correct
47 Correct 14 ms 672 KB correct
48 Correct 8 ms 332 KB correct
49 Correct 11 ms 448 KB correct
50 Correct 15 ms 588 KB correct
51 Correct 26 ms 1100 KB correct
52 Correct 21 ms 1100 KB correct
53 Correct 21 ms 972 KB correct
54 Correct 22 ms 1228 KB correct
55 Correct 22 ms 1100 KB correct
56 Correct 22 ms 1172 KB correct
57 Correct 22 ms 1096 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 113 ms 4408 KB correct
4 Correct 186 ms 6084 KB correct
5 Correct 181 ms 6072 KB correct
6 Correct 167 ms 6072 KB correct
7 Correct 111 ms 5968 KB correct
8 Correct 122 ms 6020 KB correct
9 Correct 177 ms 6072 KB correct
10 Correct 192 ms 6144 KB correct
11 Correct 181 ms 6080 KB correct
12 Correct 181 ms 6020 KB correct
13 Correct 1 ms 204 KB correct
14 Correct 153 ms 6012 KB correct
15 Correct 181 ms 6088 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 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 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 2 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 332 KB correct
23 Correct 2 ms 332 KB correct
24 Correct 2 ms 332 KB correct
25 Correct 1 ms 332 KB correct
26 Correct 2 ms 332 KB correct
27 Correct 2 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 312 KB correct
30 Correct 2 ms 332 KB correct
31 Correct 2 ms 332 KB correct
32 Correct 2 ms 332 KB correct
33 Correct 2 ms 316 KB correct
34 Correct 43 ms 1884 KB correct
35 Correct 38 ms 1892 KB correct
36 Correct 32 ms 1484 KB correct
37 Correct 12 ms 460 KB correct
38 Correct 39 ms 1864 KB correct
39 Correct 32 ms 1748 KB correct
40 Correct 28 ms 1484 KB correct
41 Correct 37 ms 1864 KB correct
42 Correct 38 ms 1864 KB correct
43 Correct 23 ms 1356 KB correct
44 Correct 21 ms 1100 KB correct
45 Correct 23 ms 1160 KB correct
46 Correct 19 ms 972 KB correct
47 Correct 14 ms 672 KB correct
48 Correct 8 ms 332 KB correct
49 Correct 11 ms 448 KB correct
50 Correct 15 ms 588 KB correct
51 Correct 26 ms 1100 KB correct
52 Correct 21 ms 1100 KB correct
53 Correct 21 ms 972 KB correct
54 Correct 22 ms 1228 KB correct
55 Correct 22 ms 1100 KB correct
56 Correct 22 ms 1172 KB correct
57 Correct 22 ms 1096 KB correct
58 Correct 1 ms 204 KB correct
59 Correct 1 ms 332 KB correct
60 Correct 113 ms 4408 KB correct
61 Correct 186 ms 6084 KB correct
62 Correct 181 ms 6072 KB correct
63 Correct 167 ms 6072 KB correct
64 Correct 111 ms 5968 KB correct
65 Correct 122 ms 6020 KB correct
66 Correct 177 ms 6072 KB correct
67 Correct 192 ms 6144 KB correct
68 Correct 181 ms 6080 KB correct
69 Correct 181 ms 6020 KB correct
70 Correct 1 ms 204 KB correct
71 Correct 153 ms 6012 KB correct
72 Correct 181 ms 6088 KB correct
73 Correct 1 ms 204 KB correct
74 Correct 199 ms 7064 KB correct
75 Correct 172 ms 6844 KB correct
76 Correct 92 ms 2760 KB correct
77 Correct 183 ms 6900 KB correct
78 Correct 184 ms 7024 KB correct
79 Correct 182 ms 6900 KB correct
80 Correct 178 ms 6840 KB correct
81 Correct 115 ms 6208 KB correct
82 Correct 182 ms 6840 KB correct
83 Correct 136 ms 3980 KB correct
84 Correct 107 ms 4804 KB correct
85 Correct 103 ms 4512 KB correct
86 Correct 90 ms 2880 KB correct
87 Correct 79 ms 2504 KB correct
88 Correct 73 ms 1836 KB correct
89 Correct 69 ms 1768 KB correct
90 Correct 67 ms 1608 KB correct
91 Correct 35 ms 460 KB correct
92 Correct 27 ms 412 KB correct
93 Correct 104 ms 4492 KB correct
94 Correct 88 ms 2988 KB correct
95 Correct 86 ms 2924 KB correct
96 Correct 73 ms 1608 KB correct
97 Correct 71 ms 1736 KB correct
98 Correct 77 ms 2504 KB correct
99 Correct 73 ms 1736 KB correct
100 Correct 46 ms 744 KB correct
101 Correct 26 ms 332 KB correct
102 Correct 105 ms 3756 KB correct
103 Correct 107 ms 3860 KB correct
104 Correct 100 ms 3644 KB correct
105 Correct 100 ms 3712 KB correct