답안 #594504

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594504 2022-07-12T14:55:01 Z FatihSolak Simurgh (IOI17_simurgh) C++17
70 / 100
1406 ms 31340 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define N 500
using namespace std;
int deg[N];
set<int> adj2[N];
set<int> adj[N];
int edge[N][N];
int par[N];
bool can[N*N];
int in_answer[N*N];
int find(int a){
	if(par[a] == a)return a;
	return par[a] = find(par[a]);
}
bool merge(int a,int b){
	a = find(a);
	b = find(b);
	if(a == b)
		return 0;
	par[b] = a;
	return 1;
}
vector<int> edges;
int dfspar[N];
void dfs(int v,int target,int pr){
	//cout << v << " " << target << " " << pr << endl;
	dfspar[v] = pr;
	if(v == target){
		while(dfspar[v] != -1){
			edges.push_back(edge[v][dfspar[v]]);
			v = dfspar[v];
		}
		return;
	}
	for(auto u:adj[v]){
		if(u == pr)continue;
		dfs(u,target,v);
	}
}
map<vector<int>,int> mp;
int get(set<int> s){
	vector<int> r;
	for(auto u:s)r.push_back(u);
	if(mp.count(r))
		return mp[r];
	return mp[r] = count_common_roads(r);
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
	int m = u.size();
	for(int i = 0;i<n;i++){
		for(int j = 0;j<n;j++){
			edge[i][j] = -1;
		}
		par[i] = i;
	}
	for(int i = 0;i<m;i++){
		can[i] = 1;
		in_answer[i] = -1;
		edge[u[i]][v[i]] = i;
		edge[v[i]][u[i]] = i;
	}
	if(n >= 4 && m == n * (n-1) / 2){
		set<int> r;
		for(int i = 0;i<n-1;i++){
			adj[i].insert(i+1);
			adj[i+1].insert(i);
			r.insert(edge[i][i+1]);
		}
		int last = 2;
		while(last < n){
			int best = get(r);
			edges.clear();
			dfs(0,last,-1);
			vector<int> known;
			vector<int> unknown;
			for(auto x:edges){
				if(in_answer[x] == -1){
					unknown.push_back(x);
				}
				else known.push_back(x);
			}
			bool add_cur = 0;
			for(auto x:unknown){
				r.erase(x);
				r.insert(edge[0][last]);
				if(get(r) > best){
					add_cur = 1;
				}
				r.erase(edge[0][last]);
				r.insert(x);
			}
			if(known.size()){
				r.erase(known[0]);
				r.insert(edge[0][last]);
				if(get(r) > best - in_answer[known[0]]){
					add_cur = 1;
				}
				r.erase(edge[0][last]);
				r.insert(known[0]);
			}
			for(auto x:unknown){
				r.erase(x);
				r.insert(edge[0][last]);
				if(best - (get(r) - add_cur) > 0){
					in_answer[x] = 1;
				}
				else in_answer[x] = 0;
				r.erase(edge[0][last]);
				r.insert(x);
			}
			if(add_cur){
				for(auto x:edges){
					if(in_answer[x] == 0){
						r.erase(x);
						r.insert(edge[0][last]);
						adj[u[x]].erase(v[x]);
						adj[v[x]].erase(u[x]);
						adj[0].insert(last);
						adj[last].insert(0);
						in_answer[edge[0][last]] = 1;
						break;
					}
				}
			}
			last++;
		}
		vector<int> ans;
		for(int i = 0;i<n;i++){
			set<int> s;
			for(int j = 0;j<n;j++){
				if(i == j)continue;
				//cout << i << " " << j << " " << edge[i][j] << endl;
				s.insert(edge[i][j]);
			}
			deg[i] = get(s);
			//cout << i << " " << deg[i] << endl;
			if(i == 0){
				for(int j = 1;j<n;j++){
					if(in_answer[edge[i][j]] == 1){
						//cout << j << endl;
						adj2[i].insert(j);
						adj2[j].insert(i);
						ans.push_back(edge[i][j]);
					}
				}
			}
		}
		assert(deg[0] == adj2[0].size());
		for(int i = 1;i<n;i++){
			vector<int> points;
			for(int j = 0;j<n;j++){
				if(i == j)continue;
				points.push_back(j);
			}
			while(adj2[i].size() != deg[i]){
				int l = 0, r= n-1;
				while(points[l] <= i || (adj2[i].size() && points[l] <= *adj2[i].rbegin())){
					l++;
				}
				while(l < r){
					int m = (l + r)/2;
					int cnt = 0;
					set<int> s;
					for(int j = 0;j<n-1;j++){
						if(l <= j && j <= m){
							s.insert(edge[points[j]][0]);
							cnt += adj2[0].count(points[j]);
						}
						else{
							s.insert(edge[i][points[j]]);
						}
					}
					if(get(s) - cnt < deg[i]){
						r = m;
					}
					else l = m + 1;
				}
				adj2[i].insert(points[l]);
				adj2[points[l]].insert(i);
				ans.push_back(edge[i][points[l]]);
			}
		}
		return ans;
	}
	set<int> r;
	//cout << endl;
	for(int i = 0;i<m;i++){
		//cout << u[i] << " " << v[i] << endl;
		if(merge(u[i],v[i])){
			//cout << i << endl;
			can[i] = 0;
			adj[u[i]].insert(v[i]);
			adj[v[i]].insert(u[i]);
			r.insert(i);
		}
	}
	// cout << "hey" << endl;
	// for(auto u:r){
	// 	cout << u << " ";
	// }
	// cout << endl;
	int last = 0;
	while(get(r) != n-1){
		while(!can[last])
			last++;
		//cout << "hey" << endl;
		int best = get(r);
		edges.clear();
		//cout << "hey" << endl;
		dfs(u[last],v[last],-1);
		// cout << "hey" << endl;
		// for(auto u:r){
		// 	cout << u << " ";
		// }
		// cout << endl;
		// for(auto x:edges){
		// 	cout << x << " ";
		// }
		// cout << endl;
		// cout << last << endl;
		vector<int> known;
		vector<int> unknown;
		for(auto x:edges){
			if(in_answer[x] == -1){
				unknown.push_back(x);
			}
			else known.push_back(x);
		}
		bool add_cur = 0;
		for(auto x:unknown){
			r.erase(x);
			r.insert(last);
			if(get(r) > best){
				add_cur = 1;
			}
			r.erase(last);
			r.insert(x);
		}
		if(known.size()){
			r.erase(known[0]);
			r.insert(last);
			if(get(r) > best - in_answer[known[0]]){
				add_cur = 1;
			}
			r.erase(last);
			r.insert(known[0]);
		}
		for(auto x:unknown){
			r.erase(x);
			r.insert(last);
			if(best - (get(r) - add_cur) > 0){
				in_answer[x] = 1;
			}
			else in_answer[x] = 0;
			r.erase(last);
			r.insert(x);
		}
		if(add_cur){
			for(auto x:edges){
				if(in_answer[x] == 0){
					r.erase(x);
				r.insert(last);
					adj[u[x]].erase(v[x]);
					adj[v[x]].erase(u[x]);
					adj[u[last]].insert(v[last]);
					adj[v[last]].insert(u[last]);
					in_answer[last] = 1;
					break;
				}
			}
		}
		last++;
	}
	vector<int> ret;
	for(auto u:r){
		ret.push_back(u);
	}
	return ret;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from simurgh.cpp:2:
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:149:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |   assert(deg[0] == adj2[0].size());
      |          ~~~~~~~^~~~~~~~~~~~~~~~~
simurgh.cpp:156:25: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  156 |    while(adj2[i].size() != deg[i]){
      |          ~~~~~~~~~~~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 340 KB correct
4 Correct 1 ms 340 KB correct
5 Correct 0 ms 364 KB correct
6 Correct 1 ms 340 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 1 ms 340 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 340 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 340 KB correct
4 Correct 1 ms 340 KB correct
5 Correct 0 ms 364 KB correct
6 Correct 1 ms 340 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 1 ms 340 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 340 KB correct
14 Correct 4 ms 596 KB correct
15 Correct 5 ms 624 KB correct
16 Correct 4 ms 496 KB correct
17 Correct 13 ms 724 KB correct
18 Correct 4 ms 596 KB correct
19 Correct 11 ms 756 KB correct
20 Correct 10 ms 704 KB correct
21 Correct 11 ms 724 KB correct
22 Correct 1 ms 468 KB correct
23 Correct 1 ms 468 KB correct
24 Correct 1 ms 468 KB correct
25 Correct 1 ms 468 KB correct
26 Correct 1 ms 468 KB correct
27 Correct 1 ms 468 KB correct
28 Correct 1 ms 468 KB correct
29 Correct 1 ms 360 KB correct
30 Correct 1 ms 484 KB correct
31 Correct 1 ms 468 KB correct
32 Correct 1 ms 492 KB correct
33 Correct 1 ms 468 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 340 KB correct
4 Correct 1 ms 340 KB correct
5 Correct 0 ms 364 KB correct
6 Correct 1 ms 340 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 1 ms 340 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 340 KB correct
14 Correct 4 ms 596 KB correct
15 Correct 5 ms 624 KB correct
16 Correct 4 ms 496 KB correct
17 Correct 13 ms 724 KB correct
18 Correct 4 ms 596 KB correct
19 Correct 11 ms 756 KB correct
20 Correct 10 ms 704 KB correct
21 Correct 11 ms 724 KB correct
22 Correct 1 ms 468 KB correct
23 Correct 1 ms 468 KB correct
24 Correct 1 ms 468 KB correct
25 Correct 1 ms 468 KB correct
26 Correct 1 ms 468 KB correct
27 Correct 1 ms 468 KB correct
28 Correct 1 ms 468 KB correct
29 Correct 1 ms 360 KB correct
30 Correct 1 ms 484 KB correct
31 Correct 1 ms 468 KB correct
32 Correct 1 ms 492 KB correct
33 Correct 1 ms 468 KB correct
34 Correct 97 ms 4108 KB correct
35 Correct 1406 ms 30244 KB correct
36 Correct 892 ms 20876 KB correct
37 Correct 51 ms 2120 KB correct
38 Correct 1405 ms 30968 KB correct
39 Correct 1135 ms 26508 KB correct
40 Correct 910 ms 21048 KB correct
41 Correct 104 ms 3980 KB correct
42 Correct 1360 ms 31340 KB correct
43 Correct 4 ms 1272 KB correct
44 Correct 3 ms 1108 KB correct
45 Correct 4 ms 1208 KB correct
46 Correct 3 ms 1108 KB correct
47 Correct 2 ms 980 KB correct
48 Correct 1 ms 852 KB correct
49 Correct 2 ms 852 KB correct
50 Correct 2 ms 1108 KB correct
51 Correct 5 ms 1268 KB correct
52 Correct 3 ms 1108 KB correct
53 Correct 3 ms 1108 KB correct
54 Correct 4 ms 1236 KB correct
55 Correct 3 ms 1216 KB correct
56 Correct 4 ms 1236 KB correct
57 Correct 4 ms 1364 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB correct
2 Correct 1 ms 340 KB correct
3 Correct 343 ms 10368 KB correct
4 Correct 541 ms 16340 KB correct
5 Correct 532 ms 16300 KB correct
6 Correct 500 ms 15968 KB correct
7 Correct 579 ms 16152 KB correct
8 Correct 510 ms 16052 KB correct
9 Correct 508 ms 16184 KB correct
10 Correct 553 ms 16324 KB correct
11 Correct 549 ms 16164 KB correct
12 Correct 545 ms 16256 KB correct
13 Correct 1 ms 340 KB correct
14 Correct 526 ms 15988 KB correct
15 Correct 527 ms 16304 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 340 KB correct
4 Correct 1 ms 340 KB correct
5 Correct 0 ms 364 KB correct
6 Correct 1 ms 340 KB correct
7 Correct 1 ms 340 KB correct
8 Correct 1 ms 340 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 1 ms 340 KB correct
11 Correct 1 ms 340 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 0 ms 340 KB correct
14 Correct 4 ms 596 KB correct
15 Correct 5 ms 624 KB correct
16 Correct 4 ms 496 KB correct
17 Correct 13 ms 724 KB correct
18 Correct 4 ms 596 KB correct
19 Correct 11 ms 756 KB correct
20 Correct 10 ms 704 KB correct
21 Correct 11 ms 724 KB correct
22 Correct 1 ms 468 KB correct
23 Correct 1 ms 468 KB correct
24 Correct 1 ms 468 KB correct
25 Correct 1 ms 468 KB correct
26 Correct 1 ms 468 KB correct
27 Correct 1 ms 468 KB correct
28 Correct 1 ms 468 KB correct
29 Correct 1 ms 360 KB correct
30 Correct 1 ms 484 KB correct
31 Correct 1 ms 468 KB correct
32 Correct 1 ms 492 KB correct
33 Correct 1 ms 468 KB correct
34 Correct 97 ms 4108 KB correct
35 Correct 1406 ms 30244 KB correct
36 Correct 892 ms 20876 KB correct
37 Correct 51 ms 2120 KB correct
38 Correct 1405 ms 30968 KB correct
39 Correct 1135 ms 26508 KB correct
40 Correct 910 ms 21048 KB correct
41 Correct 104 ms 3980 KB correct
42 Correct 1360 ms 31340 KB correct
43 Correct 4 ms 1272 KB correct
44 Correct 3 ms 1108 KB correct
45 Correct 4 ms 1208 KB correct
46 Correct 3 ms 1108 KB correct
47 Correct 2 ms 980 KB correct
48 Correct 1 ms 852 KB correct
49 Correct 2 ms 852 KB correct
50 Correct 2 ms 1108 KB correct
51 Correct 5 ms 1268 KB correct
52 Correct 3 ms 1108 KB correct
53 Correct 3 ms 1108 KB correct
54 Correct 4 ms 1236 KB correct
55 Correct 3 ms 1216 KB correct
56 Correct 4 ms 1236 KB correct
57 Correct 4 ms 1364 KB correct
58 Correct 0 ms 340 KB correct
59 Correct 1 ms 340 KB correct
60 Correct 343 ms 10368 KB correct
61 Correct 541 ms 16340 KB correct
62 Correct 532 ms 16300 KB correct
63 Correct 500 ms 15968 KB correct
64 Correct 579 ms 16152 KB correct
65 Correct 510 ms 16052 KB correct
66 Correct 508 ms 16184 KB correct
67 Correct 553 ms 16324 KB correct
68 Correct 549 ms 16164 KB correct
69 Correct 545 ms 16256 KB correct
70 Correct 1 ms 340 KB correct
71 Correct 526 ms 15988 KB correct
72 Correct 527 ms 16304 KB correct
73 Correct 1 ms 340 KB correct
74 Correct 515 ms 16192 KB correct
75 Incorrect 900 ms 21364 KB WA in grader: NO
76 Halted 0 ms 0 KB -