結果

問題 No.363 門松サイクル
ユーザー pekempeypekempey
提出日時 2016-04-18 15:37:53
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,707 bytes
コンパイル時間 1,419 ms
コンパイル使用メモリ 163,196 KB
実行使用メモリ 27,896 KB
最終ジャッジ日時 2024-04-15 03:30:51
合計ジャッジ時間 5,406 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
16,480 KB
testcase_01 AC 5 ms
16,700 KB
testcase_02 AC 7 ms
15,796 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 7 ms
16,800 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 143 ms
24,140 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 124 ms
23,636 KB
testcase_16 AC 101 ms
22,064 KB
testcase_17 AC 191 ms
27,896 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 185 ms
26,260 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 5 ms
16,236 KB
testcase_25 AC 5 ms
17,452 KB
testcase_26 AC 123 ms
27,784 KB
testcase_27 AC 123 ms
27,772 KB
testcase_28 AC 163 ms
24,792 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 <= 1) return true;
	bool result = true;
	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