Submission #636584

#TimeUsernameProblemLanguageResultExecution timeMemory
636584rainboyJail (JOI22_jail)C11
5 / 100
75 ms16240 KiB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	120000

int *ej[N], eo[N], *eh[N], eo_[N], fo_[N];

void append(int **ej, int *eo, int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int dd[N], pp[N];

void dfs1(int p, int i, int d) {
	int o;

	dd[i] = d, pp[i] = p;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs1(i, j, d + 1);
	}
}

void dfs2(int h) {
	int o;

	if (fo_[h])
		return;
	fo_[h] = -1;
	for (o = eo_[h]; o--; ) {
		int h_ = eh[h][o];

		fo_[h_]--, dfs2(h_);
	}
}

int main() {
	int t;

	scanf("%d", &t);
	while (t--) {
		static int ii[N], jj[N], hhi[N], hhj[N];
		int n, m, h, h_, i, j, line, yes;

		scanf("%d", &n);
		for (i = 0; i < n; i++)
			ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
		line = 1;
		for (h = 0; h < n - 1; h++) {
			scanf("%d%d", &i, &j), i--, j--;
			if (i + 1 != j)
				line = 0;
			append(ej, eo, i, j), append(ej, eo, j, i);
		}
		dfs1(-1, 0, 0);
		if (line) {
			scanf("%d", &m);
			memset(jj, -1, n * sizeof *jj);
			while (m--) {
				scanf("%d%d", &i, &j), i--, j--;
				jj[i] = j;
			}
			yes = 1;
			for (i = 0, j = -1; i < n; i++)
				if (jj[i] != -1) {
					if (j >= jj[i]) {
						yes = 0;
						break;
					}
					j = jj[i];
				}
			printf(yes ? "Yes\n" : "No\n");
		} else {
			scanf("%d", &m);
			memset(hhi, -1, n * sizeof *hhi), memset(hhj, -1, n * sizeof *hhj);
			for (h = 0; h < m; h++) {
				scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
				hhi[ii[h]] = hhj[jj[h]] = h;
			}
			for (h = 0; h < m; h++)
				eh[h] = (int *) malloc(2 * sizeof *eh[h]), eo_[h] = 0, fo_[h] = 0;
			for (h = 0; h < m; h++) {
				i = ii[h], j = jj[h];
				while (i != j)
					if (dd[i] > dd[j]) {
						h_ = hhi[i];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h_, h), fo_[h]++;
						h_ = hhj[i];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h, h_), fo_[h_]++;
						i = pp[i];
					} else {
						h_ = hhi[j];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h_, h), fo_[h]++;
						h_ = hhj[j];
						if (h_ != -1 && h_ != h)
							append(eh, eo_, h, h_), fo_[h_]++;
						j = pp[j];
					}
				if (hhi[i] != -1)
					append(eh, eo_, hhi[i], h);
				if (hhj[i] != -1)
					append(eh, eo_, hhj[i], h);
			}
			for (h = 0; h < m; h++)
				dfs2(h);
			yes = 1;
			for (h = 0; h < m; h++)
				if (fo_[h] > 0) {
					yes = 0;
					break;
				}
			printf(yes ? "Yes\n" : "No\n");
			for (h = 0; h < m; h++)
				free(eh[h]);
		}
		for (i = 0; i < n; i++)
			free(ej[i]);
	}
	return 0;
}

Compilation message (stderr)

jail.c: In function 'append':
jail.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
jail.c: In function 'main':
jail.c:47:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
jail.c:52:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |   scanf("%d", &n);
      |   ^~~~~~~~~~~~~~~
jail.c:57:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |    scanf("%d%d", &i, &j), i--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
jail.c:64:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |    scanf("%d", &m);
      |    ^~~~~~~~~~~~~~~
jail.c:67:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     scanf("%d%d", &i, &j), i--, j--;
      |     ^~~~~~~~~~~~~~~~~~~~~
jail.c:81:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |    scanf("%d", &m);
      |    ^~~~~~~~~~~~~~~
jail.c:84:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     scanf("%d%d", &ii[h], &jj[h]), ii[h]--, jj[h]--;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...