# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
766761 | onjo0127 | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++17 | 8 ms | 14292 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(V) ((int)V.size())
using namespace std;
using pii = pair<int, int>;
using ll = long long;
set<pii> E;
int P[100009], B[100009];
set<int> I[100009], O[100009], A[100009];
ll ans;
int root(int x) {
if(P[x] == x) return x;
return P[x] = root(P[x]);
}
ll f(int x) {
return 1LL * sz(A[x]) * (sz(I[x]) - B[x]) + 1LL * sz(A[x]) * (sz(A[x]) - 1);
}
void merg(int u, int v) {
u = root(u); v = root(v);
if(A[u].size() > A[v].size()) swap(u, v);
ans -= f(u) + f(v);
B[v] += B[u];
for(auto& it: A[u]) {
if(O[v].find(it) != O[v].end()) ++B[v];
if(I[v].find(it) != I[v].end()) ++B[v];
A[v].insert(it);
}
for(auto& it: I[u]) {
if(I[v].find(it) != I[v].end()) --B[v];
else I[v].insert(it);
}
for(auto& it: O[u]) O[v].insert(it);
ans += f(v);
P[u] = v;
}
int main() {
int N, M; scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) {
P[i] = i;
A[i] = {i};
}
while(M--) {
int u, v; scanf("%d%d", &u, &v);
E.insert({u, v});
int U = root(u), V = root(v);
if(U != V) {
ans -= f(V);
I[V].insert(U);
O[U].insert(V);
ans += f(V);
if(E.find({v, u}) != E.end()) merg(u, v);
}
printf("%lld\n", ans);
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |