이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
#define int long long
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
ostream &operator << (ostream &cout, vector <int> &a) {
cout << "{";
for (auto & it : a) cout << it << ", ";
cout << "}";
return cout;
}
ostream &operator << (ostream &cout, set <int> &a) {
cout << "{";
for (auto & it : a) cout << it << ", ";
cout << "}";
return cout;
}
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 3e5 + 15;
int n, m, p[N], sz[N];
set <int> gr[N], gr_find[N];
set <int> g[N];
int find(int v) {
if(v == p[v])
return v;
return p[v] = find(p[v]);
}
inline void unio(int a, int b) {
a = find(a);
b = find(b);
if(a != b) {
// if(gr[a].size() < gr[b].size())
// swap(a, b);
// gr[a].insert(gr[b].begin(), gr[b].end());
p[b] = a;
// sz[a] += sz[b];
}
}
ll ans;
inline void slow_calc_ans(){
ans = 0;
for(int i = 1; i <= n; ++i) {
set <int> s = {};
for(int to : g[i]) {
if(find(i) != find(to))
s.insert(find(to));
}
for(int j : s)
ans += sz[j];
}
set <int> s = {};
for(int i = 1; i <= n; ++i)
s.insert(find(i));
for(int i : s)
ans += sz[i] * (sz[i] - 1);
return;
for(int v = 1; v <= n; ++v)
s.insert(find(v));
ans = 0;
for(int j : s) {
for(int k : gr[j])
if(find(k) != find(j))
ans += sz[find(j)];
ans += 1ll * sz[j] * (sz[j] - 1);
}
}
main() {
for(int i = 0; i < N; ++i)
p[i] = i, sz[i] = 0;
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
// freopen("input.txt", "r", stdin);
cin >> n >> m;
while(m--) {
int u, v;
cin >> u >> v;
if(find(u) == find(v)) {
cout << ans << endl;
continue;
}
bool flag = false;
g[u].insert(v);
set <pii> is = {};
for(int j = 1; j <= n; ++j) {
for(int to : g[j])
is.insert({find(j), find(to)});
}
for(auto it : is)
if(is.count(mp(it.se, it.f)))
unio(it.f, it.se);
for(int i = 1; i <= n; ++i)
sz[i] = 0;
for(int i = 1; i <= n; ++i)
++sz[find(i)];
// for(int j : gr[find(u)])
// flag |= (find(j) == find(v));
if(flag)
unio(u, v);
// else
// gr[find(v)].insert(u);
slow_calc_ans();
cout << ans << endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
joitter2.cpp:125:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |