답안 #1053140

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1053140 2024-08-11T09:06:38 Z tolbi 분수 공원 (IOI21_parks) C++17
30 / 100
753 ms 116088 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
//void build(std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> b);
struct DSU{
	vector<int> par;
	int tot;
	DSU(int n):tot(n){
		par.resize(n);
		iota(par.begin(), par.end(), 0);
	}
	int find(int node){
		if (par[node]==node) return node;
		return par[node]=find(par[node]);
	}
	bool merge(int a, int b){
		a=find(a);
		b=find(b);
		if (a!=b) tot--;
		else return false;
		par[a]=b;
		return true;
	}
};
int construct_roads(vector<int> x, vector<int> y) {
	bool subtask3 = true;
	int n = x.size();
	for (int i = 0; i < n; i++){
		if (x[i]<2 || x[i]>6) subtask3=false;
	}
	if (subtask3){
		map<pair<int,int>,int> mp;
		for (int i = 0; i < n; ++i)
		{
			mp[{x[i],y[i]}]=i;
		}
		DSU dsu(n);
		vector<int> u;
		vector<int> v;
		vector<int> xa;
		vector<int> ya;
		set<pair<int,int>> occupied;
		vector<int> lol;
		for (int i = 0; i < n; i++){
			if (mp.count({x[i],y[i]-2})){
				if (dsu.merge(i,mp[{x[i],y[i]-2}])){
					u.push_back(mp[{x[i],y[i]-2}]);
					v.push_back(i);
					ya.push_back(y[i]-1);
					if (x[i]==2) xa.push_back(x[i]-1);
					else if (x[i]==6) xa.push_back(x[i]+1);
					else {
						xa.push_back(-1);
						lol.push_back(x[i]);
					}
					if (xa.back()!=-1) occupied.insert({xa.back(),ya.back()});
				}
			}
		}
		for (int i = 0; i < n; ++i)
		{
			if (x[i]==6){
				if (mp.count({x[i]-2,y[i]})){
					if (dsu.merge(i,mp[{x[i]-2,y[i]}])){
						u.push_back(mp[{x[i]-2,y[i]}]);
						v.push_back(i);
						xa.push_back(x[i]-1);
						ya.push_back(y[i]-1);
						occupied.insert({xa.back(),ya.back()});
					}
				}
			}
		}
		for (int i = 0; i < n; ++i)
		{
			if (x[i]!=6){
				if (mp.count({x[i]-2,y[i]})){
					if (dsu.merge(i,mp[{x[i]-2,y[i]}])){
						u.push_back(mp[{x[i]-2,y[i]}]);
						v.push_back(i);
						xa.push_back(x[i]-1);
						if (occupied.find({x[i]+1,y[i]-1})==occupied.end()){
							ya.push_back(y[i]-1);
						}
						else ya.push_back(y[i]+1);
						occupied.insert({xa.back(),ya.back()});
					}
				}
			}
		}
		reverse(lol.begin(), lol.end());
		bool boolean=true;
		for (int i = 0; i < xa.size(); i++){
			if (xa[i]==-1){
				if (occupied.find({lol.back()+1,ya[i]})==occupied.end()){
					xa[i]=lol.back()+1;
				}
				else if (occupied.find({lol.back()-1,ya[i]})==occupied.end()){
					xa[i]=lol.back()-1;
				}
				else {
					boolean=false;
					break;
				}
				lol.pop_back();
			}
		}
		if (dsu.tot==1 && boolean) {
			return build(u,v,xa,ya),1;
		}
		return 0;
	}




	//subtasks 4..5
	DSU dsu(n);
	map<pair<int,int>, int>mp;
	for (int i = 0; i < n; ++i)
	{
		mp[{x[i],y[i]}]=i;
	}
	vector<int> u;
	vector<int> v;
	vector<int> xa(n-1);
	vector<int> ya(n-1);
	vector<set<pair<int,int>>> arr;
	map<pair<int,int>,set<int>> qu;
	for (int i = 0; i < n; ++i)
	{
		if (mp.count({x[i]-2,y[i]})){
			if (dsu.merge(mp[{x[i]-2,y[i]}],i)){
				u.push_back(mp[{x[i]-2,y[i]}]);
				v.push_back(i);
				arr.push_back(set<pair<int,int>>());
				arr.back().insert({x[i]-1,y[i]-1});
				arr.back().insert({x[i]-1,y[i]+1});
				qu[{x[i]-1,y[i]-1}].insert(i);
				qu[{x[i]-1,y[i]+1}].insert(i);
			}
		}
		if (mp.count({x[i],y[i]-2})){
			if (dsu.merge(mp[{x[i],y[i]-2}],i)){
				u.push_back(mp[{x[i],y[i]-2}]);
				v.push_back(i);
				arr.push_back(set<pair<int,int>>());
				arr.back().insert({x[i]-1,y[i]-1});
				arr.back().insert({x[i]+1,y[i]-1});
				qu[{x[i]-1,y[i]-1}].insert(i);
				qu[{x[i]+1,y[i]-1}].insert(i);
			}
		}
	}
	if (dsu.tot!=1) return 0;
	vector<pair<int,int>> iki;
	queue<pair<int,int>> que;
	int kal = n-1;
	for (auto it : qu){
		if (it.second.size()==1) que.push(it.first);
		else iki.push_back(it.first);
	}
	while (kal>0){
		if (!que.size()){
			while (iki.size() && qu[iki.back()].size()<2) iki.pop_back();
			if (iki.size()){
				auto it = qu[iki.back()].begin();
				arr[*it].erase(iki.back());
				qu[iki.back()].erase(it);
				que.push(iki.back());
				iki.pop_back();
			}
			else return 0;
		}
		while (que.size() && qu[que.back()].size()<1) que.pop();
		pair<int,int> pr = que.front();
		que.pop();
		//bu elemani baglicam kalanina bakarik
		kal--;
		int it = *qu[pr].begin();
		xa[it]=pr.first, ya[it]=pr.second;
		for (auto it2 : arr[it]){
			qu[it2].erase(it);
			if (qu[it2].size()==1) que.push(it2);
		}
		while (arr[it].size()) arr[it].erase(arr[it].begin());
	}
	build(u,v,xa,ya);
	return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:93:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |   for (int i = 0; i < xa.size(); i++){
      |                   ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 124 ms 17864 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 38 ms 9924 KB Output is correct
12 Correct 9 ms 3164 KB Output is correct
13 Correct 25 ms 6816 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 119 ms 17848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 124 ms 17864 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 38 ms 9924 KB Output is correct
12 Correct 9 ms 3164 KB Output is correct
13 Correct 25 ms 6816 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 119 ms 17848 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 315 ms 31300 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 860 KB Output is correct
28 Correct 93 ms 12704 KB Output is correct
29 Correct 177 ms 18868 KB Output is correct
30 Correct 229 ms 25168 KB Output is correct
31 Correct 295 ms 31348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 628 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 121 ms 16540 KB Output is correct
46 Correct 190 ms 23716 KB Output is correct
47 Correct 189 ms 23716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 124 ms 17864 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 38 ms 9924 KB Output is correct
12 Correct 9 ms 3164 KB Output is correct
13 Correct 25 ms 6816 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 119 ms 17848 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 315 ms 31300 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 860 KB Output is correct
28 Correct 93 ms 12704 KB Output is correct
29 Correct 177 ms 18868 KB Output is correct
30 Correct 229 ms 25168 KB Output is correct
31 Correct 295 ms 31348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 628 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 121 ms 16540 KB Output is correct
46 Correct 190 ms 23716 KB Output is correct
47 Correct 189 ms 23716 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 313 ms 32804 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 7 ms 1372 KB Output is correct
59 Correct 8 ms 1640 KB Output is correct
60 Correct 125 ms 16576 KB Output is correct
61 Correct 199 ms 22184 KB Output is correct
62 Correct 242 ms 27052 KB Output is correct
63 Correct 301 ms 32932 KB Output is correct
64 Correct 0 ms 344 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 295 ms 35624 KB Output is correct
68 Correct 294 ms 35536 KB Output is correct
69 Correct 310 ms 35492 KB Output is correct
70 Correct 3 ms 860 KB Output is correct
71 Correct 6 ms 1372 KB Output is correct
72 Correct 129 ms 17236 KB Output is correct
73 Correct 224 ms 25768 KB Output is correct
74 Correct 324 ms 34252 KB Output is correct
75 Correct 323 ms 35492 KB Output is correct
76 Correct 286 ms 35496 KB Output is correct
77 Correct 4 ms 1116 KB Output is correct
78 Correct 7 ms 1740 KB Output is correct
79 Correct 146 ms 17220 KB Output is correct
80 Correct 222 ms 25764 KB Output is correct
81 Correct 326 ms 34356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 124 ms 17864 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 38 ms 9924 KB Output is correct
12 Correct 9 ms 3164 KB Output is correct
13 Correct 25 ms 6816 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 119 ms 17848 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Incorrect 0 ms 348 KB a[1] = 0 is not an odd integer
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 124 ms 17864 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 38 ms 9924 KB Output is correct
12 Correct 9 ms 3164 KB Output is correct
13 Correct 25 ms 6816 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 119 ms 17848 KB Output is correct
17 Incorrect 753 ms 116088 KB Tree (a[0], b[0]) = (49999, 100003) is not adjacent to edge between u[0]=148989 @(82420, 100002) and v[0]=0 @(82422, 100002)
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 124 ms 17864 KB Output is correct
10 Correct 7 ms 2396 KB Output is correct
11 Correct 38 ms 9924 KB Output is correct
12 Correct 9 ms 3164 KB Output is correct
13 Correct 25 ms 6816 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 119 ms 17848 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 315 ms 31300 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 860 KB Output is correct
28 Correct 93 ms 12704 KB Output is correct
29 Correct 177 ms 18868 KB Output is correct
30 Correct 229 ms 25168 KB Output is correct
31 Correct 295 ms 31348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1 ms 628 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 121 ms 16540 KB Output is correct
46 Correct 190 ms 23716 KB Output is correct
47 Correct 189 ms 23716 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 313 ms 32804 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 7 ms 1372 KB Output is correct
59 Correct 8 ms 1640 KB Output is correct
60 Correct 125 ms 16576 KB Output is correct
61 Correct 199 ms 22184 KB Output is correct
62 Correct 242 ms 27052 KB Output is correct
63 Correct 301 ms 32932 KB Output is correct
64 Correct 0 ms 344 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 295 ms 35624 KB Output is correct
68 Correct 294 ms 35536 KB Output is correct
69 Correct 310 ms 35492 KB Output is correct
70 Correct 3 ms 860 KB Output is correct
71 Correct 6 ms 1372 KB Output is correct
72 Correct 129 ms 17236 KB Output is correct
73 Correct 224 ms 25768 KB Output is correct
74 Correct 324 ms 34252 KB Output is correct
75 Correct 323 ms 35492 KB Output is correct
76 Correct 286 ms 35496 KB Output is correct
77 Correct 4 ms 1116 KB Output is correct
78 Correct 7 ms 1740 KB Output is correct
79 Correct 146 ms 17220 KB Output is correct
80 Correct 222 ms 25764 KB Output is correct
81 Correct 326 ms 34356 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Incorrect 0 ms 348 KB a[1] = 0 is not an odd integer
84 Halted 0 ms 0 KB -