답안 #49588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
49588 2018-05-31T18:30:45 Z WLZ Amusement Park (JOI17_amusement_park) C++17
100 / 100
481 ms 71368 KB
#include "Joi.h"
#include <bits/stdc++.h>
using namespace std;

class DSU {
	private: vector<int> p, rank;
	public:
		DSU(int n) {
			p.assign(n, -1);
			rank.assign(n , 0);
		}
		int root(int x) {
			return p[x] < 0 ? x : p[x] = root(p[x]);
		}
		bool sameSet(int x, int y) {
			return root(x) == root(y);
		}
		void connect(int x, int y) {
			x = root(x);
			y = root(y);
			if (x != y) {
				if (rank[x] > rank[y]) p[y] = x;
				else {
					p[x] = y;
					if (rank[x] == rank[y]) rank[y]++;
				}
			}
		}
};

struct Vertex {
	//bool isLeaf;
	int bitIdx;
	set<int> subTree;
	Vertex() {
		bitIdx = -1;
	}	
};

vector<Vertex> nodes;
vector<vector<int>> g;
vector<int> curTree;
int cnt = 0;

void dfs1(int u, int par) {
	curTree.emplace_back(u);
	cnt++;
	if (cnt >= 60) return;
	//if ((int) g[u].size() == 1) nodes[u].isLeaf = true;
	for (auto& v : g[u]) {
		if (v != par) {
			dfs1(v, u);
			if (cnt >= 60) return;
		}
	}
	return;
}

int findLeaf(const set<int>& st, int exclude) {
	for (auto& u : st) {
		if (u == exclude) continue;
		int cnt = 0;
		for (auto& v : g[u]) {
			if (st.count(v)) cnt++;
		}
		if (cnt <= 1) return u;
	}
}

void dfs2(int u, int par) {
	if (nodes[u].bitIdx == -1) {
		int findV = findLeaf(nodes[par].subTree, par);
		nodes[u].subTree = nodes[par].subTree;
		nodes[u].subTree.erase(findV);
		nodes[u].subTree.insert(u);
		nodes[u].bitIdx = nodes[findV].bitIdx;
	}
	for (auto& v : g[u]) {
		if (v != par) {
			dfs2(v, u);
		}
	}
	return;
}

void Joi(int N, int M, int A[], int B[], long long X, int T) {
	g.resize(N);
	DSU dsu(N);
	nodes.resize(N);
	for (int i = 0; i < M; i++) {
		if (!dsu.sameSet(A[i], B[i])) {
			dsu.connect(A[i], B[i]);
			g[A[i]].emplace_back(B[i]);
			g[B[i]].emplace_back(A[i]);
		}
	}
	dfs1(0, -1);
	for (int i = 0; i < 60; i++) {
		nodes[curTree[i]].bitIdx = i;
		for (int j = 0; j < 60; j++) nodes[curTree[i]].subTree.insert(curTree[j]);
	}
	dfs2(0, -1);
	vector<int> bin;
	for (int i = 0; i < 60; i++) {
		if (X & (1ll << i)) bin.emplace_back(1);
		else bin.emplace_back(0);
	}
	for (int i = 0; i < N; i++) {
		MessageBoard(i, bin[nodes[i].bitIdx]);
	}
	return;
}
#include "Ioi.h"
#include <bits/stdc++.h>
using namespace std;

class DSU {
	private: vector<int> p, rank;
	public:
		DSU(int n) {
			p.assign(n, -1);
			rank.assign(n , 0);
		}
		int root(int x) {
			return p[x] < 0 ? x : p[x] = root(p[x]);
		}
		bool sameSet(int x, int y) {
			return root(x) == root(y);
		}
		void connect(int x, int y) {
			x = root(x);
			y = root(y);
			if (x != y) {
				if (rank[x] > rank[y]) p[y] = x;
				else {
					p[x] = y;
					if (rank[x] == rank[y]) rank[y]++;
				}
			}
		}
};

struct Vertex {
	//bool isLeaf;
	int bitIdx;
	set<int> subTree;
	Vertex() {
		bitIdx = -1;
	}	
};

static vector<vector<int>> g;
static vector<Vertex> nodes;
static vector<int> curTree;
static int cnt = 0;
vector<int> binAns(60, 0);

static void dfs1(int u, int par) {
	curTree.emplace_back(u);
	cnt++;
	if (cnt >= 60) return;
	//if ((int) g[u].size() == 1) nodes[u].isLeaf = true;
	for (auto& v : g[u]) {
		if (v != par) {
			dfs1(v, u);
			if (cnt >= 60) return;
		}
	}
	return;
}

static int findLeaf(const set<int>& st, int exclude) {
	for (auto& u : st) {
		if (u == exclude) continue;
		int cnt = 0;
		for (auto& v : g[u]) {
			if (st.count(v)) cnt++;
		}
		if (cnt <= 1) return u;
	}
}

