Submission #1610771


Source Code Expand

#include <cstdio>
#include <cassert>
#include <vector>
#include <algorithm>
using namespace std;

#define iter(i, n) for (int i = 1; i <= n; ++i)

#define NR 101000

const int inf = 1e9;

vector<int> g[NR], add[NR << 2], del[NR << 2];
int n, bg[NR], sz[NR], son[NR], dsz, ex[NR], ey[NR], fa[NR], top[NR], dep[NR];
int t[NR << 2];
bool vis[NR];

#define lx x << 1
#define rx x << 1 | 1
#define RT 1, 1, n
#define SL lx, l, mid
#define SR rx, mid + 1, r

void upd(int x) {
	t[x] = min(t[lx], t[rx]) + add[x].size() - del[x].size();
//	printf("!%d %d\n", x, t[x]);
}

void seg_modify(int x, int l, int r, int ml, int mr, int tag, int d) {
	if (ml <= l && r <= mr) { d == +1 ? add[x].push_back(tag) : del[x].push_back(tag); t[x] += d; return; }

	int mid = (l + r) / 2;
	if (ml <= mid) seg_modify(SL, ml, mr, tag, d);
	if (mr > mid) seg_modify(SR, ml, mr, tag, d);
	upd(x);
}

#define fx top[x]
#define fy top[y]

void modify(int x, int y, int tag, int d) {
	while (fx != fy) {
		if (dep[fx] < dep[fy]) swap(x, y);
		seg_modify(RT, bg[fx], bg[x], tag, d); x = fa[fx];
	}
	if (dep[x] > dep[y]) swap(x, y);
	if (x != y) seg_modify(RT, bg[x] + 1, bg[y], tag, d);
}

void dfs(int x) {
	sz[x] = 1, son[x] = 0;
	for (int v : g[x]) if (v != fa[x]) {
		dep[v] = dep[x] + 1, fa[v] = x, dfs(v), sz[x] += sz[v];
		if (sz[v] > sz[son[x]]) son[x] = v;
	}
}

void bt(int x, int tp) {
	top[x] = tp; bg[x] = ++dsz;
	if (son[x] != 0) bt(son[x], tp);
	for (int v : g[x]) if (v != fa[x] && v != son[x]) bt(v, v);
}

void seg_clr(int x, int l, int r) {
	if (l == r) { t[x] = 2 * inf; return; }
	int mid = (l + r) / 2;
	if (t[lx] < t[rx]) seg_clr(SL);
	else seg_clr(SR);
	upd(x);
}

int seg_min(int x, int l, int r) {
	int res = -1;
	if (add[x].size() == del[x].size()) add[x].clear(), del[x].clear();
	else if (add[x].size() == del[x].size() + 1) {
		for (int i : del[x]) vis[i] = true;
		for (int i : add[x]) if (!vis[i]) res = i;
		for (int i : del[x]) vis[i] = false;	
		
		del[x].clear();
		add[x].clear();
		
		add[x].push_back(res);
	} else assert(0);

	if (res != -1) return res;
	assert(l != r);
	int mid = (l + r) / 2;

	if (t[lx] < t[rx]) return seg_min(SL);
	else return seg_min(SR);
}

int main() {
	scanf("%d", &n);

	iter(i, n - 1) {
		int x, y;
		scanf("%d%d", &x, &y);
		g[x].push_back(y), g[y].push_back(x);
	}

	dfs(1); bt(1, 1);

	iter(i, n) {
		scanf("%d%d", &ex[i], &ey[i]);
		modify(ex[i], ey[i], i, +1);
	}

	

	while (t[1] < inf) {
		//printf("!%d\n", t[1]);
		if (t[1] > 1) { puts("NO"); return 0; }
		if (t[1] == 1) {
			int i = seg_min(RT);
			seg_clr(RT);
			modify(ex[i], ey[i], i, -1);
		} else seg_clr(RT);
	}

	puts("YES");
	return 0;
}

Submission Info

Submission Time
Task E - Blue and Red Tree
User Ketsui
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 2769 Byte
Status AC
Exec Time 605 ms
Memory 60500 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:95:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
./Main.cpp:99:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
                        ^
./Main.cpp:106:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &ex[i], &ey[i]);
                                ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1400 / 1400
Status
AC × 3
AC × 53
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in46.txt, in47.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt AC 9 ms 24320 KB
in10.txt AC 514 ms 43392 KB
in11.txt AC 324 ms 37760 KB
in12.txt AC 317 ms 37632 KB
in13.txt AC 319 ms 37760 KB
in14.txt AC 318 ms 37632 KB
in15.txt AC 319 ms 37760 KB
in16.txt AC 315 ms 37632 KB
in17.txt AC 322 ms 37760 KB
in18.txt AC 316 ms 37760 KB
in19.txt AC 317 ms 37760 KB
in2.txt AC 9 ms 24320 KB
in20.txt AC 321 ms 37760 KB
in21.txt AC 321 ms 38016 KB
in22.txt AC 324 ms 38016 KB
in23.txt AC 331 ms 38016 KB
in24.txt AC 325 ms 38144 KB
in25.txt AC 328 ms 38144 KB
in26.txt AC 321 ms 37632 KB
in27.txt AC 327 ms 37760 KB
in28.txt AC 325 ms 37760 KB
in29.txt AC 339 ms 37760 KB
in3.txt AC 8 ms 24320 KB
in30.txt AC 325 ms 37760 KB
in31.txt AC 605 ms 46716 KB
in32.txt AC 587 ms 46464 KB
in33.txt AC 593 ms 46844 KB
in34.txt AC 596 ms 46848 KB
in35.txt AC 594 ms 46848 KB
in36.txt AC 596 ms 46848 KB
in37.txt AC 597 ms 46972 KB
in38.txt AC 352 ms 60500 KB
in39.txt AC 315 ms 56704 KB
in4.txt AC 537 ms 43632 KB
in40.txt AC 332 ms 57344 KB
in41.txt AC 569 ms 46332 KB
in42.txt AC 553 ms 45692 KB
in43.txt AC 565 ms 45952 KB
in44.txt AC 569 ms 46208 KB
in45.txt AC 551 ms 45440 KB
in46.txt AC 202 ms 48256 KB
in47.txt AC 219 ms 49476 KB
in5.txt AC 524 ms 43644 KB
in6.txt AC 233 ms 49860 KB
in7.txt AC 536 ms 43388 KB
in8.txt AC 513 ms 43648 KB
in9.txt AC 512 ms 43512 KB
sample1.txt AC 9 ms 24320 KB
sample2.txt AC 8 ms 24320 KB
sample3.txt AC 8 ms 24320 KB