Submission #474650

#TimeUsernameProblemLanguageResultExecution timeMemory
474650sean617우호 조약 체결 (JOI14_friends)C++98
100 / 100
124 ms13832 KiB
#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 (stderr)

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++) {
      |               ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...