static void dfs2(int u, int par) {
	if (nodes[u].bitIdx == -1) {
		int findV = findLeaf(nodes[par].subTree, par);
		nodes[u].subTree = nodes[par].subTree;
		nodes[u].subTree.erase(findV);
		nodes[u].subTree.insert(u);
		nodes[u].bitIdx = nodes[findV].bitIdx;
	}
	for (auto& v : g[u]) {
		if (v != par) {
			dfs2(v, u);
		}
	}
	return;
}

void findX(int u, int par, int source) {
	for (auto& v : g[u]) {
		if (v != par) {
			if (nodes[source].subTree.count(v)) {
				binAns[nodes[v].bitIdx] = Move(v);
				findX(v, u, source);
				Move(u);
			}
		}
	}
}

long long Ioi(int N, int M, int A[], int B[], int P, int V, int T) {
	g.resize(N);
	nodes.resize(N);
	DSU dsu(N);
	for (int i = 0; i < M; i++) {
		if (!dsu.sameSet(A[i], B[i])) {
			dsu.connect(A[i], B[i]);
			g[A[i]].push_back(B[i]);
			g[B[i]].push_back(A[i]);
		}
	}
	dfs1(0, -1);
	for (int i = 0; i < 60; i++) {
		nodes[curTree[i]].bitIdx = i;
		for (int j = 0; j < 60; j++) nodes[curTree[i]].subTree.insert(curTree[j]);
	}
	dfs2(0, -1);
	binAns[nodes[P].bitIdx] = V;
	findX(P, -1, P);
	long long ans = 0ll;
	for (int i = 0; i < 60; i++) {
		if (binAns[i] == 1) ans += (1ll << i);
	}
	return ans;
}

Compilation message

Joi.cpp: In function 'int findLeaf(const std::set<int>&, int)':
Joi.cpp:68:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

