Submission #536323

# Submission time Handle Problem Language Result Execution time Memory
536323 2022-03-12T21:46:24 Z rainboy 우호 조약 체결 (JOI14_friends) C
100 / 100
77 ms 12764 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000

int *ej[N], eo[N];

void append(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 ds[N], sz[N];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

void join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j, sz[j] += sz[i];
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i, sz[i] += sz[j];
	}
}

char visited[N];

void dfs(int i) {
	int o;

	if (visited[i])
		return;
	visited[i] = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		join(i, j);
		dfs(j);
	}
}

int main() {
	int n, m, i, j, j_, o;
	long long ans;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	while (m--) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j);
	}
	memset(ds, -1, n * sizeof *ds);
	for (i = 0; i < n; i++)
		sz[i] = 1;
	for (i = 0; i < n; i++)
		if (eo[i] >= 2) {
			j_ = ej[i][0];
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				join(j, j_);
			}
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				dfs(j);
			}
		}
	ans = 0;
	for (i = 0; i < n; i++)
		if (ds[i] < 0)
			ans += sz[i] == 1 ? eo[i] : (long long) sz[i] * (sz[i] - 1);
	printf("%lld\n", ans);
	return 0;
}

Compilation message

friends.c: In function 'append':
friends.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   12 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
friends.c: In function 'main':
friends.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
friends.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 8 ms 812 KB Output is correct
4 Correct 37 ms 1660 KB Output is correct
5 Correct 25 ms 1384 KB Output is correct
6 Correct 43 ms 2064 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 7 ms 596 KB Output is correct
10 Correct 12 ms 1004 KB Output is correct
11 Correct 60 ms 2108 KB Output is correct
12 Correct 5 ms 724 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 3 ms 596 KB Output is correct
16 Correct 8 ms 852 KB Output is correct
17 Correct 44 ms 2020 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 2 ms 852 KB Output is correct
20 Correct 45 ms 2040 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 1608 KB Output is correct
2 Correct 71 ms 9608 KB Output is correct
3 Correct 70 ms 9568 KB Output is correct
4 Correct 37 ms 6372 KB Output is correct
5 Correct 18 ms 5972 KB Output is correct
6 Correct 60 ms 7244 KB Output is correct
7 Correct 64 ms 8316 KB Output is correct
8 Correct 62 ms 8024 KB Output is correct
9 Correct 36 ms 6708 KB Output is correct
10 Correct 46 ms 7192 KB Output is correct
11 Correct 66 ms 9600 KB Output is correct
12 Correct 71 ms 10148 KB Output is correct
13 Correct 38 ms 6492 KB Output is correct
14 Correct 53 ms 4564 KB Output is correct
15 Correct 21 ms 6060 KB Output is correct
16 Correct 5 ms 5460 KB Output is correct
17 Correct 4 ms 4948 KB Output is correct
18 Correct 70 ms 9304 KB Output is correct
19 Correct 77 ms 9308 KB Output is correct
20 Correct 43 ms 6732 KB Output is correct
21 Correct 24 ms 5988 KB Output is correct
22 Correct 4 ms 5332 KB Output is correct
23 Correct 24 ms 2252 KB Output is correct
24 Correct 43 ms 6560 KB Output is correct
25 Correct 28 ms 6476 KB Output is correct
26 Correct 41 ms 12764 KB Output is correct
27 Correct 76 ms 11100 KB Output is correct
28 Correct 37 ms 6488 KB Output is correct
29 Correct 42 ms 6492 KB Output is correct
30 Correct 72 ms 9488 KB Output is correct
31 Correct 35 ms 6740 KB Output is correct
32 Correct 31 ms 6372 KB Output is correct
33 Correct 31 ms 6488 KB Output is correct
34 Correct 34 ms 6508 KB Output is correct
35 Correct 36 ms 6480 KB Output is correct
36 Correct 38 ms 10720 KB Output is correct
37 Correct 37 ms 6512 KB Output is correct