Submission #200915

# Submission time Handle Problem Language Result Execution time Memory
200915 2020-02-08T14:18:53 Z oolimry Simurgh (IOI17_simurgh) C++14
100 / 100
237 ms 7036 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> ii;

int n, m;
vector<int> answer;
 
vector<int> adj[505];
int edgeNo[505][505];
int edgeState[250000]; ///-1: unknown, 0: unroyal. 1: royal
ii edges[250000]; ///
int depth[505];
int parent[505];
int back[505];
int degree[505];
vector<ii> backCycles;
vector<int> treeEdges;

void dfs(int u){
	int largestBack = u;
	for(int v : adj[u]){
		if(depth[v] == 0){
			depth[v] = depth[u] + 1;
			parent[v] = u;
			treeEdges.push_back(edgeNo[u][v]);
			dfs(v);
		}
		else if(v != parent[u]){
			if(depth[largestBack] > depth[v]) largestBack = v;
		}
	}
	backCycles.push_back(ii(u, largestBack));
}

struct UFDS{
	int p[505];
	
	void reset(){
		for(int i = 0;i < n;i++) p[i] = i;
	}
	
	int findSet(int u){
		if(u == p[u]) return u;
		else{
			p[u] = findSet(p[u]);
			return p[u];
		}
	}
	
	bool unionSet(int u, int v){
		u = findSet(u); v = findSet(v);
		if(u == v) return false;
		p[u] = v;
		return true;
	}
} uf;

int queryForest(vector<int> forestEdges){
	uf.reset();
	int res = 0;
	
	vector<int> query;
	
	for(int e : forestEdges){
		uf.unionSet(edges[e].first, edges[e].second);
		query.push_back(e);
	}
	
	for(int e : treeEdges){
		if(uf.unionSet(edges[e].first, edges[e].second)){
			query.push_back(e);
			if(edgeState[e] == 1) res--;
		}
	}
	
	res += count_common_roads(query);
	return res;
}

