이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 100005;
int n,m;
int ans;
struct ufds_ {
int p[MAXN], sz[MAXN];
unordered_set<int> innodes[MAXN];
unordered_set<int> in[MAXN], out[MAXN];
ufds_() {
iota(p,p+MAXN,0);
fill(sz,sz+MAXN,1);
FOR(i,0,MAXN-1) {
innodes[i].insert(i);
}
}
int find(int x) {
if(x==p[x])return x;
else return p[x]=find(p[x]);
}
} ufds;
int pairs(int sz) {
return sz*(sz-1);
}
int32_t main()
{
IAMSPEED
cin >> n >> m;
FOR(idx,1,m) {
int a,b; cin >> a >> b;
// insert a->b
int ga=ufds.find(a);
int gb=ufds.find(b);
if(ga==gb){
cout<<ans<<'\n';
continue;
}
if(ufds.in[ga].find(gb) != ufds.in[ga].end()) {
queue<pi> q;
q.push({ga,gb}); // merge ga, gb;
while(q.size()) {
pi cur=q.front(); q.pop();
if(ufds.find(cur.f) == ufds.find(cur.s))continue;
int x= ufds.find(cur.f), y=ufds.find(cur.s);
ans-=pairs(ufds.sz[x]);
ans-=ufds.sz[x] * (ufds.innodes[x].size() - ufds.sz[x]);
ans-=pairs(ufds.sz[y]);
ans-=ufds.sz[y] * (ufds.innodes[y].size() - ufds.sz[y]);
if(ufds.innodes[x].size() < ufds.innodes[y].size())swap(x,y);
// innodes[x].size() >= innodes[y].size()
for(auto i:ufds.innodes[y])ufds.innodes[x].insert(i);
ufds.sz[x]+=ufds.sz[y];
ufds.p[y] = x;
ans += pairs(ufds.sz[x]);
ans += ufds.sz[x] * (ufds.innodes[x].size() - ufds.sz[x]);
for(auto i:ufds.out[y]) { // these things need to be updated from y to x.
ufds.in[i].erase(y);
ufds.in[i].insert(x);
ufds.out[x].insert(i);
if(ufds.in[x].find(i) != ufds.in[x].end()) {
q.push({x,i});
}
}
for(auto i:ufds.in[y]){
ufds.in[x].insert(i);
ufds.out[i].erase(y);
ufds.out[i].insert(x);
if(ufds.in[i].find(x) != ufds.in[i].end()) {
q.push({x,i});
}
}
}
} else {
if(ufds.innodes[gb].find(a) == ufds.innodes[gb].end()) {
ufds.in[gb].insert(ga);
ufds.innodes[gb].insert(a);
ufds.out[ga].insert(gb);
ans+=ufds.sz[gb];
}
}
cout << ans << '\n';
}
return (0-0) ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |