답안 #444843

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
444843 2021-07-15T14:42:43 Z ics0503 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
6 ms 11596 KB
#include<stdio.h>
#include<vector>
#include<set>
using namespace std;
multiset<int>edge[121212],redge[121212];
long long par[121212], sz[121212];
int find(int x) {
	if (par[x] == x)return x;
	return par[x] = find(par[x]);
}
int main() {
	int n, m; scanf("%d%d", &n, &m);
	int i, j;
	for (i = 1; i <= n; i++)par[i] = i, sz[i] = 1;
	long long ans = 0;
	for (i = 0; i < m; i++) {
		int s, e; scanf("%d%d", &s, &e);
		int ps = find(s), pe = find(e);
		if (ps == pe) {
			printf("%lld\n", ans);
			continue;
		}
		if (redge[ps].find(pe) != redge[ps].end()) {
			ans -= sz[ps] * (sz[ps] - 1);
			ans -= sz[pe] * (sz[pe] - 1);
			
			if (edge[ps].size() + redge[ps].size() < edge[pe].size() + redge[pe].size()) {
				swap(ps, pe);
			}
			for (auto v : redge[pe]) {
				if (v == ps) { ans -= sz[pe]; continue; }
				edge[v].erase(edge[v].find(pe));
				edge[v].insert(ps);
				if (redge[ps].find(v) == redge[ps].end()) {
					ans -= sz[pe];
					ans += (sz[ps] + sz[pe]);
					redge[ps].insert(v);
				}
			}
			for (auto v : redge[ps]) {
				if (v == pe) { ans -= sz[ps]; continue; }
				if (redge[pe].find(v) == redge[pe].end()) {
					ans -= sz[ps];
					ans += (sz[ps] + sz[pe]);
				}
			}
			for (auto v : edge[pe]) {
				if (v == ps)continue;
				if (edge[ps].find(v) == edge[ps].end()) {
					edge[ps].insert(v);
					redge[v].erase(redge[v].find(pe));
					redge[v].insert(ps);
				}
			}
			edge[ps].erase(pe);
			sz[ps] += sz[pe];
			par[pe] = ps;
			ans += sz[ps] * (sz[ps] - 1);
		}
		else {
			if (edge[ps].find(pe) == edge[ps].end()) {
				edge[ps].insert(pe);
				redge[pe].insert(ps);
				ans += sz[pe];
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:13:9: warning: unused variable 'j' [-Wunused-variable]
   13 |  int i, j;
      |         ^
joitter2.cpp:12:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |  int n, m; scanf("%d%d", &n, &m);
      |            ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:17:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |   int s, e; scanf("%d%d", &s, &e);
      |             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 11596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 11596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 11596 KB Output isn't correct
2 Halted 0 ms 0 KB -