Submission #3492847


Source Code Expand

#include<bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
const int inf = 0x3f3f3f3f;

int n, E, Ex, X, Y;
int a[N], b[N], c[N], d[N];
int fir[N], nex[N << 1], arr[N << 1], dep[N], fa[N];
int firx[N], nexx[N << 1], arrx[N << 1], tim[N], tag[N];
queue<int> Q;

inline void Add_Edge(int x, int y) {
	nex[++E] = fir[x];
	fir[x] = E; arr[E] = y;
}

inline void Add_Edge2(int x, int y) {
	nexx[++Ex] = firx[x];
	firx[x] = Ex; arrx[Ex] = y;
}

void dfs(int x) {
	for (int i = fir[x]; i; i = nex[i]) {
		if (arr[i] == fa[x]) continue;
		fa[arr[i]] = x;
		dep[arr[i]] = dep[x] + 1;
		dfs(arr[i]);
	}
}

int dis(int x, int y) {
	if (fa[x] == y || fa[y] == x) return 1;
	if (fa[fa[x]] == y || fa[fa[y]] == x || fa[x] == fa[y]) return 2;
	else return 3;
}

void bfs() {
	tim[X] = 0;
	Q.push(X);
	while (!Q.empty()) {
		int x = Q.front(); Q.pop();
		for (int i = firx[x]; i; i = nexx[i]) {
			if (tim[arrx[i]] < inf) continue;
			if (tim[x] + 1 < dep[arrx[i]] && tim[x] + 1 < tim[arrx[i]]) {
				tim[arrx[i]] = tim[x] + 1;
				Q.push(arrx[i]);
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (tim[i] < inf) {
			if (tag[i]) {
				puts("-1");
				exit(0);
			}
			else ans = max(ans, dep[i] * 2);
		}
	}
	cout << ans << endl;
}

int main() {
	scanf("%d%d%d", &n, &X, &Y);
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &a[i], &b[i]);
	}
	for (int i = 1; i < n; ++i) {
		scanf("%d%d", &c[i], &d[i]);
	}
	for (int i = 1; i < n; ++i) {
		Add_Edge(c[i], d[i]);
		Add_Edge(d[i], c[i]);
	}
	dfs(Y);
	for (int i = 1; i < n; ++i) {
		Add_Edge2(a[i], b[i]);
		Add_Edge2(b[i], a[i]);
		if (dis(a[i], b[i]) == 3) tag[a[i]] = tag[b[i]] = 1;
	}
	memset(tim, 0x3f, sizeof tim);
	bfs();
	return 0;
}

Submission Info

Submission Time
Task E - Sugigma: The Showdown
User Vexoben
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 1810 Byte
Status AC
Exec Time 90 ms
Memory 19968 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:65:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &n, &X, &Y);
                             ^
./Main.cpp:67:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a[i], &b[i]);
                              ^
./Main.cpp:70:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &c[i], &d[i]);
                              ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1400 / 1400
