답안 #200917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
200917 2020-02-08T14:39:35 Z oolimry Simurgh (IOI17_simurgh) C++14
100 / 100
260 ms 6136 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

///T is the spanning tree we determine, R is the royal tree

///Phase A: find any spanning tree T of the graph, and determine if each of its edges belong to the royal set in 3N queries
///T is a DFS Tree. For each node, find a back edge that goes up as much as possible. We'll call the cycle formed by the back edge and T a back cycle.

///For any cycle, we can find which edges are royal by fixing the rest of the queried tree, and try to remove each edge from the cycle.
///If the cycle has some edges we know the status of, we need not try to remove all of them, just removing 1 of them as a reference is good enough.
///As all the edges among all cycles (except for the N back edges) belong to T, there are at most 3N queries. (N edges in T + N back edges + N reference edge for each cycle)
///If any edge is not part of any back cycle, then it is a bridge and is surely a royal road

///This spanning tree is useful since we can now query for some forest F, how many edges in F are royal. We can do this just by connecting the forest by adding edges from T into F
///We keep track if we're adding royal edges into F, and later subtract if needed

///Phase B: Determine how many royal edges come out of each node. We can determine a leaf of R. We can binary search all edges of the leaf to find the edge
///Remove the leaf from R, and repeat the process until we get the whole tree. This takes NlogN queries

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; ///finding the furthest back edge
		}
	}
	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){ ///forestEdges are the edges we want to test
	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--; ///acounting for royal roads we add from T
		}
	}
	
	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);
	
	
	///Phase A
	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){ ///Encounter an edge we know the status
				if(reference != -1){ ///Reference has been calculated before (see below code)
					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){ ///First edge we know the status, we set this as the reference
					reference = res;
					if(edgeState[removedEdge] == 1) reference++; ///reference is the value we'd expect after removing an unroyal edge
				}
			}
		}
		
		if(minValue == maxValue){
			for(int e : cycleEdges){
				edgeState[e] = 0; ///a cycle cannot consist of all royal edges
			}
		}
		else{
			for(int i = 0;i < (int) cycleEdges.size();i++){
				if(cycleAnswer[i] == maxValue) edgeState[cycleEdges[i]] = 0;
				else edgeState[cycleEdges[i]] = 1;
			}
		}
		
	}
	
	for(int e : treeEdges){
		if(edgeState[e] == -1) edgeState[e] = 1; ///bridges are royal
	}
	
	
	
	///Phase B
	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 leaf = 0;leaf < n;leaf++){
			if(degree[leaf] != 1) continue; ///finding a node with deegre 1
			
			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;
			
			///Binary search the edge that belongs to the royal tree
			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]);
				}
				
				if(queryForest(query) == 1) high = s;
				else low = s;
			}
			
			///removing the leaf from the royal tree
			int leafEdge = allEdges[high];
			degree[edges[leafEdge].first]--; 
			degree[edges[leafEdge].second]--;
			answer.push_back(leafEdge);
		}
	}
	
	return answer;
}