vector<int> find_roads(int N, vector<int> P, vector<int> Q) {
	n = N; m = P.size();
	
	for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) edgeNo[i][j] = -1;
	fill(edgeState,edgeState+m,-1);
	
	for(int i = 0;i < m;i++){
		edgeNo[P[i]][Q[i]] = i; 
		edgeNo[Q[i]][P[i]] = i;
		edges[i] = ii(P[i],Q[i]);
		adj[P[i]].push_back(Q[i]);
		adj[Q[i]].push_back(P[i]);
	}

	depth[0] = 1;
	dfs(0);
	
	
	
	for(ii C : backCycles){
		vector<int> cycleEdges;
		int bottom = C.first, top = C.second;
		if(bottom == top) continue;
		
		cycleEdges.push_back(edgeNo[bottom][top]);
		while(bottom != top){
			int up = parent[bottom];
			cycleEdges.push_back(edgeNo[up][bottom]);
			bottom = up;
		}
		
		vector<int> cycleAnswer;
		int maxValue = 0; int minValue = 600;
		int reference = -1; ///value taken if we know we're excluding an unroyal edge
		
		for(int removedEdge : cycleEdges){
			if(edgeState[removedEdge] != -1){
				if(reference != -1){
					int res = reference;
					if(edgeState[removedEdge] == 1) res--;
					maxValue = max(res, maxValue);
					minValue = min(res, minValue);
					cycleAnswer.push_back(res);
					continue;
				}
			}
			
			vector<int> query;
			for(int e : cycleEdges){
				if(e != removedEdge) query.push_back(e);
			}
			int res = queryForest(query);
			cycleAnswer.push_back(res);
			maxValue = max(res, maxValue);
			minValue = min(res, minValue);
			
			if(edgeState[removedEdge] != -1){
				if(reference == -1){
					reference = res;
					if(edgeState[removedEdge] == 1) reference++;
				}
			}
		}
		
		if(minValue == maxValue){
			for(int e : cycleEdges){
				edgeState[e] = 0;
				//cout << e << " " << edgeState[e] << "D\n";
			}
		}
		else{
			for(int i = 0;i < (int) cycleEdges.size();i++){
				if(cycleAnswer[i] == maxValue) edgeState[cycleEdges[i]] = 0;
				else edgeState[cycleEdges[i]] = 1;
				//cout << cycleEdges[i] << " " << edgeState[cycleEdges[i]] << "D\n";
			}
		}
		
	}
	
	for(int e : treeEdges){
		if(edgeState[e] == -1) edgeState[e] = 1;
		//cout << e << " " << edgeState[e] << "\n";
	}
	
	for(int i = 0;i < n;i++){
		vector<int> query;
		for(int j = 0;j < n;j++){
			if(edgeNo[i][j] != -1){
				query.push_back(edgeNo[i][j]);
			}
		}
		degree[i] = queryForest(query);
	}
	
	
	while((int) answer.size() < n - 1){
		
		for(int i = 0;i < n;i++){
			//cout << degree[i] << " ";
		}
		//cout << endl;
		for(int leaf = 0;leaf < n;leaf++){
			if(degree[leaf] != 1) continue;
			
			//cout << leaf << " "<< endl;
			vector<int> allEdges;
			for(int j = 0;j < n;j++){
				if(edgeNo[leaf][j] != -1 && degree[j] != 0){
					allEdges.push_back(edgeNo[leaf][j]);
				}
			}
			
			int low = -1; int high = allEdges.size() - 1;
			
			//for(int e : allEdges){
			//	cout << edges[e].first << " " << edges[e].second << "E\n";
			//}
			while(true){
				if(low == high - 1) break;
				int s = (low + high) / 2;
				
				vector<int> query;
				for(int i = 0;i <= s;i++){
					query.push_back(allEdges[i]);
				}
				
				//cout << s << " " << queryForest(query) << endl;
				if(queryForest(query) == 1) high = s;
				else low = s;
			}
			
			int leafEdge = allEdges[high];
			degree[edges[leafEdge].first]--; 
			degree[edges[leafEdge].second]--;
			answer.push_back(leafEdge);
			
			//break;
		}
		//break;
	}
	
	return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB correct
