結果

問題 No.363 門松サイクル
ユーザー pekempeypekempey
提出日時 2016-04-18 15:51:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 329 ms / 4,000 ms
コード長 2,713 bytes
コンパイル時間 1,457 ms
コンパイル使用メモリ 162,996 KB
実行使用メモリ 27,892 KB
最終ジャッジ日時 2024-04-15 03:31:37
合計ジャッジ時間 6,928 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
16,528 KB
testcase_01 AC 5 ms
16,188 KB
testcase_02 AC 7 ms
15,932 KB
testcase_03 AC 7 ms
15,920 KB
testcase_04 AC 7 ms
15,928 KB
testcase_05 AC 7 ms
16,924 KB
testcase_06 AC 6 ms
16,788 KB
testcase_07 AC 6 ms
16,056 KB
testcase_08 AC 7 ms
16,636 KB
testcase_09 AC 93 ms
19,504 KB
testcase_10 AC 90 ms
19,604 KB
testcase_11 AC 198 ms
24,036 KB
testcase_12 AC 245 ms
23,520 KB
testcase_13 AC 201 ms
21,428 KB
testcase_14 AC 148 ms
21,016 KB
testcase_15 AC 162 ms
23,512 KB
testcase_16 AC 135 ms
22,740 KB
testcase_17 AC 300 ms
27,784 KB
testcase_18 AC 240 ms
21,704 KB
testcase_19 AC 167 ms
22,240 KB
testcase_20 AC 248 ms
21,756 KB
testcase_21 AC 329 ms
26,384 KB
testcase_22 AC 234 ms
21,632 KB
testcase_23 AC 278 ms
25,344 KB
testcase_24 AC 6 ms
16,868 KB
testcase_25 AC 6 ms
16,476 KB
testcase_26 AC 143 ms
27,828 KB
testcase_27 AC 141 ms
27,892 KB
testcase_28 AC 272 ms
24,640 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:86:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   86 |         for (int i = 0; i < n; i++) scanf("%d", &a[i]);
      |                                     ~~~~~^~~~~~~~~~~~~
main.cpp:90:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   90 |                 scanf("%d %d", &x, &y);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:103:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  103 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

bool isKadomatsu(long long x, long long y, long long z) {
	if (x == y || y == z || x == z) return false;
	if (x > y && y < z) return true;
	if (x < y && y > z) return true;
	return false;
}

int n;
int a[101010];
vector<int> g[101010];

int parent[24][101010];
int depth[101010];
bool kado[24][101010];

void dfs(int curr, int prev) {
	parent[0][curr] = prev;
	kado[0][curr] = true;
	if (prev != -1 && parent[0][prev] != -1) {
		int p = prev;
		int pp = parent[0][prev];
		kado[0][curr] = isKadomatsu(a[curr], a[p], a[pp]);
	}

	for (int next : g[curr]) if (next != prev) {
		depth[next] = depth[curr] + 1;
		dfs(next, curr);
	}
}

void build() {
	dfs(0, -1);

	for (int i = 0; i < 23; i++) {
		for (int j = 0; j < n; j++) {
			if (parent[i][j] != -1) {
				parent[i + 1][j] = parent[i][parent[i][j]];
				kado[i + 1][j] = kado[i][j] && kado[i][parent[i][j]];
			} else {
				parent[i + 1][j] = -1;
				kado[i + 1][j] = kado[i][j];
			}
		}
	}
}

int ancestor(int u, int k) {
	for (int i = 0; i < 24; i++) if (k & 1 << i) u = parent[i][u];
	return u;
}

int lca(int u, int v) {
	if (depth[u] < depth[v]) swap(u, v);
	for (int i = 23; i >= 0; i--) {
		if (depth[u] - depth[v] >= 1 << i) {
			u = parent[i][u];
		}
	}
	if (u == v) return u;
	for (int i = 23; i >= 0; i--) {
		if (parent[i][u] != parent[i][v]) {
			u = parent[i][u];
			v = parent[i][v];
		}
	}
	return parent[0][u];
}

int query(int u, int d) {
	if (d < 0) return true;
	bool result = kado[0][u];
	for (int i = 0; i < 24; i++) if (d & 1 << i) {
		result &= kado[i][u];
		u = parent[i][u];
		result &= kado[0][u];
	}
	return result;
}

int main() {
	cin >> n;

	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	for (int i = 0; i < n - 1; i++) {
		int x, y; 
		scanf("%d %d", &x, &y);
		x--;
		y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	build();

	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--;
		v--;

		int l = lca(u, v);

		bool ans = true;
		if (parent[0][u] == v || parent[0][v] == u) {
			ans = false;
		} else if (l != u && l != v) {
			int p = ancestor(u, depth[u] - depth[l] - 1);
			int q = ancestor(v, depth[v] - depth[l] - 1);
			ans &= isKadomatsu(a[p], a[l], a[q]);
			ans &= isKadomatsu(a[parent[0][u]], a[u], a[v]);
			ans &= isKadomatsu(a[parent[0][v]], a[v], a[u]);
			ans &= query(u, depth[u] - depth[l] - 2);
			ans &= query(v, depth[v] - depth[l] - 2);
		} else {
			if (l == v) swap(u, v);
			int q = ancestor(v, depth[v] - depth[l] - 1);
			ans &= query(v, depth[v] - depth[u] - 2);
			ans &= isKadomatsu(a[q], a[u], a[v]);
			ans &= isKadomatsu(a[parent[0][v]], a[v], a[u]);
		}

		printf("%s\n", ans ? "YES" : "NO");
	}
}
0