///Side note: in Phase A, we use queryForest despite not knowing the status of the edges in T.
///This is still ok since the additional edges we add are the same each time
# 결과 실행 시간 메모리 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 5 ms 376 KB correct
5 Correct 5 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 6 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 5 ms 376 KB correct
# 결과 실행 시간 메모리 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 5 ms 376 KB correct
5 Correct 5 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 6 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 5 ms 376 KB correct
14 Correct 8 ms 504 KB correct
15 Correct 7 ms 504 KB correct
16 Correct 7 ms 508 KB correct
17 Correct 7 ms 632 KB correct
18 Correct 6 ms 504 KB correct
19 Correct 7 ms 504 KB correct
20 Correct 7 ms 504 KB correct
21 Correct 8 ms 504 KB correct
22 Correct 6 ms 504 KB correct
23 Correct 6 ms 504 KB correct
24 Correct 6 ms 504 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 7 ms 504 KB correct
31 Correct 6 ms 504 KB correct
32 Correct 6 ms 508 KB correct
33 Correct 6 ms 504 KB correct
# 결과 실행 시간 메모리 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 5 ms 376 KB correct
5 Correct 5 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 6 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 5 ms 376 KB correct
14 Correct 8 ms 504 KB correct
15 Correct 7 ms 504 KB correct
16 Correct 7 ms 508 KB correct
17 Correct 7 ms 632 KB correct
18 Correct 6 ms 504 KB correct
19 Correct 7 ms 504 KB correct
20 Correct 7 ms 504 KB correct
21 Correct 8 ms 504 KB correct
22 Correct 6 ms 504 KB correct
23 Correct 6 ms 504 KB correct
24 Correct 6 ms 504 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 7 ms 504 KB correct
31 Correct 6 ms 504 KB correct
32 Correct 6 ms 508 KB correct
33 Correct 6 ms 504 KB correct
34 Correct 50 ms 2040 KB correct
35 Correct 51 ms 1912 KB correct
36 Correct 45 ms 1784 KB correct
37 Correct 21 ms 888 KB correct
38 Correct 51 ms 2008 KB correct
39 Correct 47 ms 1912 KB correct
40 Correct 43 ms 1784 KB correct
41 Correct 54 ms 2040 KB correct
42 Correct 52 ms 2040 KB correct
43 Correct 37 ms 1528 KB correct
44 Correct 36 ms 1400 KB correct
45 Correct 37 ms 1532 KB correct
46 Correct 34 ms 1272 KB correct
47 Correct 26 ms 1144 KB correct
48 Correct 12 ms 888 KB correct
49 Correct 19 ms 888 KB correct
50 Correct 26 ms 1144 KB correct
51 Correct 36 ms 1528 KB correct
52 Correct 34 ms 1400 KB correct
53 Correct 33 ms 1272 KB correct
54 Correct 38 ms 1528 KB correct
55 Correct 40 ms 1400 KB correct
56 Correct 40 ms 1400 KB correct
57 Correct 40 ms 1400 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 632 KB correct
2 Correct 5 ms 376 KB correct
3 Correct 149 ms 4344 KB correct
4 Correct 260 ms 6008 KB correct
5 Correct 233 ms 6008 KB correct
6 Correct 229 ms 6008 KB correct
7 Correct 197 ms 6012 KB correct
8 Correct 206 ms 6008 KB correct
9 Correct 232 ms 6008 KB correct
10 Correct 236 ms 6136 KB correct
11 Correct 227 ms 6008 KB correct
12 Correct 228 ms 6008 KB correct
13 Correct 5 ms 380 KB correct
14 Correct 219 ms 6008 KB correct
15 Correct 229 ms 6088 KB correct
# 결과 실행 시간 메모리 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 5 ms 376 KB correct
5 Correct 5 ms 376 KB correct
6 Correct 5 ms 376 KB correct
7 Correct 5 ms 376 KB correct
8 Correct 6 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 5 ms 376 KB correct
14 Correct 8 ms 504 KB correct
15 Correct 7 ms 504 KB correct
16 Correct 7 ms 508 KB correct
17 Correct 7 ms 632 KB correct
18 Correct 6 ms 504 KB correct
19 Correct 7 ms 504 KB correct
20 Correct 7 ms 504 KB correct
21 Correct 8 ms 504 KB correct
22 Correct 6 ms 504 KB correct
23 Correct 6 ms 504 KB correct
24 Correct 6 ms 504 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 7 ms 504 KB correct
31 Correct 6 ms 504 KB correct
32 Correct 6 ms 508 KB correct
33 Correct 6 ms 504 KB correct
34 Correct 50 ms 2040 KB correct
35 Correct 51 ms 1912 KB correct
36 Correct 45 ms 1784 KB correct
37 Correct 21 ms 888 KB correct
38 Correct 51 ms 2008 KB correct
39 Correct 47 ms 1912 KB correct
40 Correct 43 ms 1784 KB correct
41 Correct 54 ms 2040 KB correct
42 Correct 52 ms 2040 KB correct
43 Correct 37 ms 1528 KB correct
44 Correct 36 ms 1400 KB correct
45 Correct 37 ms 1532 KB correct
46 Correct 34 ms 1272 KB correct
47 Correct 26 ms 1144 KB correct
48 Correct 12 ms 888 KB correct
49 Correct 19 ms 888 KB correct
50 Correct 26 ms 1144 KB correct
51 Correct 36 ms 1528 KB correct
52 Correct 34 ms 1400 KB correct
53 Correct 33 ms 1272 KB correct
54 Correct 38 ms 1528 KB correct
55 Correct 40 ms 1400 KB correct
56 Correct 40 ms 1400 KB correct
57 Correct 40 ms 1400 KB correct
58 Correct 6 ms 632 KB correct
59 Correct 5 ms 376 KB correct
60 Correct 149 ms 4344 KB correct
61 Correct 260 ms 6008 KB correct
62 Correct 233 ms 6008 KB correct
63 Correct 229 ms 6008 KB correct
64 Correct 197 ms 6012 KB correct
65 Correct 206 ms 6008 KB correct
66 Correct 232 ms 6008 KB correct
67 Correct 236 ms 6136 KB correct
68 Correct 227 ms 6008 KB correct
69 Correct 228 ms 6008 KB correct
70 Correct 5 ms 380 KB correct
71 Correct 219 ms 6008 KB correct
72 Correct 229 ms 6088 KB correct
73 Correct 5 ms 376 KB correct
74 Correct 237 ms 6136 KB correct
75 Correct 226 ms 5880 KB correct
76 Correct 122 ms 2936 KB correct
77 Correct 236 ms 6136 KB correct
78 Correct 222 ms 6008 KB correct
79 Correct 229 ms 6008 KB correct
80 Correct 229 ms 6008 KB correct
81 Correct 193 ms 5496 KB correct
82 Correct 225 ms 5880 KB correct
83 Correct 178 ms 4008 KB correct
84 Correct 168 ms 4476 KB correct
85 Correct 162 ms 4216 KB correct
86 Correct 137 ms 3192 KB correct
87 Correct 118 ms 2808 KB correct
88 Correct 113 ms 2428 KB correct
89 Correct 115 ms 2428 KB correct
90 Correct 106 ms 2296 KB correct
91 Correct 58 ms 1528 KB correct
92 Correct 36 ms 1400 KB correct
93 Correct 156 ms 4216 KB correct
94 Correct 142 ms 3312 KB correct
95 Correct 139 ms 3192 KB correct
96 Correct 110 ms 2296 KB correct
97 Correct 112 ms 2424 KB correct
98 Correct 134 ms 2808 KB correct
99 Correct 110 ms 2424 KB correct
100 Correct 78 ms 1656 KB correct
101 Correct 47 ms 1528 KB correct
102 Correct 177 ms 3832 KB correct
103 Correct 173 ms 3832 KB correct
104 Correct 173 ms 3792 KB correct
105 Correct 174 ms 3704 KB correct