Ioi.cpp: In function 'int findLeaf(const std::set<int>&, int)':
Ioi.cpp:69:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1124 KB Output is correct
2 Correct 6 ms 1960 KB Output is correct
3 Correct 8 ms 3012 KB Output is correct
4 Correct 6 ms 3040 KB Output is correct
5 Correct 5 ms 3040 KB Output is correct
6 Correct 7 ms 3040 KB Output is correct
7 Correct 9 ms 3324 KB Output is correct
8 Correct 7 ms 3448 KB Output is correct
9 Correct 7 ms 3448 KB Output is correct
10 Correct 6 ms 3448 KB Output is correct
11 Correct 10 ms 3448 KB Output is correct
12 Correct 6 ms 3448 KB Output is correct
13 Correct 8 ms 3552 KB Output is correct
14 Correct 9 ms 3656 KB Output is correct
15 Correct 10 ms 3656 KB Output is correct
16 Correct 10 ms 3656 KB Output is correct
17 Correct 10 ms 3656 KB Output is correct
18 Correct 9 ms 3656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 226 ms 61636 KB Output is correct
2 Correct 258 ms 61720 KB Output is correct
3 Correct 227 ms 61720 KB Output is correct
4 Correct 150 ms 61720 KB Output is correct
5 Correct 220 ms 61852 KB Output is correct
6 Correct 215 ms 61984 KB Output is correct
7 Correct 218 ms 61984 KB Output is correct
8 Correct 216 ms 62048 KB Output is correct
9 Correct 210 ms 62112 KB Output is correct
10 Correct 150 ms 62112 KB Output is correct
11 Correct 161 ms 62112 KB Output is correct
12 Correct 140 ms 62112 KB Output is correct
13 Correct 144 ms 62112 KB Output is correct
14 Correct 161 ms 62112 KB Output is correct
15 Correct 450 ms 62112 KB Output is correct
16 Correct 178 ms 62112 KB Output is correct
17 Correct 169 ms 62112 KB Output is correct
18 Correct 184 ms 62168 KB Output is correct
19 Correct 175 ms 62504 KB Output is correct
20 Correct 143 ms 63256 KB Output is correct
21 Correct 129 ms 63472 KB Output is correct
22 Correct 222 ms 63472 KB Output is correct
23 Correct 222 ms 63788 KB Output is correct
24 Correct 222 ms 63948 KB Output is correct
25 Correct 217 ms 64048 KB Output is correct
26 Correct 241 ms 64356 KB Output is correct
27 Correct 223 ms 64552 KB Output is correct
28 Correct 237 ms 64784 KB Output is correct
29 Correct 202 ms 64976 KB Output is correct
30 Correct 217 ms 64976 KB Output is correct
31 Correct 7 ms 64976 KB Output is correct
32 Correct 7 ms 64976 KB Output is correct
33 Correct 8 ms 64976 KB Output is correct
34 Correct 7 ms 64976 KB Output is correct
35 Correct 6 ms 64976 KB Output is correct
36 Correct 7 ms 64976 KB Output is correct
37 Correct 6 ms 64976 KB Output is correct
38 Correct 6 ms 64976 KB Output is correct
39 Correct 6 ms 64976 KB Output is correct
40 Correct 5 ms 64976 KB Output is correct
41 Correct 6 ms 64976 KB Output is correct
42 Correct 6 ms 64976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 64976 KB Output is correct
2 Correct 6 ms 64976 KB Output is correct
3 Correct 7 ms 64976 KB Output is correct
4 Correct 31 ms 64976 KB Output is correct
5 Correct 30 ms 64976 KB Output is correct
6 Correct 30 ms 64976 KB Output is correct
7 Correct 28 ms 64976 KB Output is correct
8 Correct 28 ms 64976 KB Output is correct
9 Correct 136 ms 65376 KB Output is correct
10 Correct 136 ms 65696 KB Output is correct
11 Correct 148 ms 65700 KB Output is correct
12 Correct 6 ms 65704 KB Output is correct
13 Correct 5 ms 65704 KB Output is correct
14 Correct 5 ms 65704 KB Output is correct
15 Correct 6 ms 65704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 226 ms 65704 KB Output is correct
2 Correct 280 ms 65704 KB Output is correct
3 Correct 229 ms 65704 KB Output is correct
4 Correct 163 ms 65704 KB Output is correct
5 Correct 228 ms 65816 KB Output is correct
6 Correct 223 ms 65928 KB Output is correct
7 Correct 226 ms 65928 KB Output is correct
8 Correct 221 ms 65928 KB Output is correct
9 Correct 231 ms 65928 KB Output is correct
10 Correct 166 ms 65928 KB Output is correct
11 Correct 169 ms 65928 KB Output is correct
12 Correct 151 ms 65928 KB Output is correct
13 Correct 160 ms 65928 KB Output is correct
14 Correct 169 ms 65928 KB Output is correct
15 Correct 187 ms 65928 KB Output is correct
16 Correct 159 ms 65928 KB Output is correct
17 Correct 197 ms 65928 KB Output is correct
18 Correct 180 ms 65928 KB Output is correct
19 Correct 192 ms 65928 KB Output is correct
20 Correct 167 ms 65928 KB Output is correct
21 Correct 131 ms 65928 KB Output is correct
22 Correct 224 ms 65928 KB Output is correct
23 Correct 229 ms 65928 KB Output is correct
24 Correct 218 ms 65928 KB Output is correct
25 Correct 222 ms 65928 KB Output is correct
26 Correct 224 ms 65928 KB Output is correct
27 Correct 224 ms 65928 KB Output is correct
28 Correct 265 ms 65928 KB Output is correct
29 Correct 205 ms 65928 KB Output is correct
30 Correct 209 ms 65928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 65984 KB Output is correct
2 Correct 229 ms 65984 KB Output is correct
3 Correct 231 ms 66348 KB Output is correct
4 Correct 185 ms 66360 KB Output is correct
5 Correct 235 ms 67412 KB Output is correct
6 Correct 227 ms 67680 KB Output is correct
7 Correct 215 ms 67680 KB Output is correct
8 Correct 212 ms 67680 KB Output is correct
9 Correct 224 ms 67680 KB Output is correct
10 Correct 164 ms 67680 KB Output is correct
11 Correct 197 ms 67680 KB Output is correct
12 Correct 152 ms 67680 KB Output is correct
13 Correct 158 ms 67680 KB Output is correct
14 Correct 146 ms 67680 KB Output is correct
15 Correct 168 ms 68040 KB Output is correct
16 Correct 481 ms 68232 KB Output is correct
17 Correct 158 ms 68456 KB Output is correct
18 Correct 169 ms 68648 KB Output is correct
19 Correct 180 ms 68844 KB Output is correct
20 Correct 129 ms 69724 KB Output is correct
21 Correct 136 ms 69928 KB Output is correct
22 Correct 229 ms 70016 KB Output is correct
23 Correct 230 ms 70104 KB Output is correct
24 Correct 227 ms 70244 KB Output is correct
25 Correct 239 ms 70444 KB Output is correct
26 Correct 208 ms 70468 KB Output is correct
27 Correct 222 ms 71048 KB Output is correct
28 Correct 234 ms 71272 KB Output is correct
29 Correct 194 ms 71368 KB Output is correct
30 Correct 205 ms 71368 KB Output is correct
31 Correct 6 ms 71368 KB Output is correct
32 Correct 7 ms 71368 KB Output is correct
33 Correct 9 ms 71368 KB Output is correct
34 Correct 6 ms 71368 KB Output is correct
35 Correct 5 ms 71368 KB Output is correct
36 Correct 5 ms 71368 KB Output is correct
37 Correct 5 ms 71368 KB Output is correct
38 Correct 6 ms 71368 KB Output is correct
39 Correct 4 ms 71368 KB Output is correct
40 Correct 6 ms 71368 KB Output is correct
41 Correct 6 ms 71368 KB Output is correct
42 Correct 7 ms 71368 KB Output is correct