답안 #544787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544787 2022-04-02T16:45:52 Z rainboy Traffic (CEOI11_tra) C
100 / 100
938 ms 66168 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define N	300000
 
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
 
unsigned int X = 12345;
 
int rand_() {
	return (X *= 3) >> 1;
}
 
int *ej[N], eo[N], *fi[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 xx[N], yy[N], ll[N], rr[N];
 
void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
 
		while (j < k)
			if (yy[ii[j]] == yy[i_])
				j++;
			else if (yy[ii[j]] > yy[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}
 
char visited[N]; int qu[N], cnt;
 
void dfs1(int j) {
	int o;
 
	if (visited[j])
		return;
	visited[j] = 1;
	for (o = fo[j]; o--; ) {
		int i = fi[j][o];
 
		dfs1(i);
	}
	qu[--cnt] = j;
}
 
int qu_[N], cnt_, l, r;
 
void dfs2(int i) {
	int o;
 
	l = min(l, ll[i]), r = max(r, rr[i]);
	if (visited[i])
		return;
	visited[i] = 1, qu_[cnt_++] = i;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];
 
		dfs2(j);
	}
}
 
int main() {
	static int ii0[N], iia[N], kk[N];
	int n, n0, na, m, a, b, h, i, j, k;
 
	scanf("%d%d%d%d", &n, &m, &a, &b);
	n0 = na = 0;
	for (i = 0; i < n; i++) {
		scanf("%d%d", &xx[i], &yy[i]);
		if (xx[i] == 0)
			ii0[n0++] = i;
		else if (xx[i] == a)
			iia[na++] = i;
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		fi[i] = (int *) malloc(2 * sizeof *fi[i]);
	}
	for (h = 0; h < m; h++) {
		int t;
 
		scanf("%d%d%d", &i, &j, &t), i--, j--;
		append(ej, eo, i, j), append(fi, fo, j, i);
		if (t == 2)
			append(ej, eo, j, i), append(fi, fo, i, j);
	}
	sort(ii0, 0, n0);
	sort(iia, 0, na);
	for (h = 0; h < n0; h++)
		dfs2(ii0[h]);
	for (h = 0, k = 0; h < na; h++) {
		if (visited[iia[h]])
			k++;
		kk[h] = k;
	}
	memset(visited, 0, n * sizeof *visited);
	for (i = 0; i < n; i++)
		ll[i] = na, rr[i] = -1;
	for (h = 0; h < na; h++)
		ll[iia[h]] = rr[iia[h]] = h;
	cnt = n;
	for (i = 0; i < n; i++)
		dfs1(i);
	for (i = 0; i < n; i++)
		visited[i] = 0;
	memset(visited, 0, n * sizeof *visited);
	for (h = 0; h < n; h++)
		if (!visited[qu[h]]) {
			l = na, r = -1, cnt_ = 0;
			dfs2(qu[h]);
			while (cnt_--)
				ll[qu_[cnt_]] = l, rr[qu_[cnt_]] = r;
		}
	for (h = 0; h < n0; h++) {
		i = ii0[h];
		printf("%d\n", ll[i] > rr[i] ? 0 : kk[rr[i]] - (ll[i] == 0 ? 0 : kk[ll[i] - 1]));
	}
	return 0;
}

Compilation message

tra.c: In function 'append':
tra.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
tra.c: In function 'main':
tra.c:83:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |  scanf("%d%d%d%d", &n, &m, &a, &b);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tra.c:86:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tra.c:97:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |   scanf("%d%d%d", &i, &j, &t), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/string.h:495,
                 from tra.c:3:
In function 'memset',
    inlined from 'main' at tra.c:121:2:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:71:10: warning: '__builtin___memset_chk' specified size between 18446744071562067968 and 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
   71 |   return __builtin___memset_chk (__dest, __ch, __len, __bos0 (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 440 KB Output is correct
3 Correct 1 ms 436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 780 KB Output is correct
2 Correct 5 ms 1084 KB Output is correct
3 Correct 4 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 5068 KB Output is correct
2 Correct 79 ms 8396 KB Output is correct
3 Correct 37 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 7076 KB Output is correct
2 Correct 87 ms 10484 KB Output is correct
3 Correct 66 ms 6992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 13628 KB Output is correct
2 Correct 146 ms 17172 KB Output is correct
3 Correct 188 ms 16204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 15596 KB Output is correct
2 Correct 131 ms 16940 KB Output is correct
3 Correct 223 ms 17004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 263 ms 26256 KB Output is correct
2 Correct 259 ms 32444 KB Output is correct
3 Correct 514 ms 31208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 389 ms 42416 KB Output is correct
2 Correct 508 ms 49076 KB Output is correct
3 Correct 483 ms 33808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 824 ms 53260 KB Output is correct
2 Correct 505 ms 49428 KB Output is correct
3 Correct 894 ms 52316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 231 ms 40108 KB Output is correct
2 Correct 589 ms 52416 KB Output is correct
3 Correct 735 ms 47008 KB Output is correct
4 Correct 938 ms 66168 KB Output is correct