답안 #474650

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474650 2021-09-19T09:24:23 Z sean617 우호 조약 체결 (JOI14_friends) C++
100 / 100
124 ms 13832 KB
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define N 100005
using namespace std;

typedef long long ll;
ll n, m, ans, bu[N], v[N], m1[N*2], m2[N*2], s[N];
vector<int> a[N];
queue<int> qu;
ll f(ll p) {
	if (bu[p] == p) return p;
	else return bu[p] = f(bu[p]);;
}

void pu(int p) {
	if (v[p]) return;
	v[p] = 1;
	qu.push(p);
}

int main()
{
	ll i, j, t, t1, t2, x1, x2;
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> t1 >> t2;
		m1[i] = t1;
		m2[i] = t2;
		a[t1].push_back(t2);
	}
	for (i = 1; i <= n; i++) bu[i] = i;
	for (i = 1; i <= n; i++) {
		if (a[i].size() >= 2) {
			for (j = 1; j < a[i].size(); j++) {
				x1 = f(a[i][0]);
				x2 = f(a[i][j]);
				if (x1 != x2) bu[x2] = x1;
				pu(a[i][0]);
				pu(a[i][j]);
			}
		}
	}
	while (!qu.empty()) {
		t = qu.front(); qu.pop();
		for (i = 0; i < a[t].size(); i++) {
			t2 = a[t][i];
			x1 = f(t);
			x2 = f(t2);
			if (x1 != x2) bu[x2] = x1;
			pu(t2);
		}
	}
	for (i = 1; i <= n; i++) f(i);
	for (i = 1; i <= n; i++) {
		s[bu[i]]++;
	}
	for (i = 1; i <= n; i++) {
		if (s[i] > 1) ans += s[i] * (s[i] - 1);
	}
	for (i = 0; i < m; i++) {
		if (bu[m1[i]] != bu[m2[i]]) ans++;
	}
	cout << ans;
    return 0;
}

Compilation message

friends.cpp: In function 'int main()':
friends.cpp:38:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |    for (j = 1; j < a[i].size(); j++) {
      |                ~~^~~~~~~~~~~~~
friends.cpp:49:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   for (i = 0; i < a[t].size(); i++) {
      |               ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2608 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2596 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 3 ms 2668 KB Output is correct
17 Correct 2 ms 2672 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2892 KB Output is correct
2 Correct 3 ms 2892 KB Output is correct
3 Correct 9 ms 3480 KB Output is correct
4 Correct 40 ms 7300 KB Output is correct
5 Correct 30 ms 6280 KB Output is correct
6 Correct 50 ms 9092 KB Output is correct
7 Correct 3 ms 2764 KB Output is correct
8 Correct 6 ms 3096 KB Output is correct
9 Correct 9 ms 3660 KB Output is correct
10 Correct 18 ms 4444 KB Output is correct
11 Correct 50 ms 9100 KB Output is correct
12 Correct 7 ms 3276 KB Output is correct
13 Correct 4 ms 3020 KB Output is correct
14 Correct 3 ms 2892 KB Output is correct
15 Correct 5 ms 3148 KB Output is correct
16 Correct 10 ms 3792 KB Output is correct
17 Correct 49 ms 9028 KB Output is correct
18 Correct 3 ms 3020 KB Output is correct
19 Correct 3 ms 3020 KB Output is correct
20 Correct 50 ms 9168 KB Output is correct
21 Correct 5 ms 3020 KB Output is correct
22 Correct 3 ms 3020 KB Output is correct
23 Correct 6 ms 3148 KB Output is correct
24 Correct 3 ms 2892 KB Output is correct
25 Correct 3 ms 3020 KB Output is correct
26 Correct 5 ms 3020 KB Output is correct
27 Correct 4 ms 3020 KB Output is correct
28 Correct 4 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 6980 KB Output is correct
2 Correct 124 ms 11184 KB Output is correct
3 Correct 103 ms 13572 KB Output is correct
4 Correct 53 ms 9796 KB Output is correct
5 Correct 23 ms 7740 KB Output is correct
6 Correct 81 ms 11268 KB Output is correct
7 Correct 75 ms 13532 KB Output is correct
8 Correct 75 ms 13508 KB Output is correct
9 Correct 47 ms 10176 KB Output is correct
10 Correct 60 ms 11724 KB Output is correct
11 Correct 83 ms 13460 KB Output is correct
12 Correct 89 ms 13832 KB Output is correct
13 Correct 35 ms 9996 KB Output is correct
14 Correct 54 ms 9380 KB Output is correct
15 Correct 22 ms 7628 KB Output is correct
16 Correct 4 ms 4684 KB Output is correct
17 Correct 3 ms 4172 KB Output is correct
18 Correct 92 ms 13132 KB Output is correct
19 Correct 86 ms 13048 KB Output is correct
20 Correct 46 ms 9444 KB Output is correct
21 Correct 22 ms 7420 KB Output is correct
22 Correct 3 ms 4300 KB Output is correct
23 Correct 26 ms 5964 KB Output is correct
24 Correct 52 ms 9700 KB Output is correct
25 Correct 36 ms 9948 KB Output is correct
26 Correct 48 ms 10180 KB Output is correct
27 Correct 86 ms 13152 KB Output is correct
28 Correct 40 ms 10248 KB Output is correct
29 Correct 44 ms 10308 KB Output is correct
30 Correct 118 ms 13664 KB Output is correct
31 Correct 42 ms 8772 KB Output is correct
32 Correct 39 ms 10068 KB Output is correct
33 Correct 44 ms 10048 KB Output is correct
34 Correct 43 ms 9928 KB Output is correct
35 Correct 34 ms 9960 KB Output is correct
36 Correct 37 ms 9996 KB Output is correct
37 Correct 39 ms 10212 KB Output is correct