답안 #103717

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
103717 2019-04-02T08:37:56 Z E869120 Mousetrap (CEOI17_mousetrap) C++14
0 / 100
1185 ms 260944 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

const int MAX_N = (1 << 20);

int N, A, B, E[MAX_N], F[MAX_N], dist[MAX_N], dp[MAX_N][22], val[MAX_N], pl[MAX_N]; bool used[MAX_N];
vector<int>X[MAX_N], Z[MAX_N], lis[MAX_N];

void dfs1(int pos, int dep) {
	used[pos] = true; dist[pos] = dep;
	for (int i = 0; i < X[pos].size(); i++) {
		if (used[X[pos][i]] == true) continue;
		dp[X[pos][i]][0] = pos;
		dfs1(X[pos][i], dep + 1);
	}
}

int prevs(int pos, int x) {
	for (int i = 21; i >= 0; i--) {
		if (x >= (1 << i)) { pos = dp[pos][i]; x -= (1 << i); }
	}
	return pos;
}

int lca(int u, int v) {
	if (dist[u] > dist[v]) swap(u, v);
	v = prevs(v, dist[v] - dist[u]);
	if (u == v) return u;

	for (int i = 21; i >= 0; i--) {
		if (dp[u][i] != dp[v][i]) {
			u = dp[u][i];
			v = dp[v][i];
		}
	}
	return dp[u][0];
}

int ret[MAX_N];

void dfs2(int pos, int dep) {
	ret[pos] = dep; used[pos] = true;
	for (int i = 0; i < Z[pos].size(); i++) {
		if (used[Z[pos][i]] == true) continue;
		dfs2(Z[pos][i], dep + Z[pos].size());
	}
}

int main() {
	scanf("%d%d%d", &N, &A, &B);
	for (int i = 1; i <= N - 1; i++) {
		scanf("%d%d", &E[i], &F[i]);
		X[E[i]].push_back(F[i]);
		X[F[i]].push_back(E[i]);
	}
	dfs1(B, 0);
	for (int i = 0; i < 21; i++) {
		for (int j = 1; j <= N; j++) dp[j][i + 1] = dp[dp[j][i]][i];
	}

	for (int i = 1; i <= N; i++) {
		int v = lca(A, i);
		val[i] = dist[v];
		if (v == i) pl[dist[v]] = i;
		else lis[dist[v]].push_back(i);
	}

	int FinalAns = 0;

	for (int i = 1; i <= N; i++) used[i] = false;
	for (int i = 0; i < dist[A]; i++) {
		for (int j = 0; j < lis[i].size(); j++) {
			int u = lis[i][j], v = dp[u][0];
			Z[u].push_back(v);
			Z[v].push_back(u);
		}
		dfs2(pl[i], 0);
		int rem = (1 << 30);
		if (Z[pl[i]].size() == 1) rem = 1;
		if (Z[pl[i]].size() <= 0) rem = 0;

		for (int j = 0; j < lis[i].size(); j++) {
			int cost = (1 << 30);
			if (Z[lis[i][j]].size() <= 1) cost = 0;
			if (Z[lis[i][j]].size() == 2) cost = 1;
			rem = min(rem, ret[lis[i][j]] + cost);
		}
		FinalAns += rem;
	}
	cout << FinalAns << endl;
	return 0;
}

Compilation message

mousetrap.cpp:5:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning (disable: 4996)
 
mousetrap.cpp: In function 'void dfs1(int, int)':
mousetrap.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < X[pos].size(); i++) {
                  ~~^~~~~~~~~~~~~~~
mousetrap.cpp: In function 'void dfs2(int, int)':
mousetrap.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < Z[pos].size(); i++) {
                  ~~^~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:75:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < lis[i].size(); j++) {
                   ~~^~~~~~~~~~~~~~~
mousetrap.cpp:85:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < lis[i].size(); j++) {
                   ~~^~~~~~~~~~~~~~~
mousetrap.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &N, &A, &B);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &E[i], &F[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 74232 KB Output is correct
2 Correct 72 ms 74280 KB Output is correct
3 Correct 79 ms 74360 KB Output is correct
4 Correct 69 ms 74232 KB Output is correct
5 Incorrect 70 ms 74232 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1185 ms 260944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 74232 KB Output is correct
2 Correct 72 ms 74280 KB Output is correct
3 Correct 79 ms 74360 KB Output is correct
4 Correct 69 ms 74232 KB Output is correct
5 Incorrect 70 ms 74232 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 74232 KB Output is correct
2 Correct 72 ms 74280 KB Output is correct
3 Correct 79 ms 74360 KB Output is correct
4 Correct 69 ms 74232 KB Output is correct
5 Incorrect 70 ms 74232 KB Output isn't correct
6 Halted 0 ms 0 KB -