2 Correct 5 ms 376 KB correct
3 Correct 5 ms 376 KB correct
4 Correct 6 ms 380 KB correct
5 Correct 6 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 5 ms 376 KB correct
9 Correct 5 ms 376 KB correct
10 Correct 5 ms 376 KB correct
11 Correct 5 ms 376 KB correct
12 Correct 5 ms 376 KB correct
13 Correct 6 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB correct
2 Correct 5 ms 376 KB correct
3 Correct 5 ms 376 KB correct
4 Correct 6 ms 380 KB correct
5 Correct 6 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 5 ms 376 KB correct
9 Correct 5 ms 376 KB correct
10 Correct 5 ms 376 KB correct
11 Correct 5 ms 376 KB correct
12 Correct 5 ms 376 KB correct
13 Correct 6 ms 376 KB correct
14 Correct 7 ms 508 KB correct
15 Correct 6 ms 504 KB correct
16 Correct 7 ms 504 KB correct
17 Correct 7 ms 504 KB correct
18 Correct 6 ms 504 KB correct
19 Correct 6 ms 504 KB correct
20 Correct 7 ms 504 KB correct
21 Correct 6 ms 504 KB correct
22 Correct 6 ms 504 KB correct
23 Correct 6 ms 504 KB correct
24 Correct 6 ms 632 KB correct
25 Correct 5 ms 504 KB correct
26 Correct 6 ms 504 KB correct
27 Correct 6 ms 504 KB correct
28 Correct 6 ms 504 KB correct
29 Correct 6 ms 504 KB correct
30 Correct 6 ms 504 KB correct
31 Correct 7 ms 504 KB correct
32 Correct 6 ms 504 KB correct
33 Correct 6 ms 504 KB correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB correct
2 Correct 5 ms 376 KB correct
3 Correct 5 ms 376 KB correct
4 Correct 6 ms 380 KB correct
5 Correct 6 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 5 ms 376 KB correct
9 Correct 5 ms 376 KB correct
10 Correct 5 ms 376 KB correct
11 Correct 5 ms 376 KB correct
12 Correct 5 ms 376 KB correct
13 Correct 6 ms 376 KB correct
14 Correct 7 ms 508 KB correct
15 Correct 6 ms 504 KB correct
16 Correct 7 ms 504 KB correct
17 Correct 7 ms 504 KB correct
18 Correct 6 ms 504 KB correct
19 Correct 6 ms 504 KB correct
20 Correct 7 ms 504 KB correct
21 Correct 6 ms 504 KB correct
22 Correct 6 ms 504 KB correct
23 Correct 6 ms 504 KB correct
24 Correct 6 ms 632 KB correct
25 Correct 5 ms 504 KB correct
26 Correct 6 ms 504 KB correct
27 Correct 6 ms 504 KB correct
28 Correct 6 ms 504 KB correct
29 Correct 6 ms 504 KB correct
30 Correct 6 ms 504 KB correct
31 Correct 7 ms 504 KB correct
32 Correct 6 ms 504 KB correct
33 Correct 6 ms 504 KB correct
34 Correct 49 ms 2040 KB correct
35 Correct 51 ms 2040 KB correct
36 Correct 46 ms 1784 KB correct
37 Correct 25 ms 1064 KB correct
38 Correct 52 ms 2040 KB correct
39 Correct 49 ms 1912 KB correct
40 Correct 43 ms 1784 KB correct
41 Correct 53 ms 2040 KB correct
42 Correct 55 ms 2168 KB correct
43 Correct 38 ms 1528 KB correct
44 Correct 35 ms 1400 KB correct
45 Correct 37 ms 1400 KB correct
46 Correct 34 ms 1272 KB correct
47 Correct 27 ms 1144 KB correct
48 Correct 13 ms 888 KB correct
49 Correct 19 ms 888 KB correct
50 Correct 27 ms 1144 KB correct
51 Correct 37 ms 1528 KB correct
52 Correct 35 ms 1404 KB correct
53 Correct 32 ms 1272 KB correct
54 Correct 37 ms 1528 KB correct
55 Correct 43 ms 1528 KB correct
56 Correct 43 ms 1468 KB correct
57 Correct 40 ms 1528 KB correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB correct
2 Correct 5 ms 376 KB correct
3 Correct 144 ms 4856 KB correct
4 Correct 227 ms 7032 KB correct
5 Correct 231 ms 7036 KB correct
6 Correct 233 ms 6920 KB correct
7 Correct 209 ms 6988 KB correct
8 Correct 206 ms 7032 KB correct
9 Correct 232 ms 7032 KB correct
10 Correct 230 ms 7032 KB correct
11 Correct 228 ms 6904 KB correct
12 Correct 234 ms 6904 KB correct
13 Correct 5 ms 376 KB correct
14 Correct 215 ms 6904 KB correct
15 Correct 223 ms 6904 KB correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB correct
2 Correct 5 ms 376 KB correct
3 Correct 5 ms 376 KB correct
4 Correct 6 ms 380 KB correct
5 Correct 6 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 5 ms 376 KB correct
9 Correct 5 ms 376 KB correct
10 Correct 5 ms 376 KB correct
11 Correct 5 ms 376 KB correct
12 Correct 5 ms 376 KB correct
13 Correct 6 ms 376 KB correct
14 Correct 7 ms 508 KB correct
15 Correct 6 ms 504 KB correct
16 Correct 7 ms 504 KB correct
17 Correct 7 ms 504 KB correct
18 Correct 6 ms 504 KB correct
19 Correct 6 ms 504 KB correct
20 Correct 7 ms 504 KB correct
21 Correct 6 ms 504 KB correct
22 Correct 6 ms 504 KB correct
23 Correct 6 ms 504 KB correct
24 Correct 6 ms 632 KB correct
25 Correct 5 ms 504 KB correct
26 Correct 6 ms 504 KB correct
27 Correct 6 ms 504 KB correct
28 Correct 6 ms 504 KB correct
29 Correct 6 ms 504 KB correct
30 Correct 6 ms 504 KB correct
31 Correct 7 ms 504 KB correct
32 Correct 6 ms 504 KB correct
33 Correct 6 ms 504 KB correct
34 Correct 49 ms 2040 KB correct
35 Correct 51 ms 2040 KB correct
36 Correct 46 ms 1784 KB correct
37 Correct 25 ms 1064 KB correct
38 Correct 52 ms 2040 KB correct
39 Correct 49 ms 1912 KB correct
40 Correct 43 ms 1784 KB correct
41 Correct 53 ms 2040 KB correct
42 Correct 55 ms 2168 KB correct
43 Correct 38 ms 1528 KB correct
44 Correct 35 ms 1400 KB correct
45 Correct 37 ms 1400 KB correct
46 Correct 34 ms 1272 KB correct
47 Correct 27 ms 1144 KB correct
48 Correct 13 ms 888 KB correct
49 Correct 19 ms 888 KB correct
50 Correct 27 ms 1144 KB correct
51 Correct 37 ms 1528 KB correct
52 Correct 35 ms 1404 KB correct
53 Correct 32 ms 1272 KB correct
54 Correct 37 ms 1528 KB correct
55 Correct 43 ms 1528 KB correct
56 Correct 43 ms 1468 KB correct
57 Correct 40 ms 1528 KB correct
58 Correct 5 ms 376 KB correct
59 Correct 5 ms 376 KB correct
60 Correct 144 ms 4856 KB correct
61 Correct 227 ms 7032 KB correct
62 Correct 231 ms 7036 KB correct
63 Correct 233 ms 6920 KB correct
64 Correct 209 ms 6988 KB correct
65 Correct 206 ms 7032 KB correct
66 Correct 232 ms 7032 KB correct
67 Correct 230 ms 7032 KB correct
68 Correct 228 ms 6904 KB correct
69 Correct 234 ms 6904 KB correct
70 Correct 5 ms 376 KB correct
71 Correct 215 ms 6904 KB correct
72 Correct 223 ms 6904 KB correct
73 Correct 5 ms 376 KB correct
74 Correct 233 ms 6944 KB correct
75 Correct 234 ms 6788 KB correct
76 Correct 119 ms 3192 KB correct
77 Correct 226 ms 6904 KB correct
78 Correct 225 ms 6904 KB correct
79 Correct 234 ms 7032 KB correct
80 Correct 237 ms 6904 KB correct
81 Correct 193 ms 6264 KB correct
82 Correct 235 ms 6904 KB correct
83 Correct 175 ms 4360 KB correct
84 Correct 174 ms 4984 KB correct
85 Correct 159 ms 4600 KB correct
86 Correct 135 ms 3520 KB correct
87 Correct 123 ms 2936 KB correct
88 Correct 119 ms 2552 KB correct
89 Correct 115 ms 2512 KB correct
90 Correct 111 ms 2296 KB correct
91 Correct 60 ms 1528 KB correct
92 Correct 35 ms 1400 KB correct
93 Correct 157 ms 4600 KB correct
94 Correct 139 ms 3448 KB correct
95 Correct 134 ms 3448 KB correct
96 Correct 105 ms 2552 KB correct
97 Correct 112 ms 2552 KB correct
98 Correct 125 ms 3064 KB correct
99 Correct 113 ms 2552 KB correct
100 Correct 78 ms 1656 KB correct
101 Correct 44 ms 1656 KB correct
102 Correct 174 ms 4216 KB correct
103 Correct 176 ms 4344 KB correct
104 Correct 174 ms 4216 KB correct
105 Correct 170 ms 4112 KB correct