Status
AC × 5
AC × 103
Set Name Test Cases
Sample example0, example1, example2, example3, example4
All doublestar0, doublestar1, doublestar2, doublestar3, doublestar4, doublestar5, doublestar6, doublestar7, example0, example1, example2, example3, example4, giri0, giri1, giri2, giri3, giri4, giri5, giri6, giri7, giri8, giri9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, narashi0, narashi1, narashi2, narashi3, narashi4, narashi5, narashi6, narashi7, narashi8, narashi9, ok0, ok1, ok2, ok3, ok4, ok5, ok6, ok7, ok8, ok9, ouh0, ouh1, ouh2, ouh3, ouh4, ouh5, ouh6, ouh7, ouh8, ouh9, same0, same1, same2, same3, same4, same5, same6, same7, same8, same9, sameline0, sameline1, sameline2, sameline3, sameline4, sameline5, sameline6, sameline7, sameline8, sameline9, star0, star1, star2, star3, star4, star5, star6, star7, star8, star9, supersmall0, supersmall1, supersmall2, supersmall3, supersmall4, supersmall5, supersmall6, supersmall7, supersmall8, supersmall9
Case Name Status Exec Time Memory
doublestar0 AC 76 ms 14464 KB
doublestar1 AC 73 ms 14336 KB
doublestar2 AC 76 ms 14336 KB
doublestar3 AC 76 ms 14208 KB
doublestar4 AC 80 ms 14464 KB
doublestar5 AC 77 ms 14464 KB
doublestar6 AC 72 ms 14208 KB
doublestar7 AC 72 ms 14272 KB
example0 AC 3 ms 11264 KB
example1 AC 4 ms 11264 KB
example2 AC 3 ms 11264 KB
example3 AC 3 ms 11264 KB
example4 AC 3 ms 11264 KB
giri0 AC 80 ms 13696 KB
giri1 AC 79 ms 13568 KB
giri2 AC 81 ms 13696 KB
giri3 AC 79 ms 13568 KB
giri4 AC 81 ms 13696 KB
giri5 AC 83 ms 13696 KB
giri6 AC 82 ms 13696 KB
giri7 AC 80 ms 13568 KB
giri8 AC 82 ms 13696 KB
giri9 AC 80 ms 13696 KB
maxrand0 AC 81 ms 14336 KB
maxrand1 AC 83 ms 14336 KB
maxrand2 AC 83 ms 14336 KB
maxrand3 AC 78 ms 14208 KB
maxrand4 AC 79 ms 14208 KB
maxrand5 AC 81 ms 14208 KB
maxrand6 AC 78 ms 14208 KB
maxrand7 AC 80 ms 14336 KB
maxrand8 AC 77 ms 14208 KB
maxrand9 AC 80 ms 14336 KB
narashi0 AC 80 ms 13568 KB
narashi1 AC 79 ms 13568 KB
narashi2 AC 78 ms 13568 KB
narashi3 AC 80 ms 13696 KB
narashi4 AC 82 ms 13696 KB
narashi5 AC 80 ms 13696 KB
narashi6 AC 82 ms 13696 KB
narashi7 AC 81 ms 13696 KB
narashi8 AC 82 ms 13696 KB
narashi9 AC 79 ms 13568 KB
ok0 AC 85 ms 19200 KB
ok1 AC 86 ms 19840 KB
ok2 AC 84 ms 17792 KB
ok3 AC 87 ms 19968 KB
ok4 AC 84 ms 16512 KB
ok5 AC 87 ms 17664 KB
ok6 AC 86 ms 18176 KB
ok7 AC 83 ms 16512 KB
ok8 AC 90 ms 18816 KB
ok9 AC 84 ms 18432 KB
ouh0 AC 73 ms 14464 KB
ouh1 AC 77 ms 15104 KB
ouh2 AC 75 ms 16000 KB
ouh3 AC 80 ms 16128 KB
ouh4 AC 78 ms 15616 KB
ouh5 AC 81 ms 18560 KB
ouh6 AC 83 ms 19200 KB
ouh7 AC 76 ms 15488 KB
ouh8 AC 84 ms 18048 KB
ouh9 AC 83 ms 19200 KB
same0 AC 81 ms 13568 KB
same1 AC 81 ms 13568 KB
same2 AC 78 ms 13568 KB
same3 AC 78 ms 13568 KB
same4 AC 76 ms 13440 KB
same5 AC 81 ms 13568 KB
same6 AC 77 ms 13440 KB
same7 AC 81 ms 13568 KB
same8 AC 74 ms 13440 KB
same9 AC 83 ms 13568 KB
sameline0 AC 84 ms 18816 KB
sameline1 AC 89 ms 18944 KB
sameline2 AC 82 ms 17536 KB
sameline3 AC 84 ms 18176 KB
sameline4 AC 85 ms 18688 KB
sameline5 AC 85 ms 18560 KB
sameline6 AC 82 ms 16768 KB
sameline7 AC 87 ms 18432 KB
sameline8 AC 85 ms 17664 KB
sameline9 AC 88 ms 16768 KB
star0 AC 72 ms 13440 KB
star1 AC 72 ms 14208 KB
star2 AC 70 ms 13440 KB
star3 AC 71 ms 13440 KB
star4 AC 73 ms 13440 KB
star5 AC 74 ms 14336 KB
star6 AC 72 ms 13440 KB
star7 AC 71 ms 13440 KB
star8 AC 71 ms 13440 KB
star9 AC 75 ms 14336 KB
supersmall0 AC 4 ms 11264 KB
supersmall1 AC 4 ms 11264 KB
supersmall2 AC 4 ms 11264 KB
supersmall3 AC 3 ms 11264 KB
supersmall4 AC 3 ms 11264 KB
supersmall5 AC 3 ms 11264 KB
supersmall6 AC 3 ms 11264 KB
supersmall7 AC 4 ms 11264 KB
supersmall8 AC 3 ms 11264 KB
supersmall9 AC 4 ms 11264 KB