이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
// using namespace __gnu_pbds;
#define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
#define SPEED ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define EL cout << '\n'
#define dbg(x) cout << #x << " = " << (x) << ' '
#define dbgp(x) cout << #x << " = (" << (x.fi) << ", " << (x.se) << ") "
#define dbga(x, l, r) {cout << #x << '[' << l << ", " << r << "] = { "; for(int i = l; i <= (int)r; ++i) cout << x[i] << ' '; cout << "} ";}
#define epl emplace
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define lwb lower_bound
#define upb upper_bound
#define clz __builtin_clzll // or __builtin_clz
#define ctz __builtin_ctzll // or __builtin_ctz
#define pct __builtin_popcountll // or __builtin_popcount
typedef long long ll;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef pair<ldb, ldb> pld;
typedef pair<double, double> pdd;
template<class T1, class T2> bool minimize(T1 &a, T2 b){return a > b ? a = b, true : false;}
template<class T1, class T2> bool maximize(T1 &a, T2 b){return a < b ? a = b, true : false;}
template<class T> void remDup(vector<T> &v){sort(all(v)); v.erase(unique(all(v)),v.end());}
// int d4x[4] = {1, 0, -1, 0}, d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// int d4y[4] = {0, 1, 0, -1}, d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
const int N = 1e5 + 3;
ll res;
int n, m, pr[N];
queue<pii> to_merge;
set<int> follower[N], adj[N], radj[N];
void add_big_edge(int u, int v){
//them canh giua nhung thanh phan lien thong
adj[u].insert(v); radj[v].insert(u);
if(adj[v].count(u)) to_merge.push({u, v});
}
int find_set(int u){
return pr[u] < 0 ? u : pr[u] = find_set(pr[u]);
}
int component_sz(int u){
//component_sz(u) <= 2 * follower[u].size() vi vay dpt la O(M * 2 * log^2(N))
return follower[u].size() + adj[u].size() + radj[u].size();
}
void unite(int u, int v){
u = find_set(u);
v = find_set(v);
if(u == v) return;
if(component_sz(u) < component_sz(v)) swap(u, v);
res += 1LL * (-pr[u]) * follower[v].size() + 1LL * (-pr[v]) * follower[u].size();
pr[u] += pr[v]; pr[v] = u;
for(int x : follower[v]){
if(follower[u].count(x)) res -= (-pr[u]);
else follower[u].insert(x);
}
adj[u].erase(v); radj[u].erase(v);
adj[v].erase(u); radj[v].erase(u);
for(int x : adj[v]){
radj[x].erase(v);
add_big_edge(u, x);
}
for(int x : radj[v]){
adj[x].erase(v);
add_big_edge(x, u);
}
}
void TS_2392(){
cin >> n >> m;
memset(pr, 255, sizeof(pr));
for(int i = 1; i <= n; ++i){
follower[i].insert(i);
}
for(int i = 1; i <= m; ++i){
int u, v; cin >> u >> v;
int root_u = find_set(u);
int root_v = find_set(v);
if(root_u == root_v || follower[root_v].count(u)){
cout << res << '\n';
continue;
}
res += -pr[root_v];
follower[root_v].insert(u);
add_big_edge(root_u, root_v);
while(!to_merge.empty()){
int u, v;
tie(u, v) = to_merge.front();
to_merge.pop(); unite(u, v);
}
cout << res << '\n';
}
}
int main(){
SPEED; fileIO("text");
int numtest = 1; //cin >> numtest;
for(int itest = 1; itest <= numtest; ++itest){
// cout << "Case #" << itest << ": ";
TS_2392(); //cout << '\n';
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* small cases, special cases (n = 1 ???)
* do something instead of nothing: code bruteforce
* WRITE STUFF DOWN and STAY ORGANIZED
* DON'T GET STUCK ON ONE APPROACH
*/
컴파일 시 표준 에러 (stderr) 메시지
joitter2.cpp: In function 'int main()':
joitter2.cpp:8:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | #define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:120:12: note: in expansion of macro 'fileIO'
120 | SPEED; fileIO("text");
| ^~~~~~
joitter2.cpp:8:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | #define fileIO(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:120:12: note: in expansion of macro 'fileIO'
120 | SPEED; fileIO("text");
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |