This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int parent[100000],size[100000];
int find(int n) {
if (parent[n] != n) parent[n] = find(parent[n]);
return parent[n];
}
set<int> com[100000],in[100000],out[100000],in2[100000],out2[100000];
LLI ans = 0;
int merge(int a,int b) {
//cout<<"merge "<<a+1<<","<<b+1<<endl;
if (find(a) == find(b)) return 0;
in2[a].erase(b),in2[b].erase(a),out2[b].erase(a),out2[a].erase(b);
set<int> S;
ans -= (LLI) size[a]*in[a].size()+(LLI) size[b]*in[b].size();
//cout<<ans<<endl;
for (auto it = in[a].begin(); it != in[a].end(); it++) {
if (find(*it) != b) S.insert(*it);
//out[*it].erase(a);
//if (find(*it) != b) out[*it].insert(b);
}
for (auto it = in[b].begin(); it != in[b].end(); it++) {
if (find(*it) != a) S.insert(*it);
}
ans += (LLI) (size[a]+size[b])*S.size()+2LL*size[a]*size[b];
//cout<<ans<<endl;
in[b] = S;
vpii vv;
for (auto it = in2[a].begin(); it != in2[a].end(); it++) {
if (in2[*it].count(b)) vv.pb(mp(*it,b));
in2[b].insert(*it);
out2[*it].erase(a),out2[*it].insert(b);
}
for (auto it = out2[a].begin(); it != out2[a].end(); it++) {
if (out2[*it].count(b)) vv.pb(mp(*it,b));
out2[b].insert(*it);
in2[*it].erase(a),in2[*it].insert(b);
}
/*for (auto it = com[a].begin(); it != com[a].end(); it++) {
com[b].insert(*it);
//for (auto it2 = out[*it].begin(); it2 != out[*it].end(); it2++) {
// if (find(*it2) != b) in[*it2].erase(*it),in[*it2].insert(b);
//}
}
com[a].clear(),*/in[a].clear(),in2[a].clear(),out2[a].clear();
parent[a] = b,size[b] += size[a];
while (!vv.empty()) merge(vv.back().first,vv.back().second),vv.pop_back();
return 0;
}
int main() {
int i;
int N,M,A,B;
scanf("%d %d",&N,&M);
for (i = 0; i < N; i++) parent[i] = i,size[i] = 1;//,com[i].insert(i);
for (i = 0; i < M; i++) {
scanf("%d %d",&A,&B);
A--,B--;
//cout<<find(A)<<",,"<<find(B)<<endl;
if ((find(A) != find(B)) && !in[find(B)].count(A)) {
in[find(B)].insert(A),/*out[A].insert(find(B)),*/in2[find(B)].insert(find(A)),out2[find(A)].insert(find(B));
ans += size[find(B)];
if (in2[find(A)].count(find(B))) merge(find(A),find(B));
}
printf("%lld\n",ans);/*
cout<<"----------"<<endl;
for (int j = 0; j < N; j++) {
cout << j << ": ";
for (auto it = in[j].begin(); it != in[j].end(); it++) cout << *it << " ";
cout << j << ": ";
for (auto it = in2[j].begin(); it != in2[j].end(); it++) cout << *it << " ";
cout << endl;
}
cout<<"------------"<<endl;*/
}
return 0;
}
Compilation message (stderr)
joitter2.cpp: In function 'int main()':
joitter2.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&M